Java-GUI基础(三)java.swing

1. 简介

swing与awt:可以认为awt是swing的前身,awt即Abstrace Window Toolkit抽象窗口工具包,swing是为了解决awt在开发中的问题而开发的,是awt的改良。

因此,在功能上swing比awt更加全面,而使用方法并未太大的差别,swing中的类名称通常在awt的类名称前添加字母J,如Frame-->JFrame。

本文对swing中以下部分进行解释:

窗口/面板、弹窗、标签、面板、按钮、列表、文本(框/域,密码框)

2. 窗口、面板

建立

JFrame、JPanel,在构建窗口中一般使用init()方法进行定义,init()只是一个普通方法,但是使用者比较广泛,就如同初学者多使用helloWorld()方法一样

此外,在swing中需要添加Container对象用以添加组件或相关设置

swing中提供了默认的关闭事件(开发者也可以自己编写监听器)

public class JFrameDemo{
         public static void main(String[] args){
                 new JFrameDemo.init();
                 //静态方法不能调用非静态方法--创建对象生成窗口
         }
         public void init(){
                 JFrame jframe = new JFrame("titleForJFrame");
                 Container container = jframe.getContentPane();//获取容器对象
                 jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                 jframe.setBounds(200,200,200,200);
                 jframe.setVisible(true);
                 Button button = new Button("button");
                 container.add(button);                 
          }
}

除上述外还可通过继承JFrame类来实现

public class JFrameDemo{
         public static void main(String[])args{
                 new MyFrame();
         }         
}
class MyJFrame extends JFrame{
         public MyJFrame(){
                 setBounds(200,200,200,200);
                 setVisible(true);
                 Container container = getContentPane();
                 Button button = new Button("button");
                 container.add(button);         
         }
}

布局

除直接对容器的布局管理之外还可在组件上设置

JLabel jLabel = new JLabel("label");
jLabel.setHorizontalAlignment(SwingConstants.CENTER);

3. 弹窗

JDialog,弹窗

与其他窗口一样是在事件发生时显示出来的新窗口,JFrame视为主窗口的话JDialog可视为副窗口

默认具有关闭事件,不用单独写

public class DialogDemo{
    public static void main(String[] args){
        new MainJFrame().init();
    }
}
class MainJFrame extends JFrame {
    public void init(){
        setBounds(200,200,200,200);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container container = getContentPane();
        container.setLayout(null);
        JButton jButton = new JButton("button");
        jButton.setBounds(20,20,150,150);
        jButton.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                new MyDialog();
            }
        });
        container.add(jButton);
    }
}
class MyDialog extends JDialog{
    public MyDialog(){
        setBounds(300,300,100,100);
        JLabel jLabel = new JLabel("label");
        setVisible(true);
    }
}

4. 标签

JLabel,主体为一串文字,可加上图标,即Icon、ImageIcon

标签:

public class JLabelDemo {
    public static void main(String[] args) {
        new MyJFrameDemo();
    }
}
class MyJFrameDemo extends JFrame{
    public MyJFrameDemo(){
        setBounds(200,200,200,200);
        setVisible(true);
        JLabel jLabel = new JLabel("this is a label");
        jLabel.setBackground(Color.BLUE);
        Container container = getContentPane();
        container.add(jLabel);
        container.setBackground(Color.CYAN);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

获取、添加标签

图形图标

public class Demo {
    public static void main(String[] args) {
        new MyFrame01().init();
    }
}

class MyIcon implements Icon {
    private int width, height;

    public MyIcon() {
    }

    public MyIcon(int width, int height) {
        this.height = height;
        this.width = width;
    }

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.fillOval(x, y, width, height);
    }

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

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

class MyFrame01 extends JFrame {
    public void init() {
        setBounds(200, 200, 200, 200);
        setVisible(true);
        Container container = getContentPane();
        JLabel jLabel = new JLabel("this ia a label", new MyIcon(15,15), SwingConstants.CENTER);
        container.add(jLabel);
    }
}

图片图标

public class Demo {
    public static void main(String[] args) {
        new MyJFrameDemo();
    }
}
class MyJFrameDemo extends JFrame {
    public MyJFrameDemo(){
        URL url = MyJFrameDemo.class.getResource("wx.jpg");
        //获取图片地址
        ImageIcon imageIcon = new ImageIcon(url);
        //将图片地址加载至图片图标
        setBounds(200,200,200,200);
        setVisible(true);
        JLabel jLabel = new JLabel("this is a label");
        jLabel.setIcon(imageIcon);
        jLabel.setHorizontalAlignment(SwingConstants.WEST);
        //以上上句等价于:JLabel jLabel = new JLabel("this is a label",imageIcon,SwingConstants.CENTER);
        Container container = getContentPane();
        container.add(jLabel);
        container.setBackground(Color.CYAN);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

5. 面板

实现原理与awt基本一致,分为普通面板和滚动面板

基本面板

JPanel

public class Demo {
public static void main(String[] args) {
new JPanelDemo01().init();
}
}
class JPanelDemo extends JFrame{
public void init(){
setBounds(200,200,200,200);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = getContentPane();
JPanel jPanel = new JPanel();
JButton jButton1 = new JButton("1");
JButton jButton2 = new JButton("2");
JButton jButton3 = new JButton("3");
JButton jButton4 = new JButton("4");
JButton jButton5 = new JButton("5");
JButton jButton6 = new JButton("6");
jPanel.setLayout(new GridLayout(2,3,10,10));
jPanel.add(jButton1);
jPanel.add(jButton2);
jPanel.add(jButton3);
jPanel.add(jButton4);
jPanel.add(jButton5);
jPanel.add(jButton6);
container.add(jPanel);
}
}

滚动面板

JScrollPane

当文本超过窗口大小是会有滚动条

public class Demo {
    public static void main(String[] args) {
        new MyFrameDemo().init();
    }
}

class MyFrameDemo extends JFrame {
    public void init() {
        Container container = getContentPane();
        setVisible(true);
        setBounds(200, 200, 200, 200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        JTextArea jTextArea = new JTextArea("welcome to JScrollPane", 20, 50);
        JScrollPane jScrollPane = new JScrollPane(jTextArea);
        container.add(jScrollPane);
    }
}

 6. 按钮

普通按钮,图片按钮,单选按钮,多选按钮

普通按钮 JButton

public class Demo {
    public static void main(String[] args) {
        new JButtonDemo01().init();
    }
}
class JButtonDemo01 extends JFrame{
    public void init(){
        setBounds(200,200,200,200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
        JButton jButton1 = new JButton("button");
        JButton jButton2 = new JButton("button");
        JButton jButton3 = new JButton("button");
        JButton jButton4 = new JButton("button");
        JButton jButton5 = new JButton("button");
        Container container = getContentPane();
        container.add(jButton1,BorderLayout.WEST);
        container.add(jButton2,BorderLayout.SOUTH);
        container.add(jButton3,BorderLayout.NORTH);
        container.add(jButton4,BorderLayout.CENTER);
        container.add(jButton5,BorderLayout.EAST);
    }
}

图片按钮

public class ButtonDemo extends JFrame {
    public ButtonDemo(){
        //get container
        Container container = getContentPane();
        URL url = ButtonDemo.class.getResource("wx.jpg");
        Icon icon = new ImageIcon(url);
        //put this icon on the button
        JButton jButton = new JButton();
        jButton.setIcon(icon);
        //setup the tip text (display when mouse stay on it)
        jButton.setToolTipText("this is a pic-button");
        container.add(jButton);
        setBounds(200,200,200,200);
        setBackground(Color.GREEN);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new ButtonDemo();
    }

}

单选按钮 

public class Demo extends JFrame {
    public Demo(){
        Container container = getContentPane();
        URL url = Demo.class.getResource("wx.jpg");
        ImageIcon imageIcon = new ImageIcon(url);
        //single choice outline
        //JRadioButton的声明和添加偶尔会出现一些意外,原因不明
        JRadioButton jRadioButton1 = new JRadioButton("choice1");
        JRadioButton jRadioButton2 = new JRadioButton("choice2");
        JRadioButton jRadioButton3 = new JRadioButton("choice3");
        //由于单选框只能选择一个,所以我们通常将其分组,就是一个组只能选一个,如果不分组的话三个都可以被选中,即形式上变成了多选框
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(jRadioButton1);
        buttonGroup.add(jRadioButton2);
        buttonGroup.add(jRadioButton3);
        //add button
        container.add(jRadioButton1,BorderLayout.NORTH);
        container.add(jRadioButton2,BorderLayout.CENTER);
        container.add(jRadioButton3,BorderLayout.SOUTH);

        setVisible(true);
        setBounds(200,200,200,200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

 多选按钮

JCheckBox

public class JCheckBoxDemo extends JFrame {
    public JCheckBoxDemo(){
        JCheckBox jCheckBox1 = new JCheckBox("choice01");
        JCheckBox jCheckBox2 = new JCheckBox("choice02");
        JCheckBox jCheckBox3 = new JCheckBox("choice03");
        //added into container
        container.add(jCheckBox1,BorderLayout.SOUTH);
        container.add(jCheckBox2,0);
        container.add(jCheckBox3,"North");
        //按钮位置设定的三种写法
        setVisible(true);
        setBounds(200,200,200,200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

 6. 列表

下拉框 JComBox

public class Demo {
    public static void main(String[] args) {
        new JComBox().init();
    }
}
class JComBox extends JFrame{
    public void init(){
        setBounds(200,200,200,200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
        Container container = getContentPane();
        container.setLayout(null);
        JComboBox jComboBox = new JComboBox();
        jComboBox.setToolTipText("this is a ComBox");
        jComboBox.addItem("statement01");
        jComboBox.addItem("statement02");
        jComboBox.addItem("statement03");
        jComboBox.addItem("statement04");
        jComboBox.setLocation(10,10);
        jComboBox.setSize(150,30);
        container.add(jComboBox);
    }
}

列表框 JList

public class Demo {
    public static void main(String[] args) {
        new JListDemo().init();
    }
}
class JListDemo extends JFrame {
    public void init() {
        setVisible(true);
        setBounds(200, 200, 200, 200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container container = getContentPane();
        container.setBackground(Color.BLACK);
        container.setLayout(null);
        setResizable(false);
        String[] str = {"statement01", "statement02", "statement03", "statement04"};
        JList jList = new JList(str);
        jList.setBackground(Color.CYAN);
        jList.setSize(150,80);
        jList.setLocation(20,20);
        container.add(jList);
    }
}

 7. 文本

文本框 JTextField

public class Demo {
    public static void main(String[] args) {
        new JTextFieldDemo().init();
    }
}
class JTextFieldDemo extends JFrame{
    public void init(){
        setBounds(200,200,200,200);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        Container container = getContentPane();
        container.setLayout(null);
        JTextField jTextField01 = new JTextField();
        jTextField01.setText("hello");
        jTextField01.setLocation(10,10);
        jTextField01.setSize(100,20);
        jTextField01.setColumns(10);
        JTextField jTextField02 = new JTextField();
        jTextField02.setText("world");
        jTextField02.setLocation(10,100);
        jTextField02.setSize(100,20);
        jTextField02.setColumns(10);
        container.add(jTextField01,BorderLayout.NORTH);
        container.add(jTextField02,BorderLayout.SOUTH);
    }
}

                                       

 文本域

TextArea

public class Demo {
    public static void main(String[] args) {
        new JTextAreaDemo().init();
    }
}

class JTextAreaDemo extends JFrame {
    public void init() {
        setBounds(200, 200, 200, 200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
        setResizable(false);
        Container container = getContentPane();
        TextArea jTextArea = new TextArea(20,50);
        jTextArea.setText("helloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworld");
        container.add(jTextArea);
    }
}

JTextArea

public class Demo {
    public static void main(String[] args) {
        new JTextAreaDemo().init();
    }
}

class JTextAreaDemo extends JFrame {
    public void init() {
        setBounds(200, 200, 200, 200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
        setResizable(true);
        Container container = getContentPane();
        JTextArea jTextArea = new JTextArea(20,50);
        jTextArea.setText("helloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworld");
        container.add(jTextArea);
    }
}

 密码框 JPasswordField

public class Demo {
    public static void main(String[] args) {
        new JPasswordFieldDemo();
    }
}
class JPasswordFieldDemo extends JFrame{
    public JPasswordFieldDemo(){
        setBounds(200,200,200,200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
        Container container = getContentPane();
        JPasswordField jPasswordField = new JPasswordField();
        jPasswordField.addActionListener(new MyListener000());
        container.add(jPasswordField);
    }
}
class MyListener000 implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        String str = e.getActionCommand();
        System.out.println(str);
    }
}

    

原文地址:https://www.cnblogs.com/YuanShiRenY/p/GUI-swing.html