Swing

JFrame是指一个计算机语言-java的GUI程序的基本思路是以JFrame为基础,它是屏幕上window的对象,能够最大化、最小化、关闭。

基本板块

Swing 的三个基本构造块:标签、按钮和文本字段;但是需要个地方安放它们,并希望用户知道如何处理它们。JFrame 类就是解决这个问题的——它是一个容器,允许程序员把其他组件添加到它里面,把它们组织起来,并把它们呈现给用户。 JFrame 实际上不仅仅让程序员把组件放入其中并呈现给用户。比起它表面上的简单性,它实际上是 Swing 包中最复杂的组件。为了最大程度地简化组件,在独立于操作系统的 Swing 组件与实际运行这些组件的操作系统之间,JFrame 起着桥梁的作用。JFrame 在本机操作系统中是以窗口的形式注册的,这么做之后,就可以得到许多熟悉的操作系统窗口的特性:最小化/最大化、改变大小、移动。

1.1、窗口、面板

案例演示:

package swingdemo;

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

public class JFrameDemo {
    public static void main(String[] args) {
        //创建一个窗口
        new JFrameDemo().init();
    }
    public void init() {
        //*窗口
        JFrame jFrame = new JFrame();
        jFrame.setVisible(true);
        jFrame.setBounds(100, 100, 100, 100);
        //设置文字JLabel
        JLabel jLabel = new JLabel("欢迎你来到我的博客");
        jFrame.add(jLabel);
        //关闭时间
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

效果展示:
Swing

案例演示:标签居中

package swingdemo;

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

public class JFrameDemo {
    public static void main(String[] args) {
        new JFrameDemo().init();
    }
    public void init() {
        JFrame jFrame = new JFrame();
        jFrame.setVisible(true);
        jFrame.setBounds(100, 100, 100, 100);
        //标签居中
        JLabel jLabel = new JLabel("欢迎你来到我的博客",SwingConstants.CENTER);
        jFrame.add(jLabel);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

效果展示:
Swing

1.2、弹窗

JDialog,用来被弹出,默认就有关闭事件

案例演示:

package com.kuang.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.setSize(700,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //JFrame 放东西,容器
        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 DialogDemo();
    }
}

//弹窗的窗口
class MyDialogDemo extends JDialog{
    public MyDialogDemo() {
        this.setVisible(true);
        this.setBounds(100,100,500,500);

        Container container = this.getContentPane();
        container.setLayout(null);

        container.add(new Label("欢迎来到我的博客"));
    }
}

效果展示:
Swing

1.3、标签

构造方法:

JLabel jLabel = new JLabel("xxx");

案例演示:图标ICON

package swingdemo;

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

public class IconDemo extends JFrame implements Icon {
    //主方法
    public static void main(String[] args) {
        new IconDemo().unit();
    }
    
    private int width;
    private  int height;
    //有参无参构造
    public IconDemo(){

    }
    public IconDemo(int width,int height){
        this.width=width;
        this.height=height;
    }

    public void unit(){
        IconDemo iconDemo = new IconDemo(15,15);
        //图标放在标签,也可以放在按钮上
        JLabel label = new JLabel("欢迎来到我的博客", 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) {

    }

    @Override
    public int getIconWidth() {
        return 0;
    }

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

效果展示:
Swing

案例演示:图片Icon

package swingdemo;

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

public class ImageIconDemo extends JFrame {
    public static void main(String[] args) {
          new ImageIconDemo();
    }
    public ImageIconDemo(){
        JLabel label = new JLabel("ImageIcon");
          //获取图片的地址 
        URL url = ImageIconDemo.class.getResource("food.png");//命名不要冲突了
        //封装
        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);
        setBounds(100,100,200,200);
    }
}

效果展示:
Swing

1.4、面板

案例演示:JPanel

package swingdemo;

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

public class JPanelDemo extends JFrame {
    public static void main(String[] args) {
        new JPanelDemo();
    }
    public JPanelDemo(){
        Container container= this.getContentPane();
        container.setLayout(new GridLayout(2,5,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));
        for (int i = 0; i < 3; i++) {
            jPanel1.add(new Button("1"));
        }
        jPanel2.add(new Button("2"));
        jPanel2.add(new Button("2"));
        jPanel3.add(new Button("3"));
        jPanel3.add(new Button("3"));
        for (int i = 0; i < 6; i++) {
            jPanel4.add(new Button("4"));
        }

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

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

    }
}

效果展示:
Swing

案例演示:JScrollPanel(带有滚动条的面板)

package swingdemo;

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

public class JScrollDemo extends JFrame {
    public static void main(String[] args) {
       new JScrollDemo();
    }
    public JScrollDemo(){
        Container container = getContentPane();
        JTextArea textArea = new JTextArea(20, 50);
        textArea.setText("欢迎来到我的博客");
        //设置字体
        textArea.setFont(new Font("微软雅黑",40,40));
        //JScroll面板 把文本域加进去
        JScrollPane scrollPane = new JScrollPane(textArea);
        //再把JScroll面板加入到容器中
        container.add(scrollPane);
        setVisible(true);
        setBounds(100,100,300,350);
        //关闭事件
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

效果展示:

Swing

1.5、按钮

案例演示:把一个图片变为按钮

package swingdemo;

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

public class JButtonDemo01 extends JFrame {
    public static void main(String[] args) {
       new JButtonDemo01();
    }
    public JButtonDemo01(){
        Container container = getContentPane();
        //将一个图片变为一个图标
        URL url = JButtonDemo01.class.getResource("food.png");
        ImageIcon icon = new ImageIcon(url);
        //把图片放在按钮上
        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("图片按钮");
        //把按钮添加到容器
        container.add(button);
        this.setVisible(true);
        this.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }
}

效果展示:
Swing

案例演示:单选按钮(JRadioButton)

package swingdemo;

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

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

    public JButtonDemo02() {
        Container container = this.getContentPane();
        //将一个图片变为图标
        URL resource = JButtonDemo01.class.getResource("food.png");
        Icon icon = new ImageIcon(resource);

        //单选框
        JRadioButton radioButton1 = new JRadioButton("JRadioButton01");
        JRadioButton radioButton2 = new JRadioButton("JRadioButton02");
        JRadioButton radioButton3 = new JRadioButton("JRadioButton03");
        //由于单选框只能选择一个,分组,一个组中只能选择一个
        ButtonGroup group = new ButtonGroup();
        group.add(radioButton1);
        group.add(radioButton2);
        group.add(radioButton3);

        container.add(radioButton1, BorderLayout.NORTH);
        container.add(radioButton2, BorderLayout.CENTER);
        container.add(radioButton3, BorderLayout.SOUTH);

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

效果展示:
Swing

案例演示:复选按钮( JCheckBox)

package swingdemo;

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

public class JButtonDemo03 extends JFrame {
    public static void main(String[] args) {
        new JButtonDemo03();
    }
    public JButtonDemo03() {
        Container container = this.getContentPane();
        //将一个图片变为图标
        URL resource = JButtonDemo01.class.getResource("food.png");
        Icon icon = new ImageIcon(resource);

        //多选框
        JCheckBox checkBox01 = new JCheckBox("checkBox01");
        JCheckBox checkBox02 = new JCheckBox("checkBox02");
        
        container.add(checkBox01,BorderLayout.NORTH);
        container.add(checkBox02,BorderLayout.SOUTH);

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

效果展示:
Swing

3.6、列表

案例演示:下拉框

package swingdemo;

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

public class TestComboboxDemo02 extends JFrame {
    public static void main(String[] args) {
        new TestComboboxDemo02();
    }
    public TestComboboxDemo02() {
        Container container = getContentPane();
        //创建一个可变大小的数组序列容器
        Vector contents = new Vector();
        //列表中需要放入内容
        JList jList = new JList(contents);
        contents.add("张三");
        contents.add("李四");
        contents.add("赵六");

        container.add(jList);
        setVisible(true);
        setSize(500,350);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

效果展示:
Swing

应用场景:

  1. 选择地区或者一些单个选项
  2. 列表展示信息,一般是动态扩容

1.7、文本框

案例演示:文本框(JTextField)

package swingdemo;

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

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

        Container container = this.getContentPane();
        //创建文本框
        JTextField textField = new JTextField("hello");
        JTextField textField2 = new JTextField("world",20);

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

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

效果展示:
Swing

案例演示:密码框(JPasswordField)

package swingdemo;

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

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

        Container container = this.getContentPane();

        
         //密码框构造方法
        JPasswordField passwordField = new JPasswordField(); //****
        //替换的字符
        passwordField.setEchoChar('*');

        container.add(passwordField);

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

效果展示:
Swing

案例演示:文本域( JScrollPane)

package swingdemo;

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

public class JScrollDemo01 extends JFrame {
    public static void main(String[] args) {
        new JScrollDemo01();
    }
    public JScrollDemo01(){
        Container container = this.getContentPane();

        //文本域
        JTextArea textArea = new JTextArea(20, 50);
        textArea.setText("欢迎来到我的博客");

        //Scroll面板  构造方法
        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);

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

效果展示:
Swing

Swing

上一篇:windows查看端口占用命令


下一篇:【译】gRPC vs HTTP APIs