JavaSE基础——GUI编程(Swing)

Swing

  1、简介

  Swing并没有完全取代AWT,而是基于AWT的架构之上。Swing仅仅是提供了能力更强大的用户界面组件。在使用Swing编写的程序中,还需要使用基本的AWT处理事件。从现在开始,Swing是“被绘制的”用户界面AWT是指像事件处理这样的窗口工具箱的底层机制。

  2、窗口和面板

import javax.swing.*;
import java.awt.*;

public class JFrameDemo {

    //初始化窗口
    public static void init() {
        JFrame jFrame = new JFrame("Jframe窗口");
        jFrame.setBounds(100,100,500,500);
        jFrame.setBackground(Color.white);
        jFrame.setResizable(true);
        jFrame.setVisible(true);
        jFrame.setLayout(new FlowLayout());
        JLabel jLabel = new JLabel("Jframe 文字标签");
        jFrame.add(jLabel,BorderLayout.CENTER);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        init();
    }
}

执行结果:
在这里插入图片描述
  2、弹窗

  弹出的窗口,默认有关闭事件

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 主窗口
 */
public class DialogDemo extends JFrame{
    public DialogDemo() {
        //设置窗口属性
        this.setBounds(100,100,500,500);
        this.setBackground(Color.BLACK);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //用容器装组件
        Container container = this.getContentPane();
        container.setLayout(new FlowLayout());
        Button button = new Button("button");
        button.setBounds(100,100,200,100);
        container.add(button);
        //给按钮添加事件监听
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyDialog();
            }
        });
    }

    public static void main(String[] args) {
        new DialogDemo();
    }
}

/**
 * 弹出的窗口
 */
class MyDialog extends JDialog{
    public MyDialog() {
        this.setBounds(100,100,200,200);
        this.setVisible(true);
        this.setResizable(false);
        this.setBackground(Color.green);

        //用一个容器来装标签
        Container container = this.getContentPane();
        container.setLayout(new FlowLayout());
        container.add(new JLabel("Jlabel 标签"));
    }
}

  3、标签和图标

绘出自己的图标

import javax.swing.*;
import java.awt.*;

/**
 * Icon图标,继承Jframe的同时实现Icon接口
 */
public class IconlDemo extends JFrame implements Icon {
    private int width;
    private int height;

    public IconlDemo() {
    }

    public IconlDemo(int width, int height) throws HeadlessException {
        this.width = width;
        this.height = height;
    }

    //初始化图标
    public void init() {
        IconlDemo iconlDemo = new IconlDemo(15,15);
        //把图片放在标签上
        JLabel jLabel = new JLabel("IconTest",iconlDemo,SwingConstants.CENTER);
        Container container = this.getContentPane();
        container.add(jLabel);
        this.pack();
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    //画图标
    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.fillRect(x,y,width,height);
    }

    @Override
    public int getIconWidth() {
        return this.width;
    }

    @Override
    public int getIconHeight() {
        return this.height;
    }

    //测试
    public static void main(String[] args) {
        new IconlDemo().init();
    }
}

把图片添加到标签上

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class ImageIconDemo extends JFrame {
    public ImageIconDemo() {
        JLabel jLabel = new JLabel("ImageIcon");
        //获取图片的地址
        URL url = ImageIconDemo.class.getResource("/com/westos/demo5/Irving.jpg");
        ImageIcon imageIcon = new ImageIcon(url);
        jLabel.setIcon(imageIcon);
        Container container = this.getContentPane();
        container.add(jLabel);
        this.setBounds(100,100,600,500);
        this.setBackground(Color.YELLOW);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    /**
     * 测试图片标签
     * @param args
     */
    public static void main(String[] args) {
        new ImageIconDemo();
    }
}

  4.1、面板Jpanel

import javax.swing.*;
import java.awt.*;

public class JPanelDemo extends JFrame {
    public JPanelDemo() {
        Container container = this.getContentPane();
        container.setBackground(Color.YELLOW);
        container.setLayout(new GridLayout(2,1,5,5));

        //创建面板对象
        JPanel jPanel1 = new JPanel(new GridLayout(2, 3));
        JPanel jPanel2 = new JPanel(new GridLayout(2, 2));
        jPanel1.add(new Button("1"));
        jPanel1.add(new Button("1"));
        jPanel1.add(new Button("1"));
        jPanel1.add(new Button("1"));
        jPanel1.add(new Button("1"));
        jPanel1.add(new Button("1"));
        jPanel2.add(new Button("2"));
        jPanel2.add(new Button("2"));
        jPanel2.add(new Button("2"));
        jPanel2.add(new Button("2"));
        container.add(jPanel1);
        container.add(jPanel2);
        this.setBounds(100,100,500,500);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    /**
     * 测试
     * @param args
     */
    public static void main(String[] args) {
        new JPanelDemo();
    }
}

执行结果:
在这里插入图片描述

  4.2、滚动面板JScrollPane

import javax.swing.*;
import java.awt.*;

/**
 * 滚动窗口
 */
public class JScrollPanelDemo extends JFrame {
    public JScrollPanelDemo() {
        Container container = this.getContentPane();

        //文本域
        TextArea textArea = new TextArea(20,30);
        textArea.setText("TextArea TextArea TextArea TextArea ");

        //滚动面板
        JScrollPane jScrollPane = new JScrollPane(textArea);
        container.add(jScrollPane);
        this.setVisible(true);
        this.setBounds(100,100,500,600);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    /**
     * 测试
     * @param args
     */
    public static void main(String[] args) {
        new JScrollPanelDemo();
    }
}

  5.1、图片按钮

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class JButtonDemo extends JFrame {
    public JButtonDemo() {
        //把这个图标变为图片
        Container container = this.getContentPane();
        URL url = JButtonDemo.class.getResource("/com/westos/demo6/Irving.jpg");
        Icon icon = new ImageIcon(url);

        //把图标添加到按钮上
        JButton jButton = new JButton(icon);
        jButton.setToolTipText("图片按钮");

        container.add(jButton);

        this.setSize(400,300);
        this.setVisible(true);
        this.pack();
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    /**
     * 测试
     * @param args
     */
    public static void main(String[] args) {
        new JButtonDemo();
    }
}

  5.1、单选按钮

import javax.swing.*;
import java.awt.*;

public class JButtonDemo2 extends JFrame {
    public JButtonDemo2() {

        Container container = this.getContentPane();
        //单选框
        JRadioButton radioButton1 = new JRadioButton("radioButton1");
        JRadioButton radioButton2 = new JRadioButton("radioButton2");
        JRadioButton radioButton3 = new JRadioButton("radioButton3");

        //给单选框分组
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(radioButton1);
        buttonGroup.add(radioButton2);
        buttonGroup.add(radioButton3);

        container.add(radioButton1,BorderLayout.SOUTH);
        container.add(radioButton2,BorderLayout.NORTH);
        container.add(radioButton3,BorderLayout.CENTER);
        container.setBackground(Color.YELLOW);
        this.setSize(600,500);
        this.setVisible(true);
        this.pack();
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    /**
     * 测试
     */
    public static void main(String[] args) {
        new JButtonDemo2();
    }
}

  5.1 复选按钮

import javax.swing.*;
import java.awt.*;

public class JButtonDemo3 extends JFrame {
    public JButtonDemo3() {
        Container container = this.getContentPane();

        //创建复选框
        Checkbox checkBox1 = new Checkbox("CheckBox01");
        checkBox1.setSize(200,200);
        Checkbox checkBox2 = new Checkbox("CheckBox02");
        checkBox2.setSize(200,200);
        //复选框放进窗口容器
        container.add(checkBox1,BorderLayout.SOUTH);
        container.add(checkBox2,BorderLayout.NORTH);

        //设置窗口属性
        this.setSize(400,300);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JButtonDemo3();
    }
}

  6.1、列表下拉框

import javax.swing.*;
import java.awt.*;
import java.util.Comparator;

public class ComboxDemo01 extends JFrame {
    public ComboxDemo01() {
        Container container = this.getContentPane();
        //创建下拉框
        JComboBox jComboBox = new JComboBox();

        jComboBox.addItem(null);
        jComboBox.addItem("拍电影");
        jComboBox.addItem("看电影");
        jComboBox.addItem("付费");

        container.add(jComboBox);
        container.setBackground(Color.YELLOW);
        this.setSize(400,300);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new ComboxDemo01();
    }
}

  6.2、列表框

/**
 * 列表框
 */

import javax.swing.*;
import java.awt.*;
import java.util.Vector;

public class ComboxDemo02 extends JFrame {
    public ComboxDemo02() {
        Container container = this.getContentPane();
        //列表的内容
        Vector<String> contents = new Vector<>();
        contents.add("上海自来水来自海上");
        contents.add("山东运粮车粮运东山");
        contents.add("山西产煤处煤产西山");
        //把集合放进列表中
        JList jList = new JList(contents);
        container.add(jList);
        //设置窗口属性
        this.setSize(400,300);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new ComboxDemo02();
    }
}

  6.3、文本框

import javax.swing.*;
import java.awt.*;

/**
 * 文本框
 */
public class TextDemo extends JFrame {
    public TextDemo() {
        Container container = this.getContentPane();
        container.setBackground(Color.YELLOW);
        //创建文本框对象
        JTextField jTextField1 = new JTextField("这是一个文本框1");
        JTextField jTextField2 = new JTextField("这是一个文本框2",10);

        container.add(jTextField1,BorderLayout.NORTH);
        container.add(jTextField2,BorderLayout.SOUTH);
        this.setSize(400,300);
        this.setVisible(true);
        this.pack();
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TextDemo();
    }
}

  6.4、密码框

import javax.swing.*;
import java.awt.*;

/**
 * 文本域
 */
public class JScrollDemo extends JFrame {
    public JScrollDemo() {
        Container container = this.getContentPane();

        //文本域
        JTextArea jTextArea = new JTextArea(25, 30);
        jTextArea.setText("这是文本域中的一行文本");

        //滚动面板
        JScrollPane jScrollPane = new JScrollPane(jTextArea);

        container.add(jScrollPane);
        this.setVisible(true);
        this.setSize(300,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JScrollDemo();
    }
}

总结:在GUI学习中的基本知识有Swing和AWT的组件,如文本组件,按钮和滑动组件等,这些都是基本的用户界面组件,使用也很频繁。

发布了58 篇原创文章 · 获赞 7 · 访问量 2286

猜你喜欢

转载自blog.csdn.net/weixin_42492089/article/details/103356414