GUI编程
03 Swing
3.6 列表
- 下拉框
package com.duo.lesson06;import javax.swing.*;
import java.awt.*;public class ComboBoxDemo01 extends JFrame {public ComboBoxDemo01() throws HeadlessException {Container contentPane = getContentPane();JComboBox<Object> objectJComboBox = new JComboBox<>();objectJComboBox.addItem(null);objectJComboBox.addItem("热映中");objectJComboBox.addItem("即将上映");objectJComboBox.addItem("已下架");objectJComboBox.setFont(new Font("宋体", Font.BOLD, 20));contentPane.add(objectJComboBox);setTitle("下拉列表测试窗口");setVisible(true);setSize(350, 100);setLocationRelativeTo(null);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new ComboBoxDemo01();}
}
运行结果:
- 列表框
package com.duo.lesson06;import javax.swing.*;
import java.awt.*;
import java.util.Vector;public class ComboBoxDemo02 extends JFrame {public ComboBoxDemo02() {Container contentPane = getContentPane();//先生成一个列表String[] contents = {"One", "Two", "Three"};//用于展示contents的列表框JList<Object> objectJList1 = new JList<>(contents);contentPane.add(objectJList1);setTitle("列表框测试窗口");setVisible(true);setSize(650, 500);setLocationRelativeTo(null);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new ComboBoxDemo02();}
}
运行结果:
如图,列表框JList用于展示列表中的参数量。
列表的具体应用场景:
- 下拉列表:选择地区,或一些单个的选项
- 列表框:展示信息(可以自动动态扩容)
3.7 文本框
- 文本框
package com.duo.lesson06;import javax.swing.*;
import java.awt.*;public class TextDemo01 extends JFrame {public TextDemo01() {Container contentPane = getContentPane();contentPane.setLayout(null); //设置为绝对布局JTextField jTextField = new JTextField("Hello", 20);jTextField.setBounds(20, 20, 100, 25);JTextField jTextField2 = new JTextField("world", 20);jTextField2.setBounds(20, 50, 100, 25);contentPane.add(jTextField);contentPane.add(jTextField2);setTitle("文本框测试窗口");setVisible(true);setSize(650, 500);setLocationRelativeTo(null);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new TextDemo01();}
}
运行结果:
如上,此窗口面板设为了绝对布局。
- 密码框
package com.duo.lesson06;import javax.swing.*;
import java.awt.*;public class TextDemo02 extends JFrame {public TextDemo02() {Container contentPane = getContentPane();JPasswordField jPasswordField = new JPasswordField(); //密码框文本域默认输入为小黑圆点//jPasswordField.setEchoChar('*'); //同样可通过语句来设置输入文本后的显示样式为'*'contentPane.add(jPasswordField);setTitle("文本框测试窗口");setVisible(true);setSize(650, 500);setLocationRelativeTo(null);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new TextDemo02();}
}
运行结果:
- 文本域
package com.duo.lesson06;import javax.swing.*;
import java.awt.*;public class TextDemo03 extends JFrame {public TextDemo03() {Container contentPane = getContentPane();contentPane.setLayout(null);JTextArea jTextArea = new JTextArea("This is Schrodinger's channel.");jTextArea.setFont(new Font("Times New Roman", Font.PLAIN, 22));Dimension preferredSize = jTextArea.getPreferredSize(); //获得文本域的首选大小jTextArea.setBounds((650 - preferredSize.width) / 2,(500 - preferredSize.height) / 2 - preferredSize.height / 2,preferredSize.width, preferredSize.height);contentPane.add(jTextArea);setTitle("文本域测试窗口");setVisible(true);setSize(650, 500);setLocationRelativeTo(null);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new TextDemo03();}
}
运行结果: