【JAVA】<GUI编程>AWT & Swing 图形化编程库

目录

一、GUI编程概述:

二、AWT:

1.AWT简介:

2.组件和容器:

2.1 窗体frame:

2.2 面板Panel:

3.布局管理器:

3.1 流式布局:

3.2 东西南北中:

3.3 网格布局:Grid

4.事件监听:

5.输入框:TextField监听

画笔:

鼠标监听:

窗口监听:

键盘监听:

三、Swing:

Swing简介:

Swing 类库结构

Swing 容器

JFrame窗体:

JDialog弹窗:

Icon、ImageIcon标签:

JPanel面板:

JPanel面板:

JScroll面板:

按钮:

图片按钮:

单选按钮:

多选按钮:

列表:

下拉框:

列表框:

文本框:


一、GUI编程概述:

        GUI就是图形用户界面,是基于图形的界面,windows就是一个图形用户界面的操作系统,而DOS是基于命令提示符的操作系统,GUI编程就是编出一个图形用户界面的软件.

        JAVA GUI核心技术:Swing、AWT

Java Swing 介绍 | 菜鸟教程icon-default.png?t=LBL2https://www.runoob.com/w3cnote/java-swing-demo-intro.htmlAWT_JAVA_AWT教程 - AWT中文网™AWT中文网汇集了AWT入门学习实例教程,帮助你了解和深入学JAVA.AWT提供基础指导。icon-default.png?t=LBL2https://www.yiibai.com/html/awt


二、AWT:

1.AWT简介:

        AWT (Abstract Window Toolkit),中文译为抽象窗口工具包,该包提供了一套与本地图形界面进行交互的接口 ,是Java提供的用来建立和设置Java的图形用户界面的基本工具。 AWT中的图形函数与操作系统所提供的图形函数之间有着一一对应的关系,称之为 peers ,当利用AWT编写 图形用户界面 时,实际上是在利用本地操作系统所提供的图形库。

        AWT提供了JavaApplet和Java Application中可用的用户图形界面GUI中的基本组件(components)。由于Java是一种独立于平台的程序设计语言,但GUI却往往是依赖于特定平台的,Java采用了相应的技术使得AWT能提供给应用程序独立于机器平台的接口,这保证了同一程序的GUI在不同机器上运行具有类似的外观(不一定完全一致)。

        Abstract Window Toolkit(AWT)是一个图形过程库,使用Java语言通过位图显示来操纵窗口。最后设计者又将AWT扩充为Alternative Window工具箱和Applet Widget工具箱。最新的GUI接口称为Swing,扩展了AWT,程序开发人员可以利用Swing生成独立于平台的GUI对象。

2.组件和容器:

2.1 窗体frame:

创建一个窗口:

public class FirstWindow {
    public static void main(String[] args) {
        Frame frame = new Frame("FirstWindow-我的第一个窗口");

        //设置可见性:
        frame.setVisible(true);

        //设置窗口大小:
        frame.setSize(400,400);

        //设置窗口背景颜色:
        frame.setBackground(Color.GRAY);

        //设置窗口弹出位置:
        frame.setLocation(200,200);

        //设置窗口固定大小:
        frame.setResizable(false); //是否可以灵活改变窗口大小
    }
}

创建多个窗口:

public class MoreWindows {
    public static void main(String[] args) {

        //创建多个窗口:
        Windows windows01 = new Windows(100,100,300,300,new Color(10,134,130));
        Windows windows02 = new Windows(400,100,300,300,Color.BLUE);
    }
}

class Windows extends Frame{

    //窗口编号:
    static int id = 0;

    //构造窗口:(参数:弹出位置,窗口大小,窗口颜色)
    public Windows(int x, int y, int w, int h, Color color){
        super("MyWindows" + (++id));
        setBounds(x,y,w,h);
        setBackground(color);
        setVisible(true);
    }
}

2.2 面板Panel:

public class FirstPanel {
    public static void main(String[] args) {

        //1.创建一个弹窗:
        Frame frame = new Frame();
        frame.setTitle("panel面板");
        frame.setVisible(true);
        frame.setBounds(100,100,400,400);
        frame.setBackground(new Color(0, 165, 255));
        frame.setResizable(false);

        //设置布局:
        frame.setLayout(null);

        //2.创建一个面板:
        Panel panel = new Panel();
        panel.setBounds(10,50,300,300); //设置相对于frame窗口的位置
        panel.setBackground(Color.GRAY);

        //3.把面板加载到弹窗中:
        frame.add(panel);

        //监听事件,监听窗口关闭事件:System.exit(0)
        //适配器模式:
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                //点击关闭窗口按钮,结束程序:
                System.exit(0); //0表示正常结束程序
            }
        });
    }
}

3.布局管理器:

3.1 流式布局:

public class FirstFlowLayout {
    public static void main(String[] args) {

        //1.创建一个弹窗:
        Frame frame = new Frame();
        frame.setVisible(true);
        frame.setTitle("按钮布局");
        frame.setBounds(200,200,800,600);
        frame.setBackground(Color.GRAY);

        //2.设置布局(流式布局):
        //frame.setLayout(new FlowLayout());//默认居中
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));

        //3.设置组件——按钮:(注意:AWT对中文支持不友好,所以这里不使用中文,需要使用中文可以换成Swing中的JButton)
        Button button = new Button("button");
        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        JButton button3 = new JButton("中文按钮!!!"); //使用JButton

        //4.在弹窗上添加按钮:
        frame.add(button);
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);

        //6.给弹窗添加监听事件:
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

3.2 东西南北中:

public class FirstBorderLayout {
    public static void main(String[] args) {

        Frame frame = new Frame();
        frame.setVisible(true);
        frame.setTitle("东西南北中布局");
        frame.setBackground(Color.GRAY);
        frame.setBounds(100,100,600,600);

        Button eastButton = new Button("east");
        Button westButton = new Button("west");
        Button southButton = new Button("south");
        Button northButton = new Button("north");
        Button centerButton = new Button("center");

        frame.add(eastButton,BorderLayout.EAST);
        frame.add(westButton,BorderLayout.WEST);
        frame.add(southButton,BorderLayout.SOUTH);
        frame.add(northButton,BorderLayout.NORTH);
        frame.add(centerButton,BorderLayout.CENTER);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }
}

3.3 网格布局:Grid

public class FirstGridLayout {
    public static void main(String[] args) {

        Frame frame = new Frame();
        frame.setTitle("网格布局");
        frame.setVisible(true);
        frame.setBounds(100,100,700,700);
        frame.setBackground(Color.GRAY);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        Button button = new Button("button01");
        Button button01 = new Button("button01");
        Button button02 = new Button("button02");
        Button button03 = new Button("button03");
        Button button04 = new Button("button04");
        Button button05 = new Button("button05");

        frame.add(button);
        frame.add(button01);
        frame.add(button02);
        frame.add(button03);
        frame.add(button04);
        frame.add(button05);

        frame.setLayout(new GridLayout(2,3)); //参数位 n行 m列

        frame.pack(); //java自动调整方法
    }
}

4.事件监听:

事件监听:当某个事件触发时,同时进行相应指令的触发!

public class FirstListener {
    public static void main(String[] args) {

        Frame frame = new Frame();
        frame.setVisible(true);
        frame.setTitle("事件监听");
        frame.setBounds(100,100,300,300);
        frame.setBackground(Color.GRAY);

        Button button1 = new Button("button1");
        button1.setActionCommand("buttion1被点击了!"); //给按钮设置信息

        Button button2 = new Button("button2");
        button2.setActionCommand("buttion2被点击了!"); //给按钮设置信息

        MyListener myListener1 = new MyListener();
        MyListener myListener2 = new MyListener();

        //给按钮加上事件监听:
        button1.addActionListener(myListener1);
        button2.addActionListener(myListener2);

        frame.add(button1,BorderLayout.NORTH);
        frame.add(button2,BorderLayout.SOUTH);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

//创建一个类实现ActionListener
class MyListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("成功按下按钮!!!");

        //通过e对象获取button中定义的信息:
        System.out.println(e.getActionCommand());

    }
}

5.输入框:TextField监听

主程序只管启动!

public class FirstTextField {
    public static void main(String[] args) {
        //启动,程序入口:
        new MyFrame();
    }
}

class MyFrame extends Frame{
    //通过this指代当前类对象,可以省略:
    public MyFrame(){

        TextField textField = new TextField();

        //监听文本框:
        //在文本框中按下enter键就会触发事件
        MyActionListener myActionListener = new MyActionListener();
        textField.addActionListener(myActionListener);

        //设置文本框替换编码:
        textField.setEchoChar('*');

        this.add(textField);

        this.setVisible(true);
        this.setBounds(100,100,200,200);
        this.pack();

        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }
}

class MyActionListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        //获取文本框资源:
        TextField textField = (TextField) e.getSource();
        String text = textField.getText();//获取文本框中文本内容

        System.out.println(text);

        //设置文本框中内容:
        textField.setText("");
    }
}

画笔:

public class FirstPaint {
    public static void main(String[] args) {
        //程序启动:
        new MyPaint().LoadMyPaint();

    }
}

/**
 * 注意:当前类继承了Frame类,在类的内部可以直接设置frame弹框的属性值
 *       如果重新new一个Frame弹框实例,对这个实例进行设值后,当程序执行时,
 *       会实例化子类对象,又重新调用了Frame的构造器,又会创建了一个新的弹窗
 */
class MyPaint extends Frame{

    /**
     * 定义一个方法,加载画图弹窗
     */
    public void LoadMyPaint(){

//        Frame frame = new Frame();

        this.setVisible(true); //设置弹窗可见
        this.setBounds(100,100,600,500);
        this.setTitle("画笔工具");

        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    /**
     * 画笔
     * @param g
     */
    @Override
    public void paint(Graphics g) {
        //设置画笔颜色:
        g.setColor(Color.BLUE);

        //使用画笔;
        g.drawOval(100,100,200,200);
        g.fillRect(0,0,100,100);

        //习惯:画笔用完,还原到最初的颜色
    }
}

鼠标监听:

public class FirstMouseListener {
    public static void main(String[] args) {
        new MyFrameMouse("鼠标点击绘图");
    }
}

class MyFrameMouse extends Frame{
    //使用绘图,需要监听鼠标当前位置(坐标),需要使用集合存储这些点
    ArrayList points;

    public MyFrameMouse(String title){
        super(title); //给frame弹框加上标题
        this.setBounds(100,100,500,400);
        this.setVisible(true);

        //保存鼠标在frame弹窗的点击坐标位置:
        points = new ArrayList();

        //对当前弹框中的鼠标加上监听事件:
        this.addMouseListener(new MyMouserListener()); //添加上鼠标的监听事件

        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    @Override
    public void paint(Graphics g) {
        //绘图,监听鼠标事件:(使用迭代器进行绘图)
        Iterator iterator = points.iterator(); //使用了ArrayList中的迭代器获取元素
        while (iterator.hasNext()){
            Point point = (Point) iterator.next(); //通过迭代器获取到点对象

            g.setColor(Color.cyan);
            g.fillOval(point.x,point.y,10,10);
        }
    }

    //添加点到界面上:
    public void addPaint(Point point){
        points.add(point);
    }

    //使用适配器模式:(鼠标监听事件)
    private class MyMouserListener extends MouseAdapter{
        @Override
        public void mousePressed(MouseEvent e) {
            //获取鼠标点击的弹窗:
            MyFrameMouse myFrameMouse = (MyFrameMouse) e.getSource();

            //当鼠标在弹窗中进行点击时,就会在弹窗中创建一个点:
            myFrameMouse.addPaint(new Point(e.getX(),e.getY()));

            //每次点击鼠标,都需要重新绘图:
            myFrameMouse.repaint(); //刷新操作
        }
    }
}

窗口监听:

public class WindowListener {
    public static void main(String[] args) {
        new MyWindowListener("开始使用!");
    }
}

class MyWindowListener extends Frame{
    public MyWindowListener(String title){
        super(title);
        this.setVisible(true);
        this.setBounds(100,100,600,400);

        //窗口事件监听:
        this.addWindowListener(new WindowAdapter() {

            //窗口关闭事件:
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }

            //窗口激活事件:
            @Override
            public void windowActivated(WindowEvent e) {
                System.out.println("窗口被激活了!");
                setTitle("激活!正在使用中!!!");
            }

            //窗口失去激活事件:
            @Override
            public void windowDeactivated(WindowEvent e) {
                System.out.println("离开当前窗口!");
                setTitle("已经离开此窗口!");
            }
        });
    }
}

键盘监听:

package com.krian.GUI;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * @Author: Lunaticer
 * @Create: 2021-07-28 8:47
 * @Tip: Keeping the eyes of the prize !
 * @Description: 测试鼠标监听事件
 */
@SuppressWarnings({"all"})
public class FirstMouseListener {
    public static void main(String[] args) {
        new MyFrameMouse("鼠标点击绘图");
    }
}

class MyFrameMouse extends Frame{
    //使用绘图,需要监听鼠标当前位置(坐标),需要使用集合存储这些点
    ArrayList points;

    public MyFrameMouse(String title){
        super(title); //给frame弹框加上标题
        this.setBounds(100,100,500,400);
        this.setVisible(true);

        //保存鼠标在frame弹窗的点击坐标位置:
        points = new ArrayList();

        //对当前弹框中的鼠标加上监听事件:
        this.addMouseListener(new MyMouserListener()); //添加上鼠标的监听事件

        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    @Override
    public void paint(Graphics g) {
        //绘图,监听鼠标事件:(使用迭代器进行绘图)
        Iterator iterator = points.iterator(); //使用了ArrayList中的迭代器获取元素
        while (iterator.hasNext()){
            Point point = (Point) iterator.next(); //通过迭代器获取到点对象

            g.setColor(Color.cyan);
            g.fillOval(point.x,point.y,10,10);
        }
    }

    //添加点到界面上:
    public void addPaint(Point point){
        points.add(point);
    }

    //使用适配器模式:(鼠标监听事件)
    private class MyMouserListener extends MouseAdapter{
        @Override
        public void mousePressed(MouseEvent e) {
            //获取鼠标点击的弹窗:
            MyFrameMouse myFrameMouse = (MyFrameMouse) e.getSource();

            //当鼠标在弹窗中进行点击时,就会在弹窗中创建一个点:
            myFrameMouse.addPaint(new Point(e.getX(),e.getY()));

            //每次点击鼠标,都需要重新绘图:
            myFrameMouse.repaint(); //刷新操作
        }
    }
}

KeyEvent类中定义了键盘上所有对应的数值:(静态属性)


三、Swing:

Swing简介:

        Swing 是新一代的图形界面工具。使用 Swing 来开发图形界面比 AWT 更加优秀,因为 Swing 是一种轻量级组件,它采用纯 Java 实现,不再依赖于本地平台的图形界面,所以可以在所有平台上保持相同的运行效果,对跨平台支持比较出色。除此之外,Swing 提供了比 AWT 更多的图形界面组件,因此可以开发出美观的图形界面程序。

Swing 类库结构

        Swing 组件都采用 MVC(Model-View-Controller,即模型-视图-控制器)的设计,实现 GUI 组件的显示逻辑和数据逻辑的分离,从而允许程序员自定义 Render 来改变 GUI 组件的显示外观,以提供更多的灵活性。
        Swing 围绕 JComponent 组件构建JComponent 则由 AWT 的容器类扩展而来


图1 Swing类库组织结构图


        从图 1 可以看出,Swing 组件除了 AbstmctButton 类之外都以 J 开头。Swing 容器组件直接继承 AWT 类库中的容器组件类,其他大部分组件都是继承 JComponet 组件。组件可以划分为容器组件和非容器组件,容器组件包括 JFmme 和 JDialog。其中 JComponent 定义了非容器类的轻量级组件(JBntton、JPanel、JMenu 等)。

Swing 容器

        创建图形用户界面程序的第一步是创建一个容器类以容纳其他组件,常见的窗口就是一种容器。容器本身也是一种组件,它的作用就是用来组织、管理和显示其他组件。

Swing 中容器可以分为两类:顶层容器和中间容器。

        顶层容器是进行图形编程的基础,一切图形化的东西都必须包括在顶层容器中。顶层容器是任何图形界面程序都要涉及的主窗口,是显示并承载组件的容器组件。在 Swing 中有三种可以使用的顶层容器,分别是 JFrame、JDialog 和 JApplet。

  1. JFrame:用于框架窗口的类,此窗口带有边框、标题、关闭和最小化窗口的图标。带 GUI 的应用程序至少使用一个框架窗口。
  2. JDialog:用于对话框的类。
  1. JApplet:用于使用 Swing 组件的 Java Applet 类。


        中间容器是容器组件的一种,也可以承载其他组件,但中间容器不能独立显示,必须依附于其他的顶层容器。常见的中间容器有 JPanel、JScrollPane、JTabbedPane 和 JToolBar。

  • JPanel:表示一个普通面板,是最灵活、最常用的中间容器。
  • JScrollPane:与 JPanel 类似,但它可在大的组件或可扩展组件周围提供滚动条。
  • JTabbedPane:表示选项卡面板,可以包含多个组件,但一次只显示一个组件,用户可在组件之间方便地切换。
  • JToolBar:表示工具栏,按行或列排列一组组件(通常是按钮)。


        在 Java 程序中容器类都是继承自 Container 类。中间容器和顶层容器在,AWT 包和 Swing 包中继承 Container 类的继承关系:

JFrame窗体:

package com.krian.Swing;

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

/**
 * @Author: Lunaticer
 * @Create: 2021-07-28 10:15
 * @Tip: Keeping the eyes of the prize !
 * @Description: Swing弹窗
 */
@SuppressWarnings({"all"})
public class JFrameTest {
    public static void main(String[] args) {
        new MyJFrame("JFrame弹窗");
    }
}

@SuppressWarnings({"all"})
class MyJFrame extends JFrame{
    public MyJFrame(String title){
        super(title);
        this.setVisible(true);
        this.setBounds(100,100,600,500);

        this.setBackground(Color.red);

        /**
         * DO_NOTHING_ON_CLOSE
         * HIDE_ON_CLOSE
         * DISPOSE_ON_CLOSE
         * EXIT_ON_CLOSE)
         */
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        JLabel label = new JLabel("欢迎使用!",SwingConstants.CENTER); //在弹窗中设置文字,并居中显示
//        label.setHorizontalAlignment(SwingConstants.CENTER); //设置居中显示

        Container contentPane = this.getContentPane(); //当前容器
        contentPane.setBackground(Color.cyan); //设置当前容器背景颜色

        this.add(label);
    }
}

JDialog弹窗:

public class DialogTest extends JFrame{
    public DialogTest(String title){
        super(title);
        this.setVisible(true);
        this.setBounds(100,100,500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        Container contentPane = this.getContentPane();

        //取消默认布局,使用绝对布局:
        contentPane.setLayout(null);

        //创建一个按钮:
        JButton jButton = new JButton("点击我试试!!!");
        //设置按钮在容器中位置:
        jButton.setBounds(100,100,200,100);

        //对按钮设置事件监听:
        jButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //点击按钮出现弹窗:
                new MyDialog();
            }
        });

        contentPane.add(jButton);
    }

    public static void main(String[] args) {
        new DialogTest("点击弹窗");
    }
}

@SuppressWarnings({"all"})
class MyDialog extends JDialog{
    public MyDialog(){
        this.setVisible(true);
        this.setBounds(200,200,80,60);

        Container contentPane = this.getContentPane();

        JLabel jLabel = new JLabel("学习!!!");
        contentPane.add(jLabel);
    }
}

Icon、ImageIcon标签:

图标ImageIcon:

public class ImageIconDemo extends JFrame {

    public ImageIconDemo(String title){
        super(title);

        //获取图片的地址:
        JLabel label = new JLabel("ImageIcon");
        URL imgUrl = ImageIconDemo.class.getResource("OIP.jpg");

        //创建一个图像标签
        ImageIcon imageIcon = new ImageIcon(imgUrl);

        //在lable标签中添加上图片标签:
        label.setIcon(imageIcon);
        label.setHorizontalAlignment(SwingConstants.CENTER); //设置lable标签展示位置

        Container contentPane = getContentPane();//获取当前容器
        contentPane.add(label); //在容器中添加lable标签

        this.setVisible(true);
        this.setBounds(100,100,400,300);

        //设置关闭窗口事件:
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new ImageIconDemo("图片标签");
    }
}

JPanel面板:

JPanel面板:

public class JPanelTest extends JFrame {

    public JPanelTest(String title){
        super(title);
        //获取当前窗口容器:
        Container contentPane = this.getContentPane();

        //设置容器布局
        contentPane.setLayout(new GridLayout(2,1,10,10)); //最后两个参数设置行列间距

        //创建面板:
        JPanel jPanel = new JPanel(new GridLayout(1,3));
        JPanel jPanel1 = new JPanel(new GridLayout(1,2));
        JPanel jPanel2= new JPanel(new GridLayout(2,1));

        jPanel.add(new JButton("按钮01"));
        jPanel.add(new JButton("按钮01"));
        jPanel.add(new JButton("按钮01"));
        jPanel1.add(new JButton("按钮02"));
        jPanel1.add(new JButton("按钮02"));
        jPanel2.add(new JButton("按钮03"));
        jPanel2.add(new JButton("按钮03"));

        //向容器中添加面板:
        contentPane.add(jPanel);
        contentPane.add(jPanel1);
        contentPane.add(jPanel2);

        this.setVisible(true);
        this.setSize(600,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        this.pack();
    }

    public static void main(String[] args) {
        new JPanelTest("面板测试");
    }
}

JScroll面板:

public class JScrollTest extends JFrame {
    public JScrollTest(String title){
        super(title);

        //获取窗口容器:
        Container contentPane = this.getContentPane();

        //创建一个文本域:
        JTextArea jTextArea = new JTextArea(20,50);
        jTextArea.setText("这是一个文本域,krian正在学习JavaGUI编程!!!");

        //创建Scrol面板:
        JScrollPane scrollPane = new JScrollPane(jTextArea);
        contentPane.add(scrollPane);

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

    public static void main(String[] args) {
        new JScrollTest("JScroll");
    }
}

按钮:

图片按钮:

public class ImageIconTest extends JFrame { //继承JFrame类
    public ImageIconTest(String title){
        super(title);
        //获取当前窗口容器:
        Container contentPane = this.getContentPane();

        //从类路径获取图片资源:
        URL url = ImageIconDemo.class.getResource("img.png");
        ImageIcon imageIcon = new ImageIcon(url);

        //把上面创建的图标放入按钮:
        JButton jButton = new JButton();
        jButton.setIcon(imageIcon);
        jButton.setToolTipText("别点我!!!"); //设置悬浮提示文字

        //把按钮放入容器:
        contentPane.add(jButton);

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

    public static void main(String[] args) {
        new ImageIconTest("图片按钮");
    }
}

单选按钮:

public class SingleIconTest extends JFrame { //继承JFrame类
    public SingleIconTest(String title){
        super(title);
        //获取当前窗口容器:
        Container contentPane = this.getContentPane();

        //创建多个单选框:
        JRadioButton jRadioButton01 = new JRadioButton("JRadioButton01");
        JRadioButton jRadioButton02 = new JRadioButton("JRadioButton02");
        JRadioButton jRadioButton03 = new JRadioButton("JRadioButton03");

        //将所上面的单选框进行分组,一个组对应着只有一个选项:
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(jRadioButton01);
        buttonGroup.add(jRadioButton02);
        buttonGroup.add(jRadioButton03);

        //设置按钮在窗口位置:
        contentPane.add(jRadioButton01,BorderLayout.NORTH);
        contentPane.add(jRadioButton02,BorderLayout.CENTER);
        contentPane.add(jRadioButton03,BorderLayout.SOUTH);

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

    public static void main(String[] args) {
        new SingleIconTest("单选框按钮");
    }
}

多选按钮:

public class CheckBoxIconTest extends JFrame { //继承JFrame类
    public CheckBoxIconTest(String title){
        super(title);
        //获取当前窗口容器:
        Container contentPane = this.getContentPane();

        //创建复选按钮:
        JCheckBox jCheckBox01 = new JCheckBox("JCheckBox01");
        JCheckBox jCheckBox02 = new JCheckBox("JCheckBox02");
        JCheckBox jCheckBox03 = new JCheckBox("JCheckBox03");

        contentPane.add(jCheckBox01,BorderLayout.SOUTH);
        contentPane.add(jCheckBox02,BorderLayout.NORTH);
        contentPane.add(jCheckBox03,BorderLayout.CENTER);

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

    public static void main(String[] args) {
        new CheckBoxIconTest("多选框按钮");
    }
}

列表:

下拉框:

public class ComboBoxTest extends JFrame { //继承JFrame类
    public ComboBoxTest(String title){
        super(title);
        //获取当前窗口容器:
        Container contentPane = this.getContentPane();

        //创建一个下拉框:
        JComboBox<Object> comboBox = new JComboBox<>();

        comboBox.addItem("");
        comboBox.addItem("选项一");
        comboBox.addItem("选项二");
        comboBox.addItem("选项三");

        contentPane.add(comboBox);

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

    public static void main(String[] args) {
        new ComboBoxTest("下拉框");
    }
}

列表框:

public class JListTest extends JFrame { //继承JFrame类
    public JListTest(String title){
        super(title);
        //获取当前窗口容器:
        Container contentPane = this.getContentPane();

        //创建容器存放数据,可以是数组,可以是集合类:
        Vector vector = new Vector();
        vector.add("krian");
        vector.add("lunatic");
        vector.add("liziran");

        JList jList = new JList(vector);

        contentPane.add(jList);

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

    public static void main(String[] args) {
        new JListTest("列表框");
    }
}

文本框:

  • 文本框
  • 密码框
  • 文本域
public class TextTest extends JFrame { //继承JFrame类
    public TextTest(String title){
        super(title);
        //获取当前窗口容器:
        Container contentPane = this.getContentPane();

        //创建一个文本框:
        JTextField jTextField = new JFormattedTextField("默认文本框");
        //创建一个密码框:
        JPasswordField jPasswordField = new JPasswordField();
        //创建一个文本域:
        JTextArea jTextArea = new JTextArea("默认文本域");

        contentPane.add(jTextField,BorderLayout.NORTH);
        contentPane.add(jPasswordField,BorderLayout.CENTER);
        contentPane.add(jTextArea,BorderLayout.SOUTH);

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

    public static void main(String[] args) {
        new TextTest("文本框");
    }
}

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱吃糖的范同学

你的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值