3-3 用label展示图片
public ImageIconDemo() {
//显示ImageIcon在label上
JLabel jlabel = new JLabel("ImageIcon");
URL url = getSource("D://JavaTest//test.png");
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setAlignment(SwingConstants.CENTER);
this.setVisible(true);
Container container = this.getContentPane();
container.add(label);
}
3-4 文本域,JSCroll面板
public JScrollDemo {
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20,30); //设定x大小为20,y大小为30
textArea.setText("Arthur");
//scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
}
3-5 单选框&多选框
单选框:
JRadioButton radioButton1 = new JRadioButton();
JRadioButton radioButton2 = new JRadioButton();
//存放单选button
ButtonGroup buttongroup = new ButtonGroup();
bg.add(radioButton1);
bg.add(radioButton2);
多选框
JCheckBox checkBox01 = new JCheckBox("CheckBox01");
JCheckBox checkBox02 = new JCheckBox("CheckBox02");
3-6 下拉框、列表框
下拉框
JComboBox status = new JComboBox();
status.addItem(null);
status.addItem("已下架");
status.addItem("正在上映");
列表框
//生成列表的内容
String[] contents = {"1", "2", "3"};
//列表中需要放入内容
JList jlist = new JList(contents);
3-7 文本框、密码框、文本域
文本框
JTextField textfield1 = new JTextField("hello");
JTextField textfield2 = new JTextField("world", 30); //30为最多输入字符的长度
密码框
JPasswordField passwordfield = new JPasswordField();
passwordfield.setEchoChar('a');
文本域
JTextArea textArea = new JTextArea(20,50);//设置row为20,col为50