Swing

Swing

  • Swing 是一个为Java设计的GUI工具包。

  • Swing是JAVA基础类的一部分。

  • Swing包括了图形用户界面(GUI)器件如:文本框,按钮,分隔窗格和表。

  • Swing提供许多比AWT更好的屏幕显示元素。它们用纯Java写成,所以同Java本身一样可以跨平台运行,这一点不像AWT。它们是JFC的一部分。它们支持可更换的面板和主题(各种操作系统默认的特有主题),然而不是真的使用原生平台提供的设备,而是仅仅在表面上模仿它们。这意味着你可以在任意平台上使用JAVA支持的任意面板。轻量级组件的缺点则是执行速度较慢,优点就是可以在所有平台上采用统一的行为。

界面

package com.ar.chapter03;
import javax.swing.*;


import java.awt.*;

public class JFrameDemo02 {
    public static void main(String[] args) {
        new MyJFrame02().init();
    }
}
class MyJFrame02 extends JFrame{
    public void init(){
        this.setBounds(100,100,400,200);
        this.setVisible(true);
        JLabel jLabel=new JLabel("阿娆");
        this.add(jLabel);
        //文本居中
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
       Container container= this.getContentPane();
       container.setBackground(Color.red);
    }
}
package com.ar.chapter03;

import javax.swing.*;
import java.awt.*;
public class JFrameDome {
    //初始化
    public void init(){
        JFrame jFrame=new JFrame("Swing窗口");
        jFrame.setVisible(true);
        jFrame.setBounds(100,100,400,200);
        //设置文字
        JLabel jLabel=new JLabel("ar");

        jFrame.add(jLabel);
        //容器实例化
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JFrameDome().init();
    }
}

弹窗

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

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

public class DialongDemo extends JFrame {
    //
    public DialongDemo(){
        this.setVisible(true);
        this.setSize(700,1500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //放入新窗口
        Container container = this.getContentPane();
        //绝对布局
        container.setLayout(null);
        //按钮
        JButton button = new JButton("弹出窗口");
        button.setBounds(30,30,200,50);
        //监听事件,点击是弹出一个弹窗
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹窗
                new MyDialogDemo();
            }
        });
        container.add(button);
     

    }
    public static void main(String[] args) {
        new DialongDemo();
    }
}
//弹窗的窗口
class MyDialogDemo extends JDialog {
    public MyDialogDemo(){
        this.setVisible(true);
        this.setSize(100,100);
        this.setBounds(100,100,500,500);
      //  this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container container = new Container();
        container.setLayout(null);
        container.add(new Label("阿娆"));

    }
}

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

点击按钮,弹出弹窗

标签

label

new label("阿娆")

图标Icon

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("icontest", iconDemo, SwingConstants.CENTER);

        Container container = getContentPane();
        container.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 width;
    }

    @Override
    public int getIconHeight() {
        return height;
    }

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

}

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

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

public class IconDemo0 extends JFrame {
    public IconDemo0() {
        //获取图片地址
        JLabel label = new JLabel("ImageIcon");
        URL url = IconDemo0.class.getResource("某个图片的位置");
        ImageIcon imageIcon = new ImageIcon(url);
        label.setIcon(imageIcon);
        label.setHorizontalAlignment(SwingConstants.CENTER);

        Container container = getContentPane();
        container.add(label);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

面板

JPanel

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

public class Panel extends JFrame
{

            public Panel() {
            Container container = this.getContentPane();
            container.setLayout(new GridLayout(2, 1, 10, 10)); //后面的参数是间距

            JPanel panel1 = new JPanel(new GridLayout(1, 3));
            JPanel panel2 = new JPanel(new GridLayout(1, 2));
            JPanel panel3 = new JPanel(new GridLayout(2, 1));
            JPanel panel4 = new JPanel(new GridLayout(3, 2));

            panel1.add(new JButton("1"));
            panel1.add(new JButton("1"));
            panel1.add(new JButton("1"));
            panel2.add(new JButton("1"));
            panel2.add(new JButton("1"));
            panel3.add(new JButton("1"));
            panel3.add(new JButton("1"));
            panel4.add(new JButton("1"));
            panel4.add(new JButton("1"));
            panel4.add(new JButton("1"));
            panel4.add(new JButton("1"));
            panel4.add(new JButton("1"));
            panel4.add(new JButton("1"));

            container.add(panel1);
            container.add(panel2);
            container.add(panel3);
            container.add(panel4);

            this.setVisible(true);
            this.setSize(200, 200);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }

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

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

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

public class Panel02 extends JFrame{
    public Panel02() {
        Container container = this.getContentPane();
        JTextArea textArea = new JTextArea(20, 50);
        textArea.setText("JTextArea");

        JScrollPane scrollPane = new JScrollPane(textArea);

        container.add(scrollPane);


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

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

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

按钮

图片按钮
   public class JButtonDemo01 extends JFrame {
    public JButtonDemo01() {
        Container container = this.getContentPane();
        URL url = JButtonDemo01.class.getResource("tx.jpg");
        Icon icon = new ImageIcon(url);
   //把图标放到按钮上
    JButton button = new JButton();
    button.setIcon(icon);
    button.setToolTipText("图片按钮");

    container.add(button);

    this.setVisible(true);
    this.setSize(500, 500);
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
单选按钮
import javax.swing.*;
import java.awt.*;

public class JButtonDemo02 extends JFrame {
    public JButtonDemo02() {
        Container container = this.getContentPane();
        //单选框
        JRadioButton jRadioButton1 = new JRadioButton("01");
        JRadioButton jRadioButton2 = new JRadioButton("02");
        JRadioButton jRadioButton3 = new JRadioButton("03");
        //由于单选框只能选择一个,分组一个组中只能
        // 刚掉选一个可以多选啦
//        ButtonGroup buttonGroup = new ButtonGroup();
//        buttonGroup.add(jRadioButton1);
//        buttonGroup.add(jRadioButton2);
//        buttonGroup.add(jRadioButton3);
        container.add(jRadioButton1, BorderLayout.CENTER);
        container.add(jRadioButton2, BorderLayout.NORTH);
        container.add(jRadioButton3, BorderLayout.SOUTH);
        this.setVisible(true);
        this.setSize(500, 500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JButtonDemo02();
    }
}
多线按钮
public class JButtonDemo02 extends JFrame {
    public JButtonDemo02() {
        Container container = this.getContentPane();
        Checkbox checkbox1 = new Checkbox("01");//单词
        Checkbox checkbox2 = new Checkbox("02");
        container.add(checkbox1, BorderLayout.NORTH);
        container.add(checkbox2, BorderLayout.SOUTH);
        this.setVisible(true);
        this.setSize(500, 500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JButtonDemo02();
    }
}

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

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

列表

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

class TestComboboxDemo01 extends JFrame {
    public TestComboboxDemo01() {
        Container container = this.getContentPane();
        JComboBox status = new JComboBox();
        status.addItem(null);
        status.addItem("阿娆就很漂亮");
        status.addItem("阿娆太漂亮了");
        pack();
        container.add(status);
        this.setVisible(true);
        this.setSize(500, 500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

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

列表框

public class TestComboboxDemo02 extends JFrame {
    public TestComboboxDemo02() {

        Container container = this.getContentPane();

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

        this.setVisible(true);
        this.setSize(500, 500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestComboboxDemo02();
    }
}
  • 应用场景
    • 选自地区,或一些单个选项
    • 列表,展示信息,一般是动态扩容

文本框

[找到一个偷懒神器就这一次]((1条消息) Java之GUI 文本框 密码框 文本域_sa_hao的博客-CSDN博客)

);
this.setSize(500, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

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

}


- 应用场景
  - 选自地区,或一些单个选项
  - 列表,展示信息,一般是动态扩容

### 文本框

[找到一个偷懒神器就这一次]([(1条消息) Java之GUI 文本框 密码框 文本域_sa_hao的博客-CSDN博客](https://blog.csdn.net/sa_hao/article/details/106189726?spm=1001.2014.3001.5501))











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值