图片按钮
public class TestJButtonIcon extends JFrame {
public static void main(String[] args) {
new TestJButtonIcon();
}
public TestJButtonIcon() {
Container contentPane = getContentPane();
URL url = TestJButtonIcon.class.getResource("迦南.png");
ImageIcon imageIcon = new ImageIcon(url);
JButton jButton = new JButton(imageIcon);
jButton.setToolTipText("图片按钮");
contentPane.add(jButton);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(500,500);
}
}
单选框
public class TestRadioButton extends JFrame {
public static void main(String[] args) {
new TestRadioButton();
}
public TestRadioButton() {
Container contentPane = getContentPane();
//单选框
JRadioButton jRadioButton01 = new JRadioButton("JRadioButton01");
JRadioButton jRadioButton02 = new JRadioButton("JRadioButton02");
JRadioButton jRadioButton03 = new JRadioButton("JRadioButton03");
//由于单选框只能选择一个,一组中只能选择一个
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(jRadioButton01);
buttonGroup.add(jRadioButton02);
buttonGroup.add(jRadioButton03);
contentPane.add(jRadioButton01,BorderLayout.CENTER);
contentPane.add(jRadioButton02,BorderLayout.NORTH);
contentPane.add(jRadioButton03,BorderLayout.SOUTH);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(500,500);
}
}
多选框
public class TestCheckBox extends JFrame {
public static void main(String[] args) {
new TestCheckBox();
}
public TestCheckBox() {
Container contentPane = getContentPane();
JCheckBox jCheckBox1 = new JCheckBox("JCheckBox1");
JCheckBox jCheckBox2 = new JCheckBox("JCheckBox2");
JCheckBox jCheckBox3 = new JCheckBox("JCheckBox3");
contentPane.add(jCheckBox1,BorderLayout.SOUTH);
contentPane.add(jCheckBox2,BorderLayout.CENTER);
contentPane.add(jCheckBox3,BorderLayout.NORTH);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(500,500);
this.setVisible(true);
}
}
下拉框
public class TestComboBox extends JFrame {
public static void main(String[] args) {
new TestComboBox();
}
public TestComboBox(){
Container contentPane = getContentPane();
JComboBox status = new JComboBox<>();
status.addItem("null");
status.addItem("选项一");
status.addItem("选项二");
status.addItem("选项三");
contentPane.add(status);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(500,500);
this.setVisible(true);
}
}
列表框
public class TestJList extends JFrame {
public static void main(String[] args) {
new TestJList();
}
public TestJList(){
Container contentPane = getContentPane();
//String[] array ={"1","2","3"};
Vector array = new Vector<>();
JList list = new JList<>(array);
array.add("zhangsan");
array.add("lisi");
array.add("wangwu");
contentPane.add(list);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(500,500);
}
}
应用场景
选择地区或者一些单个选项
列表,展示一些信息,一般是动态扩容
文本框
public class TestJTextField extends JFrame {
public static void main(String[] args) {
new TestJTextField();
}
public TestJTextField(){
Container contentPane = getContentPane();
//this.setLayOut(null);
JTextField hello = new JTextField("hello");
JTextField world = new JTextField("world", 20);
contentPane.add(hello,BorderLayout.NORTH);
contentPane.add(world,BorderLayout.SOUTH);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
密码框
public class TestPassword extends JFrame {
public static void main(String[] args) {
new TestPassword();
}
public TestPassword() {
Container contentPane = getContentPane();
JPasswordField jPasswordField = new JPasswordField();
jPasswordField.setEchoChar('*');
contentPane.add(jPasswordField);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(500,500);
this.setVisible(true);
}
}
文本域
public class JScrollDemo extends JFrame {
public static void main(String[] args) {
new JScrollDemo();
}
public JScrollDemo(){
Container contentPane = getContentPane();
//文本域
JTextArea jTextArea = new JTextArea(20, 50);
jTextArea.setText("刘铁锤真的棒");
//Scroll面板
JScrollPane jScrollPane = new JScrollPane(jTextArea);
contentPane.add(jScrollPane);
setVisible(true);
setBounds(100,100,300,350);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}