Java:GUI-Swing(下)
学习资料
b站狂神说:https://www.bilibili.com/video/BV1DJ411B75F
按钮
单选按钮
package com.zy7y.gui.swing;
import javax.swing.*;
import java.awt.*;
/**
* @ProjectName: JavaSE
* @PackageName: com.zy7y.gui.swing
* @Author: zy7y
* @Date: 2020/8/16 下午2:47
* @Description: 按钮
*/
public class TestJButton extends JFrame {
public TestJButton(){
setTitle("单选按钮");
Container container = getContentPane();
setBounds(500,500,500,500);
// 单选按钮, 单选按钮只能选择一个,需要用到分组
JRadioButton jRadioButton = new JRadioButton("男");
JRadioButton jRadioButton1 = new JRadioButton("女");
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(jRadioButton);
buttonGroup.add(jRadioButton1);
container.add(jRadioButton, BorderLayout.WEST);
container.add(jRadioButton1, BorderLayout.EAST);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJButton();
}
}
多选按钮
package com.zy7y.gui.swing;
import javax.swing.*;
import java.awt.*;
/**
* @ProjectName: JavaSE
* @PackageName: com.zy7y.gui.swing
* @Author: zy7y
* @Date: 2020/8/16 下午2:47
* @Description: 按钮
*/
public class TestJButton extends JFrame {
public TestJButton(){
setTitle("多选按钮");
Container container = getContentPane();
setBounds(500,500,500,500);
// 多选按钮
Checkbox checkbox = new Checkbox("多选项");
Checkbox checkbox1 = new Checkbox("多选项1");
Checkbox checkbox2 = new Checkbox("多选项2");
container.add(checkbox, BorderLayout.WEST);
container.add(checkbox1, BorderLayout.EAST);
container.add(checkbox2, BorderLayout.CENTER);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJButton();
}
}
列表
下拉列表选择框
package com.zy7y.gui.swing;
import javax.swing.*;
import java.awt.*;
/**
* @ProjectName: JavaSE
* @PackageName: com.zy7y.gui.swing
* @Author: zy7y
* @Date: 2020/8/16 下午3:10
* @Description: 下拉框
*/
public class TestJComBox extends JFrame {
public TestJComBox(){
setTitle("下拉列表-JComboBox");
setVisible(true);
setBounds(300,100,300,300);
Container container = getContentPane();
JComboBox jComboBox = new JComboBox();
jComboBox.addItem("男");
jComboBox.addItem("女");
container.add(jComboBox);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJComBox();
}
}
列表数据展示
package com.zy7y.gui.swing;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
/**
* @ProjectName: JavaSE
* @PackageName: com.zy7y.gui.swing
* @Author: zy7y
* @Date: 2020/8/16 下午3:10
* @Description: 下拉框
*/
public class TestJComBox extends JFrame {
public TestJComBox(String title){
super(title);
setVisible(true);
setBounds(300,100,300,300);
Container container = getContentPane();
// 线程安全的数组
Vector vector = new Vector();
JList jList = new JList(vector);
// 给数组添加东西
vector.add("Java");
vector.add("Python");
vector.add("Golang");
container.add(jList);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJComBox("列表数据展示");
}
}
文本框
package com.zy7y.gui.swing;
import javax.swing.*;
import java.awt.*;
/**
* @ProjectName: JavaSE
* @PackageName: com.zy7y.gui.swing
* @Author: zy7y
* @Date: 2020/8/16 下午3:29
* @Description: 文本框、密码框、文本域
*/
public class TestText extends JFrame{
public TestText(){
setTitle("文本框、密码框");
setVisible(true);
setBounds(300,100,300,300);
Container container = getContentPane();
// 20个字符长度的文本框,有个默认值 "文本框"
JTextField jTextField = new JTextField("文本框",20);
container.add(jTextField, BorderLayout.AFTER_LAST_LINE);
// 密码框,16长度
JPasswordField jPasswordField = new JPasswordField(16);
container.add(jPasswordField, BorderLayout.AFTER_LINE_ENDS);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestText();
}
}