【JavaGUI】JavaGUI入门到小游戏实战(3)
前言:
本文内容:Swing和JFrame窗口、JDialog弹窗、Icon和ImageIcon标签、文本域JScroll面板、图片按钮 单选框 多选框、下拉框 列表框、文本框 密码框 文本域
推荐免费JavaGUI入门视频:【狂神说Java】GUI编程入门到游戏实战_哔哩哔哩_bilibili
Swing和JFrame窗体
窗口,面板:
1 | package com.jokerdig.lesson4; |
标签居中:
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
53package 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() {
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 | package com.jokerdig.lesson4; |
文本域JScroll面板
JPanel:
1 | package com.jokerdig.lesson5; |
JScrollPanel:
1 | package com.jokerdig.lesson5; |
图片按钮、单选框、多选框
-
图片按钮
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
33package 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
38package 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
36package 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
30package 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
27package 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
30package 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
31package 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
30package 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();
}
}
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Hey,Joker!
评论
ValineTwikoo