下拉框
public class TestComboboxDemo extends JFrame {
public TestComboboxDemo(){
Container contentPane = this.getContentPane();
JComboBox<Object> status = new JComboBox<>();
status.addItem(null);
status.addItem("正在热映");
status.addItem("已下架");
status.addItem("即将上映!");
contentPane.add(status);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize(500,350);
}
public static void main(String[] args) {
new TestComboboxDemo();
}
}
列表框
public class TestComboboxDemo extends JFrame {
public TestComboboxDemo(){
Container contentPane = this.getContentPane();
//生成列表的内容 稀疏数组
String[] contents = {"1","2","3"};
//列表中需要放内容
JList jList = new JList(contents);
contentPane.add(jList);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize(500,350);
}
public static void main(String[] args) {
new TestComboboxDemo();
}
}
文本框:
public class TestTextDemo01 extends JFrame {
public TestTextDemo01(){
Container contentPane = this.getContentPane();
contentPane.setLayout(null);
JTextField jTextField1 = new JTextField("hello");
JTextField jTextField2 = new JTextField("world",20);
jTextField1.setBounds(20,20,50,50);
jTextField2.setBounds(80,20,50,50);
contentPane.add(jTextField1);
contentPane.add(jTextField2);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize(500,350);
}
public static void main(String[] args) {
new TestTextDemo01();
}
}
密码框:
public class TestTextDemo01 extends JFrame {
public TestTextDemo01(){
Container contentPane = this.getContentPane();
JPasswordField jPasswordField = new JPasswordField();//*****
jPasswordField.setEchoChar('+');
contentPane.add(jPasswordField);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize(500,350);
}
public static void main(String[] args) {
new TestTextDemo01();
}
}
文本域
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container container = this.getContentPane();
JTextArea jTextArea = new JTextArea(20,50);
jTextArea.setText("欢迎大家进行学习哦!");
JScrollPane jScrollPane = new JScrollPane(jTextArea);
container.add(jScrollPane);
this.setVisible(true);
this.setBounds(100,100,300,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
配合面板使用
关于GUI的基础技术目前全部结束,接下来做一个小游戏吧!
下一节》》》贪吃蛇