前言:

本文内容:Swing和JFrame窗口、JDialog弹窗、Icon和ImageIcon标签、文本域JScroll面板、图片按钮 单选框 多选框、下拉框 列表框、文本框 密码框 文本域

推荐免费JavaGUI入门视频:【狂神说Java】GUI编程入门到游戏实战_哔哩哔哩_bilibili

Swing和JFrame窗体

窗口,面板:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.jokerdig.lesson4;

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

/**
* @author Joker大雄
* @data 2021/8/18 - 9:52
**/
public class JFrameDemo02 {
public static void main(String[] args) {
new MyJframe2().init();
}
}
class MyJframe2 extends JFrame{
public void init(){
this.setVisible(true);
this.setBounds(100,100,100,100);
JLabel label = new JLabel("这是Jlabel");
this.add(label);
//设置水平对齐
label.setHorizontalAlignment(SwingConstants.CENTER);

//获得容器
Container container =this.getContentPane();
container.setBackground(Color.pink);
//关闭事件
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

}
}

标签居中:

label.setHorizontalAlignment(SwingConstants.CENTER);

JDialog弹窗

JDialog:

  • 用来被弹出,默认有关闭事件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    package com.jokerdig.lesson4;

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

    /**
    * @author Joker大雄
    * @data 2021/8/18 - 10:09
    **/
    //主窗口
    public class DialogDemo extends JFrame {
    public DialogDemo(){
    this.setVisible(true);
    this.setSize(600,500);
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    //JFrame 放东西 容器
    Container container = this.getContentPane();
    //绝对布局
    container.setLayout(null);
    //按钮
    JButton button = new JButton("点击弹出对话框");
    button.setBounds(30,30,200,50);
    container.add(button);
    //点击这个按钮,弹出弹窗
    button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
    //弹窗
    new MyDialogDemo();
    }
    });
    }

    public static void main(String[] args) {
    new DialogDemo();
    }
    }
    //弹窗的窗口
    class MyDialogDemo extends JDialog{
    public MyDialogDemo() {
    this.setVisible(true);
    this.setBounds(100,100,500,500);
    // 弹窗默认有关闭监听
    // this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    Container con = this.getContentPane();
    con.setLayout(null);
    con.add(new Label("这就是一个弹窗"));
    }
    }

Icon、ImageIcon标签

label:

1
new JLabel("标题");

图标Icon:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.jokerdig.lesson4;

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

/**
* @author Joker大雄
* @data 2021/8/18 - 10:49
**/
public class ImageIconDemo extends JFrame {
public ImageIconDemo(){
//获取图片地址
JLabel label = new JLabel("imageIcon");
URL url=ImageIconDemo.class.getResource("1.jpg");

ImageIcon imageIcon=new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container con = getContentPane();
con.add(label);

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

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

文本域JScroll面板

JPanel:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.jokerdig.lesson5;

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

/**
* @author Joker大雄
* @data 2021/8/18 - 11:11
**/
public class JPanelDemo extends JFrame {

public JPanelDemo(){
Container con = this.getContentPane();
con.setLayout(new GridLayout(2,1,10,10));//有间距

JPanel jp = new JPanel(new GridLayout(1,3));
JPanel jp2 = new JPanel(new GridLayout(1,2));
JPanel jp3 = new JPanel(new GridLayout(3,1));
jp.add(new JButton("1"));
jp.add(new JButton("2"));
jp.add(new JButton("3"));
jp2.add(new JButton("2"));
jp2.add(new JButton("3"));
jp3.add(new JButton("2"));
jp3.add(new JButton("3"));

con.add(jp);
con.add(jp2);
con.add(jp3);
this.setVisible(true);
this.setBounds(100,100,100,100);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

}

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

JScrollPanel:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.jokerdig.lesson5;

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

/**
* @author Joker大雄
* @data 2021/8/18 - 11:16
**/
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container con = this.getContentPane();

//文本域
JTextArea textarea = new JTextArea(20,30);
textarea.setText("文本域");

//Scroll面板
JScrollPane jScrollPane = new JScrollPane(textarea);
con.add(jScrollPane);

this.setVisible(true);
this.setBounds(100,100,100,100);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

this.setVisible(true);
this.setBounds(100,100,300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

}

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

图片按钮、单选框、多选框

  • 图片按钮

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    package com.jokerdig.lesson5;

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

    /**
    * @author Joker大雄
    * @data 2021/8/18 - 11:28
    **/
    public class JButtonDemo1 extends JFrame {
    public JButtonDemo1(){
    Container con = this.getContentPane();
    //将图片变为图标
    URL url = JButtonDemo1.class.getResource("133.jpg");
    Icon icon = new ImageIcon(url);

    //把这个图标放在按钮
    JButton button = new JButton();
    button.setIcon(icon);
    button.setToolTipText("图片按钮");

    con.add(button);
    this.setVisible(true);
    this.setBounds(100,100,1001,100);
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

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

    }
  • 单选按钮(需要分组ButtonGroup)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    package com.jokerdig.lesson5;

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

    /**
    * @author Joker大雄
    * @data 2021/8/18 - 11:52
    **/
    public class JButtonDemo2 extends JFrame {
    public JButtonDemo2(){
    Container con = this.getContentPane();
    //将图片变为图标
    URL url = JButtonDemo1.class.getResource("133.jpg");
    Icon icon = new ImageIcon(url);

    //单选框
    JRadioButton jRadioButton1 = new JRadioButton("JRadioButton1");
    JRadioButton jRadioButton2 = new JRadioButton("JRadioButton1");

    //由于单选框只能选择一个,分组
    ButtonGroup group = new ButtonGroup();
    group.add(jRadioButton1);
    group.add(jRadioButton2);
    con.add(jRadioButton1,BorderLayout.NORTH);
    con.add(jRadioButton2,BorderLayout.SOUTH);

    this.setVisible(true);
    this.setBounds(100,100,1001,100);
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

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

    }
  • 复选按钮

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    package com.jokerdig.lesson5;

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

    /**
    * @author Joker大雄
    * @data 2021/8/18 - 11:52
    **/
    public class JButtonDemo3 extends JFrame {
    public JButtonDemo3(){
    Container con = this.getContentPane();
    //将图片变为图标
    URL url = JButtonDemo1.class.getResource("133.jpg");
    Icon icon = new ImageIcon(url);

    //多选框
    JCheckBox jCheckBox1 = new JCheckBox("Chebox1");
    JCheckBox jCheckBox2 = new JCheckBox("Chebox1");
    JCheckBox jCheckBox3 = new JCheckBox("Chebox1");

    con.add(jCheckBox1,BorderLayout.NORTH);
    con.add(jCheckBox2,BorderLayout.CENTER);
    con.add(jCheckBox3,BorderLayout.SOUTH);


    this.setVisible(true);
    this.setBounds(100,100,1001,100);
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

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

下拉框、列表框

  • 下拉框

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    package com.jokerdig.lesson6;

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

    /**
    * @author Joker大雄
    * @data 2021/8/18 - 12:01
    **/
    public class ComboxDemo1 extends JFrame {
    public ComboxDemo1(){
    Container con = this.getContentPane();
    //下拉框
    JComboBox jComboBox = new JComboBox();

    jComboBox.addItem(null);
    jComboBox.addItem("正字热映");
    jComboBox.addItem("即将下架");
    jComboBox.addItem("已下架");
    con.add(jComboBox);

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

    public static void main(String[] args) {
    new ComboxDemo1();
    }
    }
  • 列表框

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    package com.jokerdig.lesson6;

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

    /**
    * @author Joker大雄
    * @data 2021/8/18 - 12:01
    **/
    public class ComboxDemo2 extends JFrame {
    public ComboxDemo2(){
    Container con = this.getContentPane();

    //列表框
    String[]arge = {"1:","2:","3:"};
    JList jList = new JList(arge);
    con.add(jList);

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

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

应用场景:

  • 选择地区,或一些单个选项。
  • 列表用来展示信息,一般是动态扩容。

文本框、密码框、文本域

  • 文本框

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    package com.jokerdig.lesson6;

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

    /**
    * @author Joker大雄
    * @data 2021/8/18 - 12:01
    **/
    public class TextDemo1 extends JFrame {
    public TextDemo1(){
    Container con = this.getContentPane();

    //文本框
    JTextField text1 = new JTextField("hello");
    JTextField text2 = new JTextField("world");

    con.add(text1,BorderLayout.NORTH);
    con.add(text2,BorderLayout.SOUTH);


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

    public static void main(String[] args) {
    new TextDemo1();
    }
    }
  • 密码框

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    package com.jokerdig.lesson6;

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

    /**
    * @author Joker大雄
    * @data 2021/8/18 - 12:01
    **/
    public class TextDemo2 extends JFrame {
    public TextDemo2(){
    Container con = this.getContentPane();

    //密码框
    JPasswordField text1 = new JPasswordField("输入密码");

    JPasswordField text2 = new JPasswordField("world");
    //也可以自定义隐藏样式,默认是·
    text2.setEchoChar('*');
    con.add(text1,BorderLayout.NORTH);
    con.add(text2,BorderLayout.SOUTH);

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

    public static void main(String[] args) {
    new TextDemo2();
    }
    }
  • 文本域

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    package com.jokerdig.lesson6;

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

    /**
    * @author Joker大雄
    * @data 2021/8/18 - 12:01
    **/
    public class TextDemo3 extends JFrame {
    public TextDemo3(){
    Container con = this.getContentPane();

    //文本域
    JTextArea text1 = new JTextArea(20,30);

    text1.setText("文本域");
    //通过面板显示
    JScrollPane jScrollPane = new JScrollPane(text1);
    con.add(jScrollPane);

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

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