ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【GUI】Swing包(窗口、弹窗、标签、面板、按钮、列表、文本框)

2021-10-25 16:57:59  阅读:159  来源: 互联网

标签:容器 container GUI 按钮 文本框 add Swing new public


继续我的上一小节,学习另外一个Java包的使用。 Swing是在原有的AWT的基础上进行了补充和改进。
上节:【GUI】GUI编程;AWT包(界面属性,布局管理,事件监听)
学习文章:Java-Swing编程介绍

文章目录

3 Swing包

Swing是一个为Java设计的GUI工具包。
Swing包括了图形用户界面(GUI)器件如:文本框,按钮,分隔窗格和表。
Swing提供许多比AWT更好的屏幕显示元素。轻量级组件的缺点则是执行速度较慢,优点就是可以在所有平台上。

Swing gui包含了两种元素:组件和容器。

  • 组件(控件):组件是单独的控制元素,例如按键或者文本编辑框。组件要放到容器中才能显示出来。
    继承于JComponent类。常见的组件有标签JLabel、按键JButton、输入框JTextField、复选框JCheckBox、列表JList。
  • 容器:容器是一种可以包含组件的特殊组件。由于容器也是组件,因此容器也可放到别的容器中。故组件和容器构成了包含层级关系。
    Swing中有两大类容器。
    (1)重量级容器/顶层容器(top-level container),它们不是继承于JComponent。它们包括JFrame,JApplet,JWindow,JDialog。它们的最大特点是不能被别的容器包含,只能作为界面程序的最顶层容器来包含其它组件
    (2)轻量级容器/中间层容器,它们继承于JComponent,包括JPanel,JScrollPane等。中间层容器用来将若干个相关联的组件放在一起。由于中间层容器继承于JComponent,因此它们本身也是组件,它们必须包含在其它的容器中

3.1 JFrame窗口

  1. JFrame窗口与AWT包的Frame不同,也需要获得一个容器来实现堆窗口颜色等设定。
  2. 默认实现窗口推出监听。
  3. 方法:
    Container container = this.getContentPane();//获得一个容器
    jLabel.setHorizontalAlignment(SwingConstants.CENTER);//设置标签居中
package study.gui.swing;

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

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

//窗口
class MyJFrame extends JFrame {
    public void init(){//一个好的编程习惯,定义一个初始化方法
        this.setBounds(100,100,400,300);
        this.setVisible(true);
//        this.setBackground(Color.black);//不添加容器无法设置颜色
//        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//默认实现窗口推出监听

        JLabel jLabel = new JLabel("欢迎使用JFrame!");
        this.add(jLabel);
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);//设置标签居中

        //获得一个容器
        Container container = this.getContentPane();
        container.setBackground(Color.YELLOW);//设置容器颜色
    }
}

在这里插入图片描述

3.2 JDialog弹窗

  1. 用事件去绑定弹窗,例如:按一个按钮触发事件。
  2. 弹窗需要借助容器。
  3. 不需要写弹窗推出。
package study.gui.swing;

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

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

//窗口
class MyJFrame2 extends JFrame{
    public void init(){
        this.setTitle("窗口");
        this.setVisible(true);
        this.setBounds(10,10,400,300);

        //设置容器  存放东西
        Container container = this.getContentPane();
        container.setBackground(Color.PINK);
        //绝对布局
        container.setLayout(null);

        //创建一个弹窗按钮
        JButton jButton = new JButton("进入");
        jButton.setBounds(30,30,100,50);
        container.add(jButton);

        //为按钮绑定监听——弹窗
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyDialogDemo();
            }
        });
    }
}

//弹窗
class MyDialogDemo extends JDialog{
    public MyDialogDemo() {
        this.setTitle("弹窗");
        this.setVisible(true);
        this.setBounds(100,100,200,200);

        Container container = this.getContentPane();
        container.setBackground(Color.GREEN);
        container.add(new JLabel("Helo"));
    }
}

在这里插入图片描述

3.3 标签

3.3.1 JLabel标签

JLabel jLabel = new JLabel("xxx");
jLabel.setHorizontalAlignment(SwingConstants.CENTER);//设置标签居中

3.3.2 Icon图标

  1. Icon是一个接口需要实现类 ,JFrame来继承。
  2. 方法:
    (1)实现类 iconDemo = new 实现类(宽,高);//图标
    (2)JLabel label = new JLabel("point",iconDemo,SwingConstants.CENTER);//把图标放在标签上 也可以放在按钮上 格式:显示文字+图标名称+位置
package study.gui.swing;

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

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

//窗口
class MyIconFrame extends JFrame implements Icon{
    int width;
    int height;

    //构造
    public MyIconFrame() throws HeadlessException {
    }
    public MyIconFrame(int width, int height) {
        this.width = width;
        this.height = height;
    }

    public void init(){
        MyIconFrame iconDemo = new MyIconFrame(20,20);//图标
        //把图标放在标签上 也可以放在按钮上   格式:显示文字+图标名称+位置
        JLabel label = new JLabel("point",iconDemo,SwingConstants.CENTER);

        Container container = this.getContentPane();
        container.add(label);

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

    //必须实现的Icon接口方法
    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.drawOval(x,y,width,height);//画个空心圆
    }

    @Override
    public int getIconWidth() {
        return this.width;
    }

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

在这里插入图片描述

3.3.3 图片标签

  1. 不需要继承Icon接口
  2. 方法:
    (1)ImageIcon imageIcon = new ImageIcon("图片地址");//构造图片标签
    (2)label.setIcon(imageIcon);//把图标放在标签上
package study.gui.swing;

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

public class ImageText extends JFrame {
    public ImageText() {
        JLabel label = new JLabel("这是一个图片标签");
        //获取图片的地址
        ImageIcon imageIcon = new ImageIcon("G:\\Java\\JavaSE\\JavaText\\src\\study\\gui\\swing\\1.jpg");

        label.setIcon(imageIcon);
        label.setHorizontalAlignment(SwingConstants.CENTER);

        Container container = this.getContentPane();
        container.add(label);

        setVisible(true);
        setBounds(100,100,700,600);
    }

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

在这里插入图片描述

3.4 JPanel与JScrollPane面板

  1. 两种面板形式:
    (1)普通面板:JPanel panel = new JPanel(new GridLayout(1,3));
    (2)可以滚动的面板:JScrollPane scrollPane = new JScrollPane(textArea);
package study.gui.swing;

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

public class JPanelDemo  extends JFrame {
    public JPanelDemo() {
        Container container = this.getContentPane();
        container.setLayout(new GridLayout(2,1,10,10));//后面的参数表示面板之间的间距

        //面板
        JPanel panel = new JPanel(new GridLayout(1,3));

        panel.add(new JButton("1"));
        panel.add(new JButton("2"));
        panel.add(new JButton("3"));

        container.add(panel);

        //可以滚动的面板
        //文本域
        JTextArea textArea = new JTextArea(30,50);//参数表示行列
        textArea.setText("If you set your goals ridiculously high and it's a failure, you will fail above everyone else's success.");
        //Scroll面板
        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);

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

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

在这里插入图片描述

3.6 按钮

  1. 三种按钮形式:
    (1)普通按钮new JButton():可以添加图片等。
    (2)单选按钮new JRadioButton();:放在一个组里面只能选一个。
    (3)复选按钮new JCheckBox();:放在一个盒子里面可以选多个。
package study.gui.swing;

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

public class ButtonText extends JFrame {
    public ButtonText() {
        Container container = this.getContentPane();
        container.setLayout(new GridLayout(1,3,10,10));//后面的参数表示面板之间的间距

        //面板1:显示图片按钮
        JPanel panel1 = new JPanel(new GridLayout(1,1));

        //把图片添加到按钮
        Icon imageIcon = new ImageIcon("G:\\Java\\JavaSE\\JavaText\\src\\study\\gui\\swing\\1.jpg");
        JButton button1 = new JButton();
        button1.setIcon(imageIcon);
        button1.setToolTipText("图片按钮");//悬浮提示

        panel1.add(button1);
        container.add(panel1);

        //面板2:显示单选按钮
        JPanel panel2 = new JPanel(new GridLayout(3,1));

        //单选框
        JRadioButton radioButton1 = new JRadioButton("GUCCI");
        JRadioButton radioButton2 = new JRadioButton("LV");
        JRadioButton radioButton3 = new JRadioButton("CHANEL");

        //单选框进行分组才能在选择时是单项选择:分组之后,一组之中只能选一个
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(radioButton1);
        buttonGroup.add(radioButton2);
        buttonGroup.add(radioButton3);

        //添加
        panel2.add(radioButton1,BorderLayout.CENTER);
        panel2.add(radioButton2,BorderLayout.SOUTH);
        panel2.add(radioButton3,BorderLayout.NORTH);
        container.add(panel2);

        //面板3:显示复选按钮
        JPanel panel3 = new JPanel(new GridLayout(3,1));
        //多选框
        JCheckBox checkBox1 = new JCheckBox("Java");
        JCheckBox checkBox2 = new JCheckBox("Python");
        JCheckBox checkBox3 = new JCheckBox("C");

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

        this.setVisible(true);
        this.setTitle("按钮");
        this.setBounds(100,100,500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

在这里插入图片描述

3.7 列表

3.8 文本框

标签:容器,container,GUI,按钮,文本框,add,Swing,new,public
来源: https://blog.csdn.net/weixin_43931465/article/details/120951207

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有