目录
一、图片按钮
代码演示:
package com.zibo.Lession05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class TestImageButton extends JFrame {
public TestImageButton() {
Container container = getContentPane();
URL url = TestImageButton.class.getResource("tx.jpg");
ImageIcon imageIcon = new ImageIcon(url);
JButton jButton = new JButton();
jButton.setIcon(imageIcon);
jButton.setToolTipText("图片按钮");
container.add(jButton);
setVisible(true);
setBounds(300,300,800,800);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestImageButton();
}
}
运行结果:
二、单选按钮
代码演示:
package com.zibo.Lession05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class TestJRadioButton extends JFrame {
public TestJRadioButton() {
Container container = getContentPane();
//单选框
JRadioButton rb1 = new JRadioButton("RB1");
JRadioButton rb2 = new JRadioButton("RB2");
JRadioButton rb3 = new JRadioButton("RB3");
//由于单选框只能选一个,我们将他们成组
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(rb1);
buttonGroup.add(rb2);
buttonGroup.add(rb3);
container.add(rb1,BorderLayout.NORTH);
container.add(rb2,BorderLayout.CENTER);
container.add(rb3,BorderLayout.SOUTH);
setVisible(true);
setBounds(300,300,500,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJRadioButton();
}
}
运行结果:
三、多选按钮
代码演示:
package com.zibo.Lession05;
import javax.swing.*;
import java.awt.*;
public class TestJCheckBox extends JFrame {
public TestJCheckBox() {
Container container = getContentPane();
//多选框
JCheckBox cb1 = new JCheckBox("cb1");
JCheckBox cb2 = new JCheckBox("cb2");
JCheckBox cb3 = new JCheckBox("cb3");
container.add(cb1,BorderLayout.NORTH);
container.add(cb2,BorderLayout.CENTER);
container.add(cb3,BorderLayout.SOUTH);
setVisible(true);
setBounds(300,300,500,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJCheckBox();
}
}
运行结果:
四、下拉框
代码演示:
package com.zibo.lession06;
import com.zibo.Lession05.TestJCheckBox;
import javax.swing.*;
import java.awt.*;
public class TestJComboBox extends JFrame {
public TestJComboBox(){
Container container = getContentPane();
JComboBox<String> jComboBox = new JComboBox<>();
jComboBox.addItem("one");
jComboBox.addItem("two");
jComboBox.addItem("three");
container.add(jComboBox);
setVisible(true);
setBounds(300,300,300,120);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJComboBox();
}
}
运行结果:
五、列表框
代码演示:
package com.zibo.lession06;
import javax.swing.*;
import java.awt.*;
public class TestJList extends JFrame {
public TestJList() {
Container container = getContentPane();
//列表
String[] strings = {"大哥","二哥","三哥","四哥"};
JList<String> jList = new JList<>(strings);
container.add(jList);
setVisible(true);
setBounds(300,300,500,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJList();
}
}
运行结果:
备注代码:
//可用此实现动态增减
Vector<String> vector = new Vector<>();
JList<String> jList = new JList<>(vector);
vector.add("大哥");
vector.add("二哥");
vector.add("三哥");
vector.add("四哥");
vector.remove("三哥");
六、文本框
代码演示:
package com.zibo.lession06;
import javax.swing.*;
import java.awt.*;
public class TestJTextField extends JFrame {
public TestJTextField() {
Container container = getContentPane();
//文本框
JTextField jTextField = new JTextField("这是文本框");
container.add(jTextField);
setVisible(true);
setBounds(300,300,500,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJTextField();
}
}
运行结果:
七、密码框
代码演示:
package com.zibo.lession06;
import javax.swing.*;
import java.awt.*;
public class TestJPasswordField extends JFrame {
public TestJPasswordField() {
Container container = getContentPane();
//文本框
JPasswordField jPasswordField = new JPasswordField("这是密码框");
jPasswordField.setEchoChar('*');
container.add(jPasswordField);
setVisible(true);
setBounds(300,300,500,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJPasswordField();
}
}
运行结果:
八、文本域
代码演示:
package com.zibo.Lession05;
import javax.swing.*;
import java.awt.*;
//带滚动条的面板
public class TestJScrollPane extends JFrame {
public TestJScrollPane() {
Container container = getContentPane();
//文本域
JTextArea jTextArea = new JTextArea(20, 50);
jTextArea.setText("TestJScrollPane");
//面板
JScrollPane jScrollPane = new JScrollPane(jTextArea);
container.add(jScrollPane);
setVisible(true);
setBounds(300,300,300,350);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJScrollPane();
}
}
运行结果: