15.GUI编程

GUI编程

组件:

  • 窗口
  • 弹窗
  • 面板
  • 文本框
  • 列表框
  • 按钮
  • 图片
  • 监听事件
  • 鼠标
  • 键盘事件
  • 破解工具
简介

GUI的核心技术:Swing AWT

  1. 因为界面不美观
  2. 需要jre环境

为什么要学习?

  1. 可以写出心中的一些小工具
  2. 工作时候,也可能需要维护到Swing界面,概率极小!
  3. 了解MVC架构,了解监听!
AWT
  1. AWT介绍
    • 包含了很多类和接口!GUI:
    • 元素:窗口,按钮,文本框

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

2.组件和容器
package JAVA.GUI.Test01;

import java.awt.*;

//GUI的第一个界面
public class TestFrame {
    public static void main(String[] args) {
        //Frame ,JDK
        Frame frame = new Frame("我的第一个java图形界面窗口");
        //需要设置可见性
        frame.setVisible(true);
        //设置窗口大小
        frame.setSize(400,400);
        //设置背景颜色 Color
        frame.setBackground(new Color(85,250,57));
        //弹出初始设置
        frame.setLocation(200,200);
        //设置大小固定
        frame.setResizable(false);
    }
}

运行结果:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QCjDqebA-1629187897751)(C:\Users\LXK\AppData\Roaming\Typora\typora-user-images\image-20210809094518222.png)]

(问题:弹窗无法关掉,需停止程序才能关掉)

package JAVA.GUI.Test01;


import com.sun.source.tree.UsesTree;

import java.awt.*;

public class TestFrame2 {
    public static void main(String[] args) {
//展示多个窗口
        MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.cyan);
        MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.black);
        MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.pink);
        MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.yellow);
    }
}
class MyFrame extends Frame{
    static int id = 0;//可能存在多个窗口,我们需要一个计数器。
    public MyFrame(int x, int y, int w, int h, Color color){
        super("MyFrame+"+(++id));
        setBackground(color);
        setBounds(x,y,w,h);
        setVisible(true);
    }
}

运行结果:

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

3.面板

解决了关闭事件!

package JAVA.GUI.Test01;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

//Panel 可以看成是一个空间,但不能单独存在。
public class TestPanel {
    public static void main(String[] args) {
        Frame frame = new Frame();
        //布局概念
        Panel panel = new Panel();

        //设置布局
        frame.setLayout(null);
        //坐标
        frame.setBounds(300,300,500,500);
        frame.setBackground(new Color(11, 179, 232));
        //Pabel设置坐标,相对于frame
        panel.setBounds(50,50,400,400);
        panel.setBackground(new Color(231, 7, 15));

        //frame.add(panel)
        frame.add(panel);
        frame.setVisible(true);

        //监听事件,监听关闭窗口事件
        //适配器模式:
        frame.addWindowListener(new WindowAdapter() {
            //窗口点击关闭时需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }
}

运行结果:

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

4.布局管理器
  • 流式布局

    • package JAVA.GUI.Test01;
      
      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.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);
                  }
              });
      
          }
      }
      

运行结果:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tFKSidMy-1629187897755)(C:\Users\LXK\AppData\Roaming\Typora\typora-user-images\image-20210809110339377.png)]

  • 东西南北中

    • package JAVA.GUI.Test01;
      
      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.setVisible(true);
              frame.setSize(200,200);
              frame.addWindowListener(new WindowAdapter() {
                  @Override
                  public void windowClosing(WindowEvent e) {
                      System.exit(0);
                  }
              });
          }
      }
      

      运行结果:

      image-20210809105322983

  • 表格布局

    • package JAVA.GUI.Test01;
      
      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("TestGridLayout");
              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));
              frame.add(button1);
              frame.add(button2);
              frame.add(button3);
              frame.add(button4);
              frame.add(button5);
              frame.add(button6);
              frame.pack();//JAVA函数
              frame.setVisible(true);
              frame.addWindowListener(new WindowAdapter() {
                  @Override
                  public void windowClosing(WindowEvent e) {
                      System.exit(0);
                  }
              });
      
          }
      }
      

运行结果:

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

练习:

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

解:

package JAVA.GUI.Test01;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

//练习
public class ExDemo {
    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setVisible(true);
        frame.setSize(400,300);
        frame.setLocation(300,400);
        frame.setBackground(Color.pink);
        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));
//上面
        panel1.add(new Button("East-1"),BorderLayout.EAST);
        panel1.add(new Button("Wast-1"),BorderLayout.WEST);
        panel2.add(new Button("panel2-button-1"));
        panel2.add(new Button("panel2-button-2"));
        panel1.add(panel2,BorderLayout.CENTER);

//下面
        panel3.add(new Button("East-2"),BorderLayout.EAST);
        panel3.add(new Button("West-2"),BorderLayout.WEST);
        //中间四个
        for (int i = 0; i < 4; i++) {
            panel4.add(new Button("for-"+i));
        }
        panel3.add(panel4,BorderLayout.CENTER);

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

总结:

  1. Frame是一个顶级窗口。
  2. Panel无法单独显示,必须添加到某个容器中。
  3. 布局管理器
    • 流式
    • 东西南北中
    • 表格
  4. 大小,定位,背景颜色,可见性,监听!

事件监听

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

案例:

package JAVA.GUI.Test02;

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 button = new Button();

        //因为addActionListener()需要一个ActionListener,所以我们需要构造一个ActionListener
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);
        frame.add(button,BorderLayout.CENTER);
        frame.pack();
        windowClose(frame);
        frame.setVisible(true);
    }
    //关闭窗口的事件
    private static void windowClose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }
}
//事件监听
class MyActionListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("我好想你!");
    }
}

多个摁纽可以共享一个事件

package JAVA.GUI.Test02;

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 TestAction02 {
    public static void main(String[] args) {
        //两个按钮实现同一个监听
        //开始    停止
        Frame frame = new Frame("开始 - 停止");
        Button button1 = new Button("开始");
        Button button2 = new Button("停止");
        // 可以显示的定义触发会返回的命令,如果不显示定义,则会走默认值!
        //可以多个摁纽只写一个监听类
//        button1.setActionCommand("uwwu");
        MyAction myAction = new MyAction();
        button1.addActionListener(myAction);
        button2.addActionListener(myAction);
        frame.add(button1,BorderLayout.EAST);
        frame.add(button2,BorderLayout.WEST);
        frame.pack();
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
class MyAction implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        //e.getActionCommand()  获取按钮的信息
        System.out.println("点了一下"+e.getActionCommand());
    }
}

输入框TwxtField监听

package JAVA.GUI.Test02;

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

public class Test01 {
    public static void main(String[] args) {
        //启动
        new MyFrame();
    }
}
class MyFrame extends Frame{
    public MyFrame(){
        TextField textField = new TextField();
        add(textField);
        //监听这个文本框输入的文字
        MyActionListener2 myActionListener2 = new MyActionListener2();
        //按下enter 就会触发这个输入框的事件
        textField.addActionListener(myActionListener2);
        //设置一些属性
        setVisible(true);
        pack();
    }
}
class MyActionListener2 implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        TextField textField = (TextField) e.getSource();//获得一些资源,返回一个对象
        System.out.println(textField.getText());
        textField.setText("");//回车清空输入行 null
        textField.setEchoChar('*');
    }
}
简易计算器,组合+内部类回顾复习!

oop原则:组合大于继承!

案例:写一个简单的计算机:

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

package JAVA.GUI.Test02;

import java.awt.*;
import java.awt.event.*;

public class TestCalc {
    public static void main(String[] args) {
        new Calculator();
    }
}
//计算器类
class Calculator extends Frame{
    public Calculator() {
        //三个文本框
        TextField num1 = new TextField(10);//字符数
        TextField num2 = new TextField(10);
        TextField num3 = new TextField(20);
        //一个按钮
        Button button = new Button("=");
        button.addActionListener(new MyCalculatorListener(num1,num2,num3));
        //一个标签
        Label label1 = new Label("+");
        Label label2 = new Label("=");
        //布局
        setLayout(new FlowLayout());
        add(num1);
        add(label1);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);
    }
}
//监听器类
class MyCalculatorListener implements ActionListener{
    //获取3个变量
private TextField num1;
private TextField num2;
private TextField num3;
    public MyCalculatorListener(TextField num1,TextField num2,TextField num3) {
        this.num1 = num1;
        this.num2 = num2;
        this.num3 = num3;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //1.获得加数和被加数
        int n1 = Integer.parseInt(num1.getText());
        int n2 = Integer.parseInt(num2.getText());

        //2.将这个值加法运算后,放入第三个框。
        num3.setText(""+(n1+n2));

        //3.清楚前两个框
        num1.setText("");
        num2.setText("");

    }
}

优化:(面向对象)

package JAVA.GUI.Test02;

import java.awt.*;
import java.awt.event.*;

public class TestCalc {
    public static void main(String[] args) {
        new Calculator().loadFrame();
    }
}
//计算器类
class Calculator extends Frame{
    //属性
    TextField num1,num2,num3;
    //方法
    public void loadFrame(){
        //三个文本框
         num1 = new TextField(10);//字符数
         num2 = new TextField(10);
         num3 = new TextField(20);
        //一个按钮
        Button button = new Button("=");
        button.addActionListener(new MyCalculatorListener(this));
        //一个标签
        Label label1 = new Label("+");
        Label label2 = new Label("=");
        //布局
        setLayout(new FlowLayout());
        add(num1);
        add(label1);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);
    }
//    public Calculator() {
//
//    }
}
//监听器类
class MyCalculatorListener implements ActionListener{
    //获取计算器这个对象,在一个类中组合另外一个类:
    Calculator calculator = null;
    //获取3个变量
//private TextField num1;
//private TextField num2;
//private TextField num3;
    public MyCalculatorListener(Calculator calculator) {
        this.calculator = calculator;
//        this.num1 = num1;
//        this.num2 = num2;
//        this.num3 = num3;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
       int n1 = Integer.parseInt(calculator.num1.getText());
       int n2 = Integer.parseInt(calculator.num1.getText());
       calculator.num3.setText(""+(n1+n2));
       calculator.num1.setText("");
       calculator.num2.setText("");

        //1.获得加数和被加数
//        int n1 = Integer.parseInt(num1.getText());
//        int n2 = Integer.parseInt(num2.getText());

        //2.将这个值加法运算后,放入第三个框。
//        num3.setText(""+(n1+n2));

        //3.清楚前两个框
//        num1.setText("");
//        num2.setText("");

    }
}
内部类实现简化
package JAVA.GUI.Test02;

import java.awt.*;
import java.awt.event.*;

public class TestCalc {
    public static void main(String[] args) {
        new Calculator().loadFrame();
    }
}
//计算器类
class Calculator extends Frame{
    //属性
    TextField num1,num2,num3;
    //方法
    public void loadFrame(){
        //三个文本框
         num1 = new TextField(10);//字符数
         num2 = new TextField(10);
         num3 = new TextField(20);
        //一个按钮
        Button button = new Button("=");
        button.addActionListener(new MyCalculatorListener());
        //一个标签
        Label label1 = new Label("+");
        //布局
        setLayout(new FlowLayout());
        add(num1);
        add(label1);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);
    }
    //监听器类
    //内部类最大的好处,就是可以畅通无阻的访问外部属性和方法!
    private class MyCalculatorListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            int n1 = Integer.parseInt(num1.getText());
            int n2 = Integer.parseInt(num1.getText());
            num3.setText(""+(n1+n2));
            num1.setText("");
            num2.setText("");
        }
    }
}
画笔
package JAVA.GUI.Test03;

import java.awt.*;

public class TestPaint {
    public static void main(String[] args) {
        new MyPaint().loadFrame();
    }
}
class MyPaint extends Frame {
    public void loadFrame(){
        setBounds(200,200,600,400);
        setVisible(true);
    }
    //画笔

    @Override
    public void paint(Graphics g) {
        //        //画笔,需要有颜色,可以画画
        g.setColor(Color.red);
        g.drawOval(100,100,100,100);//圆
        g.fillOval(100,100,100,100);//实心圆
        g.setColor(Color.GREEN);
        g.fillRect(150,200,200,200);
    }
}
鼠标监听

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

package JAVA.GUI.Test04;

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(200,200,400,300);
//存储鼠标点击的点
        points = new ArrayList<>();

        setVisible(true);
        //鼠标监听器,对于这个窗口
        this.addMouseListener(new MyMouseListener());
    }

    @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 MyMouseListener extends MouseAdapter{
        //鼠标按下,弹起,按住不放
        @Override
        public void mousePressed(MouseEvent e) {
            Myframe frame = (Myframe) e.getSource();
            //这个我们点击的时候,就会在界面上产生一个点!
//            这个点就是鼠标的点
            frame.addPaint(new Point(e.getX(),e.getY()));

            //每次点击鼠标都需要重新画一遍
            frame.repaint();//刷新
        }
    }
}
窗口监听
package JAVA.GUI.Test05;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestWindow {
    public static void main(String[] args) {
        new WindowFrame();

    }
}
class WindowFrame extends Frame{
    public WindowFrame() {
        setBackground(Color.pink);
        setBounds(100,100,200,200);
        setVisible(true);
//        addWindowListener(new MyWindowListener());
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("关闭");
                System.exit(0);
            }
            @Override
            public void windowActivated(WindowEvent e) {
                WindowFrame source = (WindowFrame)e.getSource();
                source.setTitle("haha");
                System.out.println("窗口激活");
            }
        });
    }
    class MyWindowListener extends WindowAdapter{
        @Override
        public void windowClosing(WindowEvent e) {
            setVisible(false);//隐藏窗口,通过按钮隐藏窗口

            System.exit(0);//正常退出
        }
    }
}
键盘监听
package JAVA.GUI.Test06;



import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

//键
public class TestKeyListener {
    public static void main(String[] args) {
new KeyFrame();
    }
}
class KeyFrame extends Frame{
    public KeyFrame(){
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("关闭");
                System.exit(0);
            }
        });
        setBounds(1,1,300,400);
        setVisible(true);
        this.addKeyListener(new KeyAdapter() {
            //键盘按下
            @Override
            public void keyPressed(KeyEvent e) {
                //获得键盘按下的是哪一个,当前的码
                int keyCode = e.getKeyCode();//不需要去记录这个数值,直接使用静态属性 VK_XXX
                System.out.println(keyCode);
                if(keyCode == KeyEvent.VK_UP) {
                    System.out.println("你摁下了上键");
                }
            }
        });
    }
}

Swing

窗口、面板

package JAVA.GUI.Swing.JFrame;

import javax.swing.*;

public class JFrameTest01 {
    //init();初始化
    public void init(){
        //顶级窗口
        JFrame frame = new JFrame("这是一个JFrame窗口");
        frame.setVisible(true);
        frame.setBounds(100,100,200,200);

        //设置文字
        JLabel label = new JLabel("haha");
        frame.add(label);

        //容器实例化
        //关闭事件
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        //建立一个窗口
        new JFrameTest01().init();

    }
}
package JAVA.GUI.Swing.JFrame;

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

public class JFrameTest02 {
    public static void main(String[] args) {
        new MyJframe2().init();
    }
}
class MyJframe2 extends JFrame{
    public void init(){
        this.setVisible(true);
        this.setBounds(10,10,200,300);

        //设置文字
        JLabel label = new JLabel("haha");
        this.add(label);
        //让文本标签居中
        label.setHorizontalAlignment(SwingConstants.CENTER);
        //获得一个容器
        Container container = this.getContentPane();
        container.setBackground(Color.pink);
    }
}

弹窗

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

package JAVA.GUI.Swing.Dialog;

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

public class DialogTest01 extends JFrame {
    public DialogTest01(){
        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 MyDialogTest01();
            }
        });
        container.add(button);
    }
    public static void main(String[] args) {
        new DialogTest01();
    }
}
class MyDialogTest01 extends JDialog{
    public MyDialogTest01(){
        this.setVisible(true);
        this.setBounds(100,100,500,500);
//        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container container = this.getContentPane();
//        container.setLayout(null);
        container.add(new Label("haha"));
    }
}

标签

package JAVA.GUI.Swing.Icon;

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

//图标,需要实现类,Frame继承
public class IconTest01 extends JFrame implements Icon {

    private int width;
    private int height;
    public IconTest01(){}//无参构造

    public IconTest01(int width, int height){
        this.width = width;
        this.height = height;
    }
    public void init(){
        IconTest01 iconTest01= new IconTest01(50,50);
        //图标放在标签上也可以放在按钮上
        JLabel label = new JLabel("icontest", SwingConstants.CENTER);
        Container container = getContentPane();
        container.add(label);
        this.setVisible(true);
        this.setBounds(300,300,400,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

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

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.fillOval(x,y,width,height);
    }

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

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

面板

按钮

  • 单选按钮
  • 复选按钮

列表

文本框

ate int width;
private int height;
public IconTest01(){}//无参构造

public IconTest01(int width, int height){
    this.width = width;
    this.height = height;
}
public void init(){
    IconTest01 iconTest01= new IconTest01(50,50);
    //图标放在标签上也可以放在按钮上
    JLabel label = new JLabel("icontest", SwingConstants.CENTER);
    Container container = getContentPane();
    container.add(label);
    this.setVisible(true);
    this.setBounds(300,300,400,500);
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

}

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

@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
    g.fillOval(x,y,width,height);
}

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

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

}




### 面板



### 按钮

- 单选按钮
- 复选按钮



### 列表



### 文本框
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值