Swing
窗体JFrame
实际上他就是对Frame进行了继承
public class JFrameTest {
public static void main(String[] args) {
new JFrameTest().init();
}
public void init(){
JFrame jframe = new JFrame("测试一下JFrame");
jframe.setBounds(400,400,400,400);
//JLabel jLabel = new JLabel("hello,world!");
//想让标签居中,采取以下方式
JLabel jLabel = new JLabel("hello,world!",SwingConstants.CENTER);
//或者这种方式,设置水平对齐
//jLabel.setHorizontalAlignment(SwingConstants.CENTER);
//添加label到窗口中
jframe.add(jLabel);
//此种方法设置背景颜色无效
//jframe.setBackground(Color.cyan);
//设置背景颜色需要先获取容器
Container content = jframe.getContentPane();
content.setBackground(Color.cyan);
jframe.setVisible(true);
jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
弹窗JDialog
public class JDialogTest extends JFrame {
public static void main(String[] args) {
new JDialogTest();
}
public JDialogTest(){
this.setVisible(true);
this.setSize(700,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(null);
JButton button = new JButton("点击会出现弹窗");
button.setBounds(20,20,100,50);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new MyDialog();
}
});
container.add(button);
}
}
class MyDialog extends JDialog{
public MyDialog() {
this.setVisible(true);
this.setBounds(100,100,200,200);
Container container = this.getContentPane();
container.add(new JLabel("你成功打开了弹窗",SwingConstants.CENTER));
}
}
标签Icon、ImageIcon
标签
public class IconTest extends JFrame implements Icon {
private int width;
private int height;
public IconTest(){
}
public IconTest(int width,int height){
this.height = height;
this.width = width;
}
public void init(){
setBounds(400,400,400,400);
IconTest icon = new IconTest(50, 50);
JLabel jLabel = new JLabel("这是一个图标",icon,SwingConstants.CENTER);
Container container = this.getContentPane();
container.add(jLabel);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new IconTest().init();
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
//g.fillOval(x,y,this.width,this.height);
g.fillRect(x,y,this.width,this.height);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
}
图片标签
public class ImageIconTest extends JFrame {
public ImageIconTest(){
JLabel label = new JLabel("这是图片标签");
URL url = ImageIconTest.class.getResource("icon1.jpg");
//图片位置要放到对应的out文件夹下否则会找不到
ImageIcon icon = new ImageIcon(url);
label.setIcon(icon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container = this.getContentPane();
container.add(label);
setVisible(true);
setBounds(400,400,400,400);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ImageIconTest();
}
}
图片需要放到对应的out文件夹下,否则会找不到
报以下错误
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
at com.nightfall.Lesson_4.ImageIconTest.<init>(ImageIconTest.java:12)
at com.nightfall.Lesson_4.ImageIconTest.main(ImageIconTest.java:25)
运行结果
面板、文本域JScroll
- 面板
public class JPanelTest extends JFrame {
public JPanelTest(){
Container container = this.getContentPane();
container.setLayout(new GridLayout(2,1,10,10));//后两个参数设置水平垂直间距
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
panel1.setLayout(new GridLayout(1,2));
panel2.setLayout(new GridLayout(2,1));
panel3.setLayout(new GridLayout(2,2));
panel4.setLayout(new GridLayout(3,2));
panel1.add(new Button("1"));
panel1.add(new Button("1"));
panel2.add(new Button("2"));
panel2.add(new Button("2"));
panel3.add(new Button("3"));
panel3.add(new Button("3"));
panel3.add(new Button("3"));
panel3.add(new Button("3"));
panel4.add(new Button("4"));
panel4.add(new Button("4"));
panel4.add(new Button("4"));
panel4.add(new Button("4"));
panel4.add(new Button("4"));
panel4.add(new Button("4"));
container.add(panel1);
container.add(panel2);
container.add(panel3);
container.add(panel4);
this.setVisible(true);
this.setBounds(400,400,500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelTest();
}
}
运行效果
- 文本域JScroll
public class JScrollTest extends JFrame {
public JScrollTest(){
Container container = this.getContentPane();
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("你好,这里在测试JScroll的效果");
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setBounds(400,400,400,200);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollTest();
}
}
运行结果
图片按钮、单选按钮、多选按钮
普通按钮
代码
public class JButtonTest extends JFrame {
public JButtonTest(){
this.setTitle("测试图片按钮");
this.setBounds(400,400,400,400);
this.setLayout(new FlowLayout());
Container container = this.getContentPane();
URL resource = JButtonTest.class.getResource("icon1.jpg");
Icon icon = new ImageIcon(resource);
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("图片按钮");
button.setBounds(10,10,100,50);
container.add(button);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonTest();
}
}
运行结果
单选按钮
代码
public class JButtonTest_2 extends JFrame {
public JButtonTest_2(){
this.setTitle("测试单选按钮");
this.setBounds(400,400,400,400);
this.setLayout(new FlowLayout());
Container container = this.getContentPane();
URL resource = JButtonTest.class.getResource("icon1.jpg");
Icon icon = new ImageIcon(resource);
JRadioButton jRadioButton_1 = new JRadioButton("JRadioButton_1");
JRadioButton jRadioButton_2 = new JRadioButton("JRadioButton_2");
JRadioButton jRadioButton_3 = new JRadioButton("JRadioButton_3");
//由于单选框只能选择一个,一般将他们放到一个组中,一个组中只能选一个
ButtonGroup group = new ButtonGroup();
group.add(jRadioButton_1);
group.add(jRadioButton_2);
group.add(jRadioButton_3);
this.add(jRadioButton_1);
this.add(jRadioButton_2);
this.add(jRadioButton_3);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonTest_2();
}
}
运行结果
多选按钮
代码
public class JButtonTest_3 extends JFrame {
public JButtonTest_3(){
this.setTitle("测试多选按钮");
this.setBounds(400,400,400,400);
this.setLayout(new FlowLayout());
Container container = this.getContentPane();
Checkbox checkbox_1 = new Checkbox("checkbox_1");
Checkbox checkbox_2 = new Checkbox("checkbox_2");
container.add(checkbox_1);
container.add(checkbox_2);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonTest_3();
}
}
运行结果
下拉框、列表框
下拉框
代码
public class ComboBoxTest extends JFrame {
public ComboBoxTest(){
this.setTitle("测试下拉列表");
this.setBounds(400,400,200,100);
Container container = this.getContentPane();
JComboBox status = new JComboBox();
status.addItem(null);
status.addItem("正在上映");
status.addItem("已下架");
status.addItem("即将上映");
container.add(status);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ComboBoxTest();
}
}
运行结果
列表框
简单理解为展示出来的列表框
代码
public class ComboBoxTest_2 extends JFrame {
public ComboBoxTest_2(){
this.setTitle("测试下拉列表");
this.setBounds(400,400,200,100);
Container container = this.getContentPane();
//生成列表的内容
String[] str = {"1","2","3"};
JList list = new JList(str);
container.add(list);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ComboBoxTest_2();
}
}
运行结果
应用场景
- 下拉框、选择地区
- 列表框、展示信息,一般是动态扩容
文本框、密码框、文本域
文本框
代码
public class TextTest_1 extends JFrame {
public TextTest_1(){
this.setTitle("测试文本框");
this.setLayout(new FlowLayout());
Container container = this.getContentPane();
JTextField field = new JTextField("hello");
JTextField field_2 = new JTextField("world",20);
container.add(field);
container.add(field_2);
this.setVisible(true);
this.setBounds(400,400,400,100);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TextTest_1();
}
}
运行结果
密码框
代码
public class TextTest_2 extends JFrame {
public TextTest_2(){
this.setTitle("测试密码框");
Container container = this.getContentPane();
JPasswordField field = new JPasswordField();
field.setEchoChar('*');
container.add(field);
this.setVisible(true);
this.setBounds(400,400,400,100);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TextTest_2();
}
}
运行结果
文本域
代码
public class JScrollTest extends JFrame {
public JScrollTest(){
Container container = this.getContentPane();
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("你好,这里在测试JScroll的效果");
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setBounds(400,400,400,200);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollTest();
}
}
运行结果