GUI编程---Swing

GUI

Swing

1. 窗口、面板

package com.GUI.lesson04;

import javax.swing.*;
import java.awt.*;

public class JFrameDemo {

    public void init(){
        JFrame jframe = new JFrame("JFrame!");
        jframe.setVisible(true);
        jframe.setBounds(100,100,300,300);
        jframe.setBackground(Color.cyan);
        JLabel Label = new JLabel("初始化文字",SwingConstants.CENTER);//居中
        jframe.add(Label);
        
        Container contentPane = jframe.getContentPane();//创建容器,在容器里对面板进行控制
        contentPane.setBackground(Color.black);//黑色覆盖之前的颜色,表明这个方法也可行


        jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭事件
    }

    public static void main(String[] args) {
        new JFrameDemo().init();

    }
}

在这里插入图片描述

2. 弹窗

JDialog,用来被弹出,默认有关闭事件
package com.GUI.lesson04;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//主窗口
public class DialogDemo extends JFrame {

    public DialogDemo(){
        this.setVisible(true);
        this.setBounds(200,200,400,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //JFrame 放东西,容器
        Container contentPane = this.getContentPane();
        contentPane.setLayout(null);//绝对布局

        JButton button = new JButton("弹窗");
        button.setBounds(250,250,30,30);
        button.addActionListener(new ActionListener() {//监听器
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyDialogDemo();//弹窗
            }
        });

        contentPane.add(button);

    }

    public static void main(String[] args) {
        new DialogDemo();
    }
}
//弹窗的窗口
class MyDialogDemo extends JDialog{
    public MyDialogDemo() {
        this.setVisible(true);
        this.setBounds(300,300,300,300);
        //this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);默认有关闭事件

        Container contentPane1 = this.getContentPane();
        contentPane1.setLayout(null);//绝对布局

        contentPane1.add(new Label("信息学习"));
    }
}

在这里插入图片描述

3. 标签 Label

JLabel Label = new JLabel("初始化文字",SwingConstants.CENTER);//居中
3.1 插入图标
package com.GUI.lesson04;

import javax.swing.*;
import java.awt.*;

//图标,需要实现类,Frame继承
public class IconDemo extends JFrame implements Icon {
    private int width;
    private int height;
    public IconDemo() {}//无参构造
    public IconDemo(int width, int height) {//有参构造
        this.width = width;
        this.height = height;
    }
    public void init(){
        IconDemo iconDemo = new IconDemo(15, 15);
        JLabel label = new JLabel("iconDemo", iconDemo, SwingConstants.CENTER);//图标放在标签上
        Container contentPane = getContentPane();//配置容器
        contentPane.add(label);

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {g.fillOval(x,y,width,height);}//图标是个圆
    @Override
    public int getIconWidth() {return this.width;}
    @Override
    public int getIconHeight() {return this.height;}

    
    public static void main(String[] args) {
        new IconDemo().init();

    }
}
3.2 插入图片
package com.GUI.lesson04;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class ImageIconDemo extends JFrame{
    public ImageIconDemo() {
        JLabel label = new JLabel("ImageIcon");//建立标签
        
        URL url = ImageIconDemo.class.getResource("p.jpg");//获得当前类的同级资源
        ImageIcon imageIcon = new ImageIcon(url);//将资源变为图片
        
        label.setIcon(imageIcon);//标签设置图片
        label.setHorizontalAlignment(SwingConstants.CENTER);//设置文本居中

        Container contentPane = getContentPane();//初始化容器
        contentPane.add(label);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBounds(200,200,400,400);
    }

    public static void main(String[] args) {
        new ImageIconDemo();
    }
}

在这里插入图片描述

4. 面板 JPanel

package com.GUI.lesson05;

import javax.swing.*;
import java.awt.*;

public class JPanelDemo extends JFrame {
    public JPanelDemo() {
        Container contentPane = this.getContentPane();
        contentPane.setLayout(new GridLayout(2,1,10,10));//后面参数的意思是间距

        JPanel jPanel1 = new JPanel(new GridLayout(1,3));
        JPanel jPanel2 = new JPanel(new GridLayout(1,2));
        JPanel jPanel3 = new JPanel(new GridLayout(2,1));
        JPanel jPanel4 = new JPanel(new GridLayout(3,2));

        jPanel1.add(new JButton("1"));
        jPanel1.add(new JButton("2"));
        jPanel1.add(new JButton("3"));
        jPanel2.add(new JButton("3"));
        jPanel2.add(new JButton("3"));
        jPanel3.add(new JButton("3"));
        jPanel3.add(new JButton("3"));
        jPanel4.add(new JButton("3"));
        jPanel4.add(new JButton("3"));
        jPanel4.add(new JButton("3"));
        jPanel4.add(new JButton("3"));
        jPanel4.add(new JButton("3"));
        jPanel4.add(new JButton("3"));

        contentPane.add(jPanel1);
        contentPane.add(jPanel2);
        contentPane.add(jPanel3);
        contentPane.add(jPanel4);

        this.setVisible(true);
        this.setBounds(200,200,400,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


    }

    public static void main(String[] args) {
        new JPanelDemo();

    }
}

在这里插入图片描述

JScroll
package com.GUI.lesson05;

import javax.swing.*;
import java.awt.*;

public class JScrollDemo extends JFrame {
    public JScrollDemo(){
        Container container = this.getContentPane();

        //文本域
        JTextArea textArea = new JTextArea(20, 50);
        textArea.setText("学习");
        //JScroll面板
        JScrollPane jScrollPane = new JScrollPane(textArea);
        container.add(jScrollPane);
        
        this.setVisible(true);
        this.setBounds(100,100,200,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        new JScrollDemo();
    }
}

在这里插入图片描述

5. 按钮

  • 图片按钮
package com.GUI.lesson05;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class JButtonDemo extends JFrame {
    public JButtonDemo(){
        Container container = this.getContentPane();

        URL resource = JButtonDemo.class.getResource("OUT.jpg");//获得当前类的同级资源【图片名不能为数字】
        Icon icon = new ImageIcon(resource);
        //把这个图标放到按钮上
        JButton button = new JButton("1");
        button.setIcon(icon);
        button.setToolTipText("图片按钮");

        container.add(button);

        this.setVisible(true);
        this.setBounds(100,200,500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        new JButtonDemo();
    }
}

在这里插入图片描述

  • 单选框按钮
package com.GUI.lesson05;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class JButtonDemo01 extends JFrame{
    public JButtonDemo01(){
        Container container = this.getContentPane();
        //将一个图片变为图标
        URL resource = JButtonDemo.class.getResource("OUT.jpg");//获得当前类的同级资源
        Icon icon = new ImageIcon(resource);
        //单选框
        JRadioButton radioButton01 = new JRadioButton("JRadioButton01");
        JRadioButton radioButton02 = new JRadioButton("JRadioButton02");
        JRadioButton radioButton03 = new JRadioButton("JRadioButton03");
        //由于单选框只能选一个,分组,一个组只能选一个
        ButtonGroup group = new ButtonGroup();
        group.add(radioButton01);
        group.add(radioButton02);
        group.add(radioButton03);

        container.add(radioButton01,BorderLayout.CENTER);
        container.add(radioButton02,BorderLayout.NORTH);
        container.add(radioButton03,BorderLayout.SOUTH);

        this.setVisible(true);
        this.setBounds(100,200,500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JButtonDemo01();
    }
}

在这里插入图片描述

  • 复选框按钮
package com.GUI.lesson05;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class JButtonDemo02 extends JFrame {
    public JButtonDemo02(){
        Container container = this.getContentPane();
        //将一个图片变为图标
        URL resource = JButtonDemo.class.getResource("OUT.jpg");//获得当前类的同级资源
        Icon icon = new ImageIcon(resource);

        //多选框
        JCheckBox checkBox1 = new JCheckBox("checkBox1");
        JCheckBox checkBox2 = new JCheckBox("checkBox2");
        JCheckBox checkBox3 = new JCheckBox("checkBox3");

        container.add(checkBox1,BorderLayout.NORTH);
        container.add(checkBox2,BorderLayout.CENTER);
        container.add(checkBox3,BorderLayout.SOUTH);

        this.setVisible(true);
        this.setBounds(100,200,500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        new JButtonDemo02();
    }
}

在这里插入图片描述

6. 列表

  • 下拉框
package com.GUI.lesson06;

import javax.swing.*;
import java.awt.*;

public class TestConboboxDemo01 extends JFrame {
    public TestConboboxDemo01() {
        Container container = this.getContentPane();

        JComboBox<Object> comboBox = new JComboBox<>();
        comboBox.addItem(null);
        comboBox.addItem("正在热映");
        comboBox.addItem("已下架");
        comboBox.addItem("即将上映");

        container.add(comboBox);

        this.setVisible(true);
        this.setBounds(400,400,500,80);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


    }

    public static void main(String[] args) {
        new TestConboboxDemo01();
    }
}

在这里插入图片描述

  • 列表框
package com.GUI.lesson06;

import javax.swing.*;
import java.awt.*;

public class TestConboboxDemo02 extends JFrame {
    public TestConboboxDemo02() {
        Container container = this.getContentPane();


        String[] contents = {"1","2","3"};//生成列表的内容
        JList<String> jList = new JList<>(contents);//列表中需要放入的内容

        container.add(jList);

        this.setVisible(true);
        this.setBounds(400,400,500,180);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestConboboxDemo02();
    }
}

在这里插入图片描述

  • 改进的列表框

Vector 类实现了一个动态数组。和 ArrayList 很相似,但是两者是不同的:

  • Vector 是同步访问的。
  • Vector 包含了许多传统的方法,这些方法不属于集合框架。

Vector 主要用在事先不知道数组的大小,或者只是需要一个可以改变大小的数组的情况。

package com.GUI.lesson06;

import javax.swing.*;
import java.awt.*;
import java.util.Vector;

public class TestConboboxDemo02 extends JFrame {
    public TestConboboxDemo02() {
        Container container = this.getContentPane();

        Vector<Object> vector = new Vector<>();
        JList<Object> jList = new JList<>(vector);

        vector.add("皮卡丘");
        vector.add("小智");

        container.add(jList);

        this.setVisible(true);
        this.setBounds(400,400,500,180);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestConboboxDemo02();
    }
}

在这里插入图片描述

7. 文本框

1. 文本框
package com.GUI.lesson06;

import javax.swing.*;
import java.awt.*;

public class TestTextDemo01 extends JFrame {
    public TestTextDemo01() {
        Container container = this.getContentPane();

        JTextField textField1 = new JTextField("hello",20);
        JTextField textField2 = new JTextField("world",20);

        container.add(textField1,BorderLayout.SOUTH);
        container.add(textField2,BorderLayout.NORTH);

        this.setVisible(true);
        this.setBounds(400,400,500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestTextDemo01();
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5v6jKrmK-1685434401352)(C:\Users\shinelon\AppData\Roaming\Typora\typora-user-images\image-20230530160005358.png)]

2. 密码框
package com.GUI.lesson06;

import javax.swing.*;
import java.awt.*;

public class TestTextDemo02 extends JFrame {
    public TestTextDemo02() {
        Container container = this.getContentPane();

        JPasswordField passwordField = new JPasswordField("输入密码");
        passwordField.setEchoChar('*');

        container.add(passwordField);

        this.setVisible(true);
        this.setBounds(400,400,500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestTextDemo02();
    }
}

在这里插入图片描述

3. 文本域
package com.GUI.lesson05;

import javax.swing.*;
import java.awt.*;

public class JScrollDemo extends JFrame {
    public JScrollDemo(){
        Container container = this.getContentPane();

        //文本域
        JTextArea textArea = new JTextArea(20, 50);
        textArea.setText("学习");

        //JScroll面板
        JScrollPane jScrollPane = new JScrollPane(textArea);


        container.add(jScrollPane);
        this.setVisible(true);
        this.setBounds(100,100,200,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        new JScrollDemo();
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值