GUI

AWT

2.1、Frame

Frame的实例是一个底层容器,即通常被称为窗口。

package GUI;

import javax.swing.*;
import java.awt.*;
//GUI的第一个界面
public class testFrame {
    public static void main(String[] args) {
        //Frame,JDK  看源码;
        Frame frame = new Frame("我的第一个java图形界面窗口");
        //设置可见性 w h 没有
        frame.setVisible(true);
        //设置窗口大小
        frame.setSize(400, 400);
        //背景颜色 Color
        Color color = new Color(155, 89, 104);
        frame.setBackground(color);
        //弹出的初始位置
        frame.setLocation(200, 200);
        //设置大小固定
        frame.setResizable(false);
    }
}

尝试回顾封装:


package GUI;

import java.awt.*;
public class testFrame2 {
    public static void main(String[] args) {
        MyFrame01 myFrame1 = new MyFrame01(100, 100, 200, 200, Color.blue);
        MyFrame01 myFrame2 = new MyFrame01(300, 100, 200, 200, Color.yellow);
        MyFrame01 myFrame3 = new MyFrame01(500, 100, 200, 200, Color.RED);
        MyFrame01 myFrame4 = new MyFrame01(700, 100, 200, 200, Color.BLACK);
    }
}
class MyFrame01 extends Frame{
    //除了Frame方法以外,还有自己的方法
    static int id = 0;//可能存在多个窗口,我们需要一个计数器
    //构造器封装一下
    public MyFrame01(int x,int y, int w,int h,Color color){
        super("MyFrame+"+(++id));
        setBackground(color);
        setVisible(true);
        //相当于初始位置x,y和窗口大小w h
        setBounds(x,y,w,h);
        setResizable(false);
    }
}


2.2 、面板panel


package GUI;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
//Panel 可以看成一个空间,但是不能单独存在,得放在Frame上
public class testPanel {
    public static void main(String[] args) {
        //窗口
        Frame frame = new Frame();
        //布局的概念
        //面板
        Panel panel = new Panel();
        /*设置布局  不设置会默认置顶
         未设置Layout时,java默认为flowLayout布局的,
         设置为null即为清空布局管理器,之后添加组件,常常是设置组件左上角坐标相
         对于容器左上角(0,0)的x,y值来确定组件的位置,即使更改容器大小也不会
         改变位置。这种方式常常用于窗体大小固定的容器里。*/
        frame.setLayout(null);
        //坐标
        frame.setBounds(300,300,500,500);
        frame.setBackground(new Color(59, 164, 125));
        //panel 设置坐标,相对于Frame的坐标
        panel.setBounds(50,50,200,200);
        panel.setBackground(new Color(90, 46, 30));
        //frame.add(panel)frame添加面板
        frame.add(panel);//Panel经过三层继承,最终继承了Component
        frame.setVisible(true);
        //监听时间,监听窗口关闭事件->System.exit(0)
        //适配器模式: new WindowsListener()重写的太多了,所以new其子类new WindowAdapter(),  adapter(适配器)
        frame.addWindowListener(new WindowAdapter() {
            //窗口点击关闭的时候需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);
            }
        });
    }
}


2.3 布局管理器

setLayout(布局对象)

flowlayout 流式布局 从左到右


package GUI;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class testFlowLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();
        //组件-按钮
        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");
        //设置为流式布局
        //frame.setLayout(new FlowLayout());//默认是中FlowLayout.center
        //frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
        frame.setSize(200,200);
        frame.setVisible(true);
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.addWindowListener(new WindowAdapter() {
            //窗口点击关闭的时候需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);
            }
        });
    }
}


BorderLayout 东西南北中


package GUI;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class testBorderLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("TestBorderLayout");

        Button east = new Button("East");
        Button west = new Button("West");
        Button south = new Button("South");
        Button north = new Button("North");
        Button center = new Button("Center");
        //东西南北中布局不会自动填充,所以得自己去定义组件放的位置。
        frame.add(east,BorderLayout.EAST);
        frame.add(west ,BorderLayout.WEST);
        frame.add(south ,BorderLayout.SOUTH);
        frame.add(north ,BorderLayout.NORTH);
        frame.add(center,BorderLayout.CENTER);

        frame.setSize(300,300);
        frame.setVisible(true);

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

GridLayout 表格布局


package GUI;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class testGridLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("TsetGridLayout");

        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");
        Button button4 = new Button("button4");
        Button button5 = new Button("button5");
        Button button6 = new Button("button6");

        frame.setLayout(new GridLayout(3,2));
        //表格布局会自动填充,所以直接add即可,系统会按照add的顺序自动排列
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);
        frame.add(button6);

        frame.pack();//让布局变得好看
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}


实例


package GUI;


import java.awt.*;

public class work01 {
    public static void main(String[] args) {
        Frame frame = new Frame("TestLayoutLianxi");
        frame.setSize(400,300);
        frame.setLocation(300,300);
        frame.setBackground(Color.BLUE);
        frame.setVisible(true);
        //表格布局窗口,两行一列
        frame.setLayout(new GridLayout(2,1));

        Panel panel1 = new Panel(new BorderLayout());
        Panel panel2 = new Panel(new GridLayout(2,1));
        Panel panel3 = new Panel(new BorderLayout());
        Panel panel4 = new Panel(new GridLayout(2,2));

        Button button1 = new Button("BUTTON1");
        Button button2 = new Button("BUTTON2");
        Button button3 = new Button("BUTTON3");
        Button button4 = new Button("BUTTON4");
        Button button5 = new Button("BUTTON5");
        Button button6 = new Button("BUTTON6");
        Button button7 = new Button("BUTTON7");
        Button button8 = new Button("BUTTON8");
        Button button9 = new Button("BUTTON9");
        Button button10 = new Button("BUTTON10");
        //东西南北中布局pannel1
        panel1.add(button1,BorderLayout.EAST);
        panel1.add(button2,BorderLayout.WEST);
        //表格布局panel2,两行一列
         /*另一种简洁的写法:
        for(int i=3;i<=4;i++){
            panel4.add(new Button("button"+i));
        }
        这样写前面就不用new Button();
        */
        panel2.add(button3);
        panel2.add(button4);
        //将pannel2嵌套到pannel1中
        panel1.add(panel2,BorderLayout.CENTER);
        //东西南北中布局pannel3
        panel3.add(button5,BorderLayout.EAST);
        panel3.add(button6,BorderLayout.WEST);
        //表格布局panel4,两行两列
        /*另一种简洁的写法:
        for(int i=7;i<=10;i++){
            panel4.add(new Button("button"+i));
        }
        */
        panel4.add(button7);
        panel4.add(button8);
        panel4.add(button9);
        panel4.add(button10);
        //将pannel4嵌套到pannel3中
        panel3.add(panel4,BorderLayout.CENTER);
        //将pannel1和pannel3嵌套到窗口中
        frame.add(panel1);
        frame.add(panel3);
        
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}


总结

之前的知识总结

  1. 标题,大小,定位,背景颜色,可见性,监听
  2. Frame是一个底层容器。
  3. Panel是一个中间容器,无法单独显示,必须添加到底层容器中
  4. 布局管理器
    a. 流失
    b. 东西南北中
    c. 表格

2.4 、事件监听

事件监听:当某个事件发生的时候,干什么?


package GUI;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class testActionEvent {
    public static void main(String[] args) {
        //按下按钮,触发一些事件
        Frame frame = new Frame();
        Button button1 = new Button("button1");
        //因为addActionListener需要ActionListener,因此我们需要构造一个ActionListener
        MyActionListener myActionListener = new MyActionListener();
        button1.addActionListener(new MyActionListener());
        frame.add(button1, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
        windowsClosing(frame);//调用关闭窗体方法
    }
    //关闭窗体的事件
    private static void windowsClosing(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
    //事件监听
    static class MyActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("点击按钮");
        }
    }
}


多个按钮共享一个事件

package GUI;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileOutputStream;
public class testActionEvent2 {
    public static void main(String[] args) {
        //两个按钮,实现同一个监听
        Frame frame = new Frame("多个愿望一次满足");
        Button button1 = new Button("start");
        Button button2 = new Button("stop");
        //setActionCommand()可以显示的定义出发会返回的命令,如果不显示定义,则会走默认值 
        button1.setActionCommand("button1-start");
        //可以多个按钮只写一个监听类
        MyActionListener myActionListener = new MyActionListener();
        button1.addActionListener(myActionListener);
        button2.addActionListener(myActionListener);
        frame.add(button1, BorderLayout.WEST);
        frame.add(button2, BorderLayout.EAST);
        frame.pack();
        frame.setVisible(true);
    }

}
class MyActionListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        //e.getActionCommand()获得按钮的信息
        System.out.println("按钮被点击了:" + e.getActionCommand());
    }
}

2.5、输入框事件监听TextField

输入框中输入的字,可以打印出来,并将输入的字全部删除。


package GUI.actionListener;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class textArea01 {
    public static void main(String[] args) {
        //启动!只负责启动
        new MyFrame();
    }
}
class MyFrame extends Frame{
    public MyFrame(){
        TextField textField = new TextField();
        add(textField);
        //监听这个文本框输入的文字
        //按下回车键,就会触发这个输入框的事件,在下边的重写方法中重写的语句为  获得输入框的文本并打印
        textField.addActionListener(new myActionListener());
        //设置替换编码
        textField.setEchoChar('*');
        setVisible(true);
        pack();
    }
}
class myActionListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        //getSource()获得一些资源,返回的一个对象,返回对象的类型是object,下转型为TextField
        TextField text = (TextField) e.getSource();
        System.out.println(text.getText());//获得输入框的文本
        //每次都设置为空 即每次文本框输入完以后,都会全部删除清零
        text.setText("");
    }
}


2.6、简易计算器,组合+内部类回顾复习

oop原则:组合,大于继承

原始版本


package GUI.actionListener;

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

public class addCalculate01 {
    public static void main(String[] args) {
        new Calculator01();
    }
}
//计算器类
class Calculator01 extends Frame {
    public Calculator01() {
        //3个文本框
        TextField textField1 = new TextField(10);//字符数
        TextField textField2 = new TextField(10);//字符数
        TextField textField3 = new TextField(20);//字符数
        //1 个按钮
        Button button = new Button("=");
        button.addActionListener(new addActionListener01(textField1,textField2,textField3));
        //1个标签
        Label label = new Label("+");

        setLayout(new FlowLayout());
        add(textField1);
        add(label);
        add(textField2);
        add(button);
        add(textField3);
        pack();
        setVisible(true);
    }
}
//监听器类
class addActionListener01 implements ActionListener {
    //获取三个变量
    private TextField textField1,textField2,textField3;

    public addActionListener01(TextField textField1, TextField textField2, TextField textField3){
        this.textField1 = textField1;
        this.textField2 = textField2;
        this.textField3 = textField3;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //1.getText()获得加数和被加数。Integer.parseInt()是把()里的内容转换成整数,将"123"=>123
        int n = Integer.parseInt(textField1.getText());
        int n1 = Integer.parseInt(textField2.getText());
        //2.setText将这个值加法运算后,放到第三个框。""+(n+n1)把整数转化为字符串,将123="123"
        textField3.setText(""+(n+n1));
        //3.清除前两个框
        textField1.setText("");
        textField2.setText("");
    }
}


组合版本

  • 完全改造成面向对象
package GUI.actionListener;

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

public class addCalculate02 {
    public static void main(String[] args) {
        new Calculator02().loadFrame();
    }
}
//计算器类
class Calculator02 extends Frame {
    //属性
    TextField textField1,textField2,textField3;
    //方法
    public void loadFrame(){
        //组件——3个文本框,1个按钮,1个标签
        textField1 = new TextField(10);//字符数
        textField2 = new TextField(10);//字符数
        textField3 = new TextField(20);//字符数
        Button button = new Button("=");
        Label label = new Label("+");
        //添加按钮监听事件
        button.addActionListener(new addActionListener02(this));
        //布局
        setLayout(new FlowLayout());
        add(textField1);
        add(label);
        add(textField2);
        add(button);
        add(textField3);
        pack();
        setVisible(true);
    }

}
//监听器类
class addActionListener02 implements ActionListener {
    //获取计算器这个对象,在一个类中组合另一个类
    Calculator02 calculator;
    public addActionListener02(Calculator02 calculator){
        this.calculator=calculator;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //1.获得加数和被加数
        int t1=Integer.parseInt(calculator.textField1.getText());
        int t2=Integer.parseInt(calculator.textField2.getText());
        //2.将这个值加法运算后,放到第三个框
        calculator.textField3.setText(""+(t1+t2));
        //3.清除前两个框
        calculator.textField1.setText("");
        calculator.textField2.setText("");
    }
}


内部类版本


package GUI.actionListener;

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

public class addCalculate03 {
    public static void main(String[] args) {
        new Calculator02().loadFrame();
    }
}
//计算器类
class Calculator03 extends Frame {
    //属性
    TextField textField1,textField2,textField3;
    //方法
    public void loadFrame(){
        //组件——3个文本框,1个按钮,1个标签
        textField1 = new TextField(10);//字符数
        textField2 = new TextField(10);//字符数
        textField3 = new TextField(20);//字符数
        Button button = new Button("=");
        Label label = new Label("+");
        //添加按钮监听事件
        button.addActionListener(new addActionListener02());
        //布局
        setLayout(new FlowLayout());
        add(textField1);
        add(label);
        add(textField2);
        add(button);
        add(textField3);
        pack();
        setVisible(true);
    }
    //内部监听器类
    //内部类最大的好处,就是可以畅通无阻的访问外部的属性和方法!
    private class addActionListener02 implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            //1.获得加数和被加数
            int t1=Integer.parseInt(textField1.getText());
            int t2=Integer.parseInt(textField2.getText());
            //2.将这个值加法运算后,放到第三个框
            textField3.setText(""+(t1+t2));
            //3.清除前两个框
            textField1.setText("");
            textField2.setText("");
        }
    }
}


2.7、画笔


package GUI.Paint;

import java.awt.*;
public class textPaint {
    public static void main(String[] args) {
        new MYpaint().loadFrame1();
    }
}
class MYpaint extends Frame{
    //创建窗口
    public void loadFrame1(){
        setBounds(200,200,600,400);
        setVisible(true);
    }
    @Override
    public void paint(Graphics g) {
        //super.paint(g);有些类的父类有一些初始化操作,不能随便干掉
        //画笔,需要颜色,画笔可以画画
        g.setColor(Color.red);
        g.drawOval(100,100,100,100);//空心圆
        g.fillOval(200,100,100,100);//实心园
        g.setColor(Color.green);
        g.fillRect(100,300,100,100);//实心矩形
        g.drawRect(200,300,100,100);//空心矩形
        //养成习惯 画笔画完,将他还原到最初的颜色
        g.setColor(Color.BLACK);
    }
}


2.8、鼠标监听

目的:想要实现鼠标画画!

思路:

  1. 创建 画板 画笔 鼠标 存点的集合
  2. 鼠标监听事件,当鼠标在画板上点击时,获得一个点(鼠标当前的坐标x,y)
  3. 将该点(x,y)存进集合中
  4. 画笔将集合中点读出,画在画板上。但是画笔只能画一次,所以就让鼠标每次点击后都重新画一次(repaint())
package GUI;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
//鼠标监听事件
public class TestMouseListener {
    public static void main(String[] args) {
        new MyFrame("画图");
    }
}
//自己的类
class MyFrame extends Frame {
    //画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点
    ArrayList points;//存储点的集合
    public MyFrame(String title) {
        super(title);
        setBounds(100, 100, 500, 400);
        //存鼠标的点
        points = new ArrayList<>();
        setVisible(true);
        //鼠标监听器,针对这个窗口
        this.addMouseListener(new myMouseAdapter());
    }
    @Override
    public void paint(Graphics g) {
        //画画,监听鼠标的事件
        Iterator iterator = points.iterator();//迭代器
        while (iterator.hasNext()){
            Point point = (Point) iterator.next();
            g.setColor(Color.BLUE);
            g.fillOval(point.x,point.y,10,10);
        }
    }
    //添加一个点到界面上
    public void addPaint(Point point){
        points.add(point);
    }
    //适配器模式
    private class myMouseAdapter extends MouseAdapter {
        //鼠标 按下,弹起,按住不放
        @Override
        public void mouseClicked(MouseEvent e) {
            MyFrame myframe = (MyFrame) e.getSource();
            //这里我们点击的时候,就会在界面产生一个点
            myframe.addPaint(new Point(e.getX(),e.getY()));
            //每次点击鼠标都需要重新画一遍
            myframe.repaint();//刷新
        }
    }
}


2.9、窗口监听


package GUI.Window;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class testWindow {
    public static void main(String[] args) {
        new myWindowFrame();
    }
}
class myWindowFrame extends Frame {
    public myWindowFrame() {
        setBackground(Color.BLUE);
        setBounds(100, 100, 200, 200);
        setVisible(true);
        this.addWindowListener(
                //匿名内部类
                new WindowAdapter() {
                    //关闭窗口
                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.out.println("windowsClosing");
                        System.exit(0);
                    }
                    //激活窗口
                    @Override
                    public void windowActivated(WindowEvent e) {
                        myWindowFrame source = (myWindowFrame) e.getSource();
                        source.setTitle("已激活");
                        System.out.println("windowActivated");
                    }
                }
                );
    }
           /* @Override
            public void windowClosing(WindowEvent e) {
                setVisible(false);// 隐藏窗口
                System.exit(0);//正常退出   1是非正常退出
            };*/
}


2.10、键盘监听

package GUI.KeyListener;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.SQLOutput;
//键
public class testKeyListener {
    public static void main(String[] args) {
        new KeyFrame();
    }
}
class KeyFrame extends Frame{
    public KeyFrame(){
        setBounds(0,0,300,400);
        setVisible(true);
        //适配器
        this.addKeyListener(new KeyAdapter() {
            //键盘按下
            @Override
            public void keyPressed(KeyEvent e) {
                //getKeyCode()获得键盘下的键对应的整型数据
                int keyCode = e.getKeyCode();//不需要去记录这个数值,直接使用静态属性VK_xxx
                System.out.println(keyCode);
                if (keyCode == KeyEvent.VK_UP){
                    System.out.println("你按了上键盘");
                    //根据不同的操作,进行不同的结果
                }
            }
        });
    }
}

Swing

3.1、窗口JFrame


package Swing.Jframe;

import javax.swing.*;
import java.awt.*;
public class testJframe {
    //init();初始化
    public void init(){
        //顶级窗口
        JFrame jf = new JFrame("这是一个JFrame窗口");
        jf.setBounds(100,100,400,300);
        //设置文字Label->JLabel
        jf.setBackground(Color.BLUE);
        JLabel jl = new JLabel("JLabel");
        jf.add(jl);
        //让文本标签居中
        jl.setHorizontalAlignment(SwingConstants.CENTER);
        //容器实例化,这样我们才能看到效果
        jf.getContentPane().setBackground(Color.yellow);
        jf.setVisible(true);
        //关闭事件
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        //建立一个窗口
        new testJframe().init();
    }
}


3.2、弹窗Dialog


package Swing.Dialog;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class testDialog extends JFrame {
    public testDialog() {
        this.setVisible(true);
        this.setBounds(100, 100, 400, 400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //Jframe 放东西,容器
        Container contentPane = this.getContentPane();
        //绝对布局
        contentPane.setLayout(null);
        //设置背景
        contentPane.setBackground(Color.BLUE);
        //按钮
        JButton jButton = new JButton("点击弹出一个对话框");
        jButton.setBounds(30, 30, 200, 50);
        //点击按钮弹出弹框,监听按钮事件
        jButton.addActionListener(new ActionListener() {//监听器
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹窗
                new MyDialog();
            }
        });
        contentPane.add(jButton);
    }
    public static void main(String[] args) {
        new testDialog();
    }
}
//弹窗的窗口
class MyDialog extends JDialog {
    public MyDialog() {
        this.setVisible(true);
        this.setBounds(500, 100, 500, 500);
        //this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //JDialog退出如果使用EXIT会报错,但JDiaolog本身就默认有关闭事件。
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        //JDialog退出只能是D0_ONTHING,HIDE,DISPOSE这三个中的一种
        this.setTitle("这是一个弹窗");
        Container contentPane = this.getContentPane();
        contentPane.setLayout(null);
        contentPane.setBackground(Color.ORANGE);
        JLabel lable = new JLabel("学习学习");
        contentPane.add(lable);
        lable.setBounds(20,20,100,50);
    }
}

3.3、标签

图标Icon

package Swing.Icon;

import javax.swing.*;
import java.awt.*;
//图标,需要实现类,Frame继承
public class testIcon extends JFrame implements Icon {
    private  int width;
    private  int hight;
    //无参构造
    public testIcon(){};
    //有参构造
    public testIcon(int width,int hight){
        this.width = width;
        this.hight = hight;
    };
    public void init(){
        testIcon iconDemo = new testIcon(15, 15);
        //图标可以放在标签,也可以放在按钮上!
        //JLabel(String text, Icon icon, int horizontalAlignment)
        JLabel jLabel = new JLabel("标签",iconDemo,SwingConstants.CENTER);
        Container contentPane = getContentPane();
        contentPane.add(jLabel);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.setColor(Color.blue);
        g.fillOval(x,y,width,hight);

    }
    @Override
    public int getIconWidth() {
        return this.width;
    }
    @Override
    public int getIconHeight() {
        return this.hight;
    }
    public static void main(String[] args) {
        new testIcon().init();
    }
}

图片标签ImageIcon


package Swing.Icon;

import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class testImageIcon extends JFrame {
    public testImageIcon(){
        JLabel jLabel = new JLabel("图片");
        //获取图片的地址
        //testImageIcon.class.getResource("name.jpg")->获取当前这个类(testImageIcon)的同级资源下名为name.jpg的文件。
        URL url = testImageIcon.class.getResource("img.png");
        //将图片放进imageIcon中
        //ImageIcon是一个类已经实现了Icon接口
        ImageIcon imageIcon = new ImageIcon(url);
        //将imageIcon放进jLabel中
        jLabel.setIcon(imageIcon);
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        Container contentPane = getContentPane();
        contentPane.add(jLabel);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBounds(100,100,800,800);
    }
    public static void main(String[] args) {
        new testImageIcon();
    }
}


3.4、面板

JPanel


package Swing.Jpanel;

import javax.swing.*;
import java.awt.*;
public class testJpanel extends JFrame {
    public  testJpanel(){
        Container contentPane = this.getContentPane();
        contentPane.setLayout(new GridLayout(2,1,10,10));//后边两个参数代表的是间距
        JPanel jPanel = new JPanel(new GridLayout(1, 3));
        JPanel jPane2 = new JPanel(new GridLayout(1, 2));
        JPanel jPane3 = new JPanel(new GridLayout(2, 1));
        JPanel jPane4 = new JPanel(new GridLayout(2, 2));

        jPanel.add(new JButton("a"));
        jPanel.add(new JButton("b"));
        jPanel.add(new JButton("c"));
        jPane2.add(new JButton("1"));
        jPane2.add(new JButton("2"));
        jPane3.add(new JButton("A"));
        jPane3.add(new JButton("B"));
        jPane4.add(new JButton("@"));
        jPane4.add(new JButton("#"));
        jPane4.add(new JButton("$"));
        jPane4.add(new JButton("&"));

        contentPane.add(jPanel);
        contentPane.add(jPane2);
        contentPane.add(jPane3);
        contentPane.add(jPane4);

        setBounds(100,100,500,400);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
    }
    public static void main(String[] args) {
        new testJpanel();
    }
}

JScrollPanel


package Swing.Jpanel;

import javax.swing.*;
import java.awt.*;
public class testJScrollPanel extends JFrame {
    public testJScrollPanel(){
        Container contentPane = this.getContentPane();
        //文本域JTextArea允许编辑多行文本,文本框JTextField允许编辑单行文本
        //JTextArea(20, 50)表示20行50列
        JTextArea jTextArea = new JTextArea(20, 50);
        jTextArea.setText("学习学习");
        //JScrolPane()面板,带滚动条的面板,并添加到contentpane
        JScrollPane jScrollPane = new JScrollPane(jTextArea);
        contentPane.add(jScrollPane);
        this.setVisible(true);
        this.setBounds(100,100,400,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jTextArea.setBackground(Color.yellow);
    }
    public static void main(String[] args) {
        new testJScrollPanel();
    }
}


3.5、按钮

图片按钮


import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo01 extends JFrame {
    public JButtonDemo01(){
        Container contentPane = this.getContentPane();
        //图片变为图标
        URL resource = JButtonDemo01.class.getResource("4.png");
        Icon icon = new ImageIcon(resource);
        JButton jButton = new JButton();
        jButton.setIcon(icon);
        //悬浮框
        jButton.setToolTipText("这是一个图片按钮");
        contentPane.add(jButton);
        this.setVisible(true);
        this.setBounds(100,100,400,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JButtonDemo01();
    }
}


单选按钮


package Swing.Button;

import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class testButton02 extends JFrame {
    public testButton02(){
        Container contentPane = this.getContentPane();
        //图片变为图标
        URL resource = testButton02.class.getResource("img.png");
        Icon icon = new ImageIcon(resource);
        //单选框JRadioButton
        JRadioButton radioButton1 = new JRadioButton("radioButton1");
        JRadioButton radioButton2 = new JRadioButton("radioButton2");
        JRadioButton radioButton3 = new JRadioButton("radioButton3");
        //由于单选框只能选择一个,分组
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(radioButton1);
        buttonGroup.add(radioButton2);
        buttonGroup.add(radioButton3);

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

        this.setVisible(true);
        this.setBounds(100,100,400,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new testButton02();
    }
}


复选按钮

package Swing.Button;

import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class testButton03 extends JFrame {
    public testButton03(){
        Container contentPane = this.getContentPane();
        //图片变为图标
        URL resource = testButton02.class.getResource("img.png");
        Icon icon = new ImageIcon(resource);

        //多选框
        JCheckBox checkBox1 = new JCheckBox("checkBox1");
        JCheckBox checkBox2 = new JCheckBox("checkBox2");
        JCheckBox checkBox3 = new JCheckBox("checkBox3");
        //流式布局
        contentPane.setLayout(new FlowLayout(FlowLayout.LEFT));
        contentPane.add(checkBox1);
        contentPane.add(checkBox2);
        contentPane.add(checkBox3);
        //东西南北中布局
        /*
         contentPane.add(checkBox1,BorderLayout.NORTH);
        contentPane.add(checkBox2,BorderLayout.CENTER);
        contentPane.add(checkBox3,BorderLayout.SOUTH);
        */

        this.setVisible(true);
        this.setBounds(100,100,400,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new testButton03();
    }
}

3.6、列表

下拉框

列表框

3.7、文本框

文本框

package Swing.Text;

import javax.swing.*;
import java.awt.*;
public class testTextField extends JFrame {
    public testTextField(){
        Container container = this.getContentPane();
        
        //不布局只会出现WORLD,且位置不对
        this.setLayout(new FlowLayout(FlowLayout.LEFT));
        JTextField jTextField1 = new JTextField("HELLO");
        JTextField jTextField2 = new JTextField("WORLD",20);
        container.add(jTextField1);
        container.add(jTextField2);
        
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(100,100,400,300);
    }
    public static void main(String[] args) {
        new testTextField();
    }
}

密码框


package Swing.Text;

import javax.swing.*;
import java.awt.*;
public class testPassword extends JFrame {
    public testPassword(){
        Container container = this.getContentPane();

        JPasswordField jPasswordField = new JPasswordField();
        //若不设置替换格式,则默认为 *
        jPasswordField.setEchoChar('#');
        container.add(jPasswordField);

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(100,100,400,300);
    }
    public static void main(String[] args) {
        new testPassword();
    }
}


文本域


package Swing.Text;

import javax.swing.*;
import java.awt.*;
public class testTextArea extends JFrame {
    public testTextArea(){
        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.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(100,100,400,300);
    }
    public static void main(String[] args) {
        new testTextArea();
    }
}