GUI编程

1、简介

  • GUI的核心技术:Swing AWT(即将淘汰 )
  • 需要jre环境
  • 界面不美观
  • 为什么要学习?
  • 可以写出自己心中想要的一些小工具
  • 工作时候,也可能需要维护到Swing界面,概率极小
  • 了解MVC架构,了解监听!

2、AWT

2.1 AWT介绍

  • 包含了很多类和接口,用于图形用户界面编程。
  • 元素:窗口、按钮、文本框
  • java.awt.*
  • 组件(Component)
  • 基本组件
  • button
  • TextArea
  • Label
  • ...
  • 容器(Container)
  • Window
  • Frame
  • Dialog(弹窗)
  • Panel(面板)
  • Applet

2.2组件和容器

  • frame
package com.gui;

import java.awt.*;

//GUI的第一个界面
public class TestFrame {
    public static void main(String[] args) {
        Frame frame = new Frame("第一个JAVA图像界面窗口");
        //设置可见性
        frame.setVisible(true);
        //设置窗口大小
        frame.setSize(400,400);
        //设置背景颜色
        frame.setBackground(new Color(180, 48, 48));
        //窗口弹出的初始位置
        frame.setLocation(200,200);
        //设置窗口大小固定
        frame.setResizable(false);
        
    }
}
  • 发现窗口关闭不掉,停止java程序。
  • 使用封装
package com.gui;

import java.awt.*;


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

        //设置窗口大小、窗口弹出的初始位置
        setBounds(x, y, w, h);
        //设置背景颜色
        setBackground(color);
        //设置可见性
        setVisible(true);
    }

}
  • Panel
  • 通过panel将窗口进行划分,对frame进行布局
package com.gui;

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

//Panel 可以看成一个空间,但是不能单独存在  Panel需要放到frame上
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(69, 206, 63));
        //panel设置坐标,相对于frame
        panel.setBounds(50,50,400,400);
        panel.setBackground(new Color(205, 39, 25));
        //frame.add(panel)
        frame.add(panel);
        //设置可见性
        frame.setVisible(true);
        //监听事件,监听窗口关闭事件 SYstem。exit(0) 解决无法关闭
        //适配器模式:
        frame.addWindowListener(new WindowAdapter() {
            //点击窗口关闭的时候需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }
}

2.3布局管理器

  • FlowLayout(流式布局)
package com.gui;

import java.awt.*;

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());默认居中
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));

        frame.setSize(200,200);
        //添加按钮
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);

        frame.setVisible(true);
    }

}
  • BorderLayout(默认布局)
package com.gui;

import java.awt.*;

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);
    }
}
  • GridLayout(表格布局)
package com.gui;
import java.awt.*;
public class TestGridLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("TestGridLayout");
        Button btn1 = new Button("btn1");
        Button btn2 = new Button("btn2");
        Button btn3 = new Button("btn3");
        Button btn4 = new Button("btn4");
        Button btn5 = new Button("btn5");
        Button btn6 = new Button("btn6");

        frame.setLayout(new GridLayout(3,2));
        frame.add(btn1);
        frame.add(btn2);
        frame.add(btn3);
        frame.add(btn4);
        frame.add(btn5);
        frame.add(btn6);

        frame.pack();//Java函数  自动选择最优位置
        frame.setVisible(true);
    }
}

2.4布局练习

实现以下界面:

javafx tabpane 背景颜色 javafx设置面板背景颜色_java

package com.gui;

import java.awt.*;

public class ExerciseLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setLayout(new GridLayout(2,1));
        //设置窗口大小
        frame.setSize(400,400);
        //需要使用到的画布  在frame的基础上划分两个界面
        Panel top = new Panel(new BorderLayout());
        Panel topMiddle = new Panel(new GridLayout(2,1));
        Panel bottom = new Panel(new BorderLayout());
        Panel bottomMiddle = new Panel(new GridLayout(2,2));
        //向画布中添加元素
        top.add(new Button("T-left"),BorderLayout.EAST);
        top.add(new Button("T-right"),BorderLayout.WEST);
        top.add(topMiddle,BorderLayout.CENTER);
        topMiddle.add(new Button("TMid-top"));
        topMiddle.add(new Button("TMid-bottom"));

        bottom.add(new Button("B-left"),BorderLayout.EAST);
        bottom.add(new Button("B-right"),BorderLayout.WEST);
        bottom.add(bottomMiddle,BorderLayout.CENTER);
        bottomMiddle.add(new Button("bMid-top1"));
        bottomMiddle.add(new Button("bMid-top2"));
        bottomMiddle.add(new Button("bMid-bottom1"));
        bottomMiddle.add(new Button("bMid-bottom2"));
        //添加画布到容器
        frame.add(top);
        frame.add(bottom);
        //设置窗口大小固定
        frame.setResizable(false);
        //设置可见
        frame.setVisible(true);

    }
}

javafx tabpane 背景颜色 javafx设置面板背景颜色_java_02

2.5事件监听

package com.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 button = new Button("Test");
// 因为.addActionListener();需要一个ActionListener 所以我们需要构造一个ActionListener
        MyActionLinster myActionLinster = new MyActionLinster();
        button.addActionListener(myActionLinster);

        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  MyActionLinster implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("aaa");
    }
}
  • 多个按钮公用一个监听器
package com.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 TestActionEvent02 {
    public static void main(String[] args) {
        //按下按钮触发一些事件
        Frame frame = new Frame();
        Button button1 = new Button("start");
        Button button2 = new Button("stop");
        //设置按钮的参数  默认为按钮名称
        button2.setActionCommand("b-stop");
        MyMonitor myMonitor = new MyMonitor();
        button1.addActionListener(myMonitor);
        button2.addActionListener(myMonitor);

        frame.add(button1,BorderLayout.WEST);
        frame.add(button2,BorderLayout.EAST);
        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  MyMonitor implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("按钮被触发--"+e.getActionCommand());
        if(e.getActionCommand().equals("start")){
            System.out.println("开始");
        }else{
            System.out.println("结束");
        }
    }
}

2.6输入框TextFiled

package com.gui;

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

public class TestTextField {
    public static void main(String[] args) {
        new MyFrame02();
    }
}
class MyFrame02 extends Frame{
    public MyFrame02(){
        TextField textField = new TextField();
        add(textField);
        MyActionListener02 myActionListener02 = new MyActionListener02();
        //按下enter  触发输入框事件
        textField.addActionListener(myActionListener02);

        //设置替换编码
        textField.setEchoChar('*');
        setVisible(true);
        pack();
    }
}
class MyActionListener02 implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        TextField field=(TextField)e.getSource();
        System.out.println(field.getText());
        //设置文本框为空
        field.setText("");
    }
}

简易计算器,组合+内部类 (oop原则:组合大于继承!)

未优化代码

package com.gui;

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

public class ExerciseCalculator {
    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("=");
        //一个标签
        Label label = new Label("+");
        //为按钮绑定事件
        button.addActionListener(new MyCalculatorActionListener(num1,num2,num3));
        //布局
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);

    }
}
//监听器类
class MyCalculatorActionListener implements ActionListener{
    private TextField num1;
    private TextField num2;
    private TextField num3;
    //获取变量
    public MyCalculatorActionListener(TextField num1,TextField num2,TextField num3){
        this.num1=num1;
        this.num2=num2;
        this.num3=num3;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //获取要计算的数据
        int n1=Integer.parseInt(num1.getText());
        int n2=Integer.parseInt(num2.getText());
        //计算并放入num3中
        num3.setText(""+(n1+n2));
        //清楚前两个框中的数据
        num1.setText("");
        num2.setText("");
    }
}

优化代码

package com.gui;

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

public class ExerciseCalculator {
    public static void main(String[] args) {
        new Calculator().Loading();
    }
}
//计算器类
class Calculator extends Frame{
    //属性
    TextField num1,num2,num3;
    //方法
    public void Loading() {
        //三个文本框
        num1 = new TextField(10);//字符数
        num2 = new TextField(10);
        num3 = new TextField(20);
        Button button = new Button("=");
        Label label = new Label("+");
        //为按钮绑定事件
        button.addActionListener(new MyCalculatorActionListener(this));
        //布局
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);
    }
}
//监听器类
class MyCalculatorActionListener implements ActionListener{
    Calculator calculator=null;
    //获取变量
    public MyCalculatorActionListener(Calculator calculator){
       this.calculator=calculator;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //获取要计算的数据
        int n1=Integer.parseInt(calculator.num1.getText());
        int n2=Integer.parseInt(calculator.num2.getText());
        //计算并放入num3中
        calculator.num3.setText(""+(n1+n2));
        //清楚前两个框中的数据
        calculator.num1.setText("");
        calculator.num2.setText("");
    }
}

完全改造面对对象

package com.gui;

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

public class ExerciseCalculator {
    public static void main(String[] args) {
        new Calculator().Loading();
    }
}
//计算器类
class Calculator extends Frame implements ActionListener{
    //属性
    TextField num1,num2,num3;
    //方法
    public void Loading() {
        //三个文本框
        num1 = new TextField(10);//字符数
        num2 = new TextField(10);
        num3 = new TextField(20);
        Button button = new Button("=");
        Label label = new Label("+");
        //为按钮绑定事件
        button.addActionListener(this);
        //布局
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //获取要计算的数据
        int n1=Integer.parseInt(num1.getText());
        int n2=Integer.parseInt(num2.getText());
        //计算并放入num3中
        num3.setText(""+(n1+n2));
        //清除前两个框中的数据
        num1.setText("");
        num2.setText("");
    }
}

2.7画笔Paint

package com.gui;

import java.awt.*;

public class TestPaint {
    public static void main(String[] args) {
        new MyPaint().Loading();
    }
}
class MyPaint extends Frame{
    public void Loading(){
        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(100,100,50,50);
        
        
    }
}

2.8鼠标监听

目标:实现简易画图------鼠标每次点击都会在窗口上一个点

package com.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 MyMouse("画图");
    }
}

class MyMouse extends Frame {
    ArrayList points;
    public  MyMouse(String title){
        super(title);
        setBounds(200,200,400,400);
        //存鼠标点击的位置
        points=new ArrayList<>();
        //鼠标监听器  针对整个窗口
        this.addMouseListener(new MyMouseListener());
        setVisible(true);
    }

    @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);
        }
    }
    //继承MouseListener需要实现这个接口中的所有方法 我们只用到其中的按压方法故选择适配器模式
    private  class  MyMouseListener extends MouseAdapter{
        @Override
        public void mousePressed(MouseEvent e) {
            MyMouse myMouse= (MyMouse) e.getSource();//返回当前对象----返回MyMouse(Frame) 用于获取鼠标资源
            //当鼠标点击时获得这个点的位置
            points.add(new Point(e.getX(),e.getY()));
            //鼠标点击后  画笔 重新画图 
            myMouse.repaint();
        }
    }

}

javafx tabpane 背景颜色 javafx设置面板背景颜色_javafx tabpane 背景颜色_03

2.9窗口监听

package com.gui;

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

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

    }
}
class MyWindow extends Frame{
    public MyWindow()  {
        setBackground(Color.BLUE);
        setBounds(100,100,200,200);
        setVisible(true);

        this.addWindowListener(
                //匿名内部类
                new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("你点击了关闭");
                System.exit(0);//正常退出
            }
        });
    }
}

2.10键盘监听

package com.gui;

import javafx.animation.KeyFrame;

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

public class TestKeyListener {
    public static void main(String[] args) {
        new keyFrame();
    }
}
class keyFrame extends Frame{
    public keyFrame() {
        setBackground(Color.BLUE);
        setBounds(100,100,400,400);
        setVisible(true);
        this.addKeyListener(new KeyAdapter() {
            //键盘按压事件
            @Override
            public void keyPressed(KeyEvent e) {
                //按压键盘后 获得对应的键码值
                int keyCode= e.getKeyCode();
                //根据不同的键码值,产生不同的操作
                if(keyCode==KeyEvent.VK_UP){
                    System.out.println("按下了上键,值为:"+keyCode);
                    setBackground(Color.red);
                }
                else if (keyCode==KeyEvent.VK_DOWN){
                    System.out.println("按下了下键,值为:"+keyCode);
                    setBackground(Color.BLUE);
                }

            }
        });
    }
}

3、Swing

3.1、窗口、面板

package com.gui.swing;

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

public class JFrameDemo {
    //init() 初始化
    public void init(){
        JFrame jf = new JFrame("这是一个JFrame窗口");
        jf.setVisible(true);
        jf.setBounds(100,100,200,200);
        jf.setBackground(Color.green);//无法显示背景颜色
        //设置文字JLabel
        JLabel label = new JLabel("欢迎来到祖安");
        //文本标签居中  设置水平对齐
        label.setHorizontalAlignment(SwingConstants.CENTER);
        jf.add(label);

        //背景颜色无法显示:  容器显示实例化
        Container contentPane = jf.getContentPane();
        contentPane.setBackground(Color.red);
        //关闭事件
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        //建议一个方法
        new JFrameDemo().init();
    }
}

3.2弹窗

package com.gui.swing;

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

//主窗口
public class DialogDemo extends JFrame {
    public DialogDemo() {
        this.setVisible(true);
        this.setSize(700,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //JFrame放元素,容器
        Container container = this.getContentPane();
        //绝对布局
        container.setLayout(null);
        //按钮
        JButton button = new JButton("点击弹窗");
        button.setBounds(30,30,200,50);

        container.add(button);
        //点击按钮 触发监听事件  弹出一个弹窗
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyDialogDemo();
            }
        });
    }

    public static void main(String[] args) {
        new DialogDemo();
    }
}
//弹窗的窗口
class MyDialogDemo extends JDialog{
    public MyDialogDemo() {
        this.setVisible(true);
        this.setBounds(100,100,500,500);
//        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container container = this.getContentPane();
        container.setLayout(null);
        JLabel label = new JLabel("Test");
        label.setBounds(20,20,100,100);
        container.add(label);
    }
}

3.3、标签

  • label
  • 标签主要用于展示 文本图片
  • Icon
  • 在Swing中通过Icon接口来创建图标,可以在创建时 指定图标的大小、颜色等特性
  • 图标放在标签上,也可以放在按钮上
  • 需要实现类
package com.gui.swing;

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

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

    private int width;
    private  int height;

    public IconDemo()   {
    }

    public IconDemo(int width, int height)  {

        this.width = width;
        this.height=height;
    }
    public  void  init(){
        this.setSize(500,500);

        IconDemo iconDemo = new IconDemo(30, 30);

        //图标放在标签上,也可以放在按钮上
        JLabel label = new JLabel("icontest", iconDemo, SwingConstants.CENTER);
        label.setBounds(10,10,200,220);

        Container container = getContentPane();
        container.add(label);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new IconDemo().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;
    }
}
  • ImageIcon
package com.gui.swing;

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

public class ImageIconDemo extends JFrame{
    public ImageIconDemo(){
        //获取图片的地址
        JLabel label=new JLabel("ImageIcon");
        //获取IconDemo这个类 同级资源下的 icon01.jpg
        URL url = ImageIconDemo.class.getResource("tx.jpg");
        System.out.println(url);
        ImageIcon imageIcon = new ImageIcon(url);
        label.setIcon(imageIcon);
        label.setHorizontalAlignment(SwingConstants.CENTER);

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

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

    }
}

3.4、面板

  • JPanel
package com.gui.swing;

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

public class TestJPanel extends JFrame {

    public TestJPanel()   {
        //所有操作都放在面板上进行操作
        // 1.获得Container(容器)
        Container container = this.getContentPane();
        //2.设置容器的样子
        container.setLayout(new GridLayout(2,1,10,10));//两行一列上下间距为10
        //创建面板
        JPanel jPanel1 = new JPanel(new GridLayout(1,3));
        JPanel jPanel2 = new JPanel(new GridLayout(1,2));
        JPanel jPanel3 = new JPanel(new GridLayout(2,2));
        jPanel1.add(new Button("1"));
        jPanel1.add(new Button("1"));
        jPanel1.add(new Button("1"));
        jPanel2.add(new Button("2"));
        jPanel2.add(new Button("2"));
        jPanel3.add(new Button("3"));
        jPanel3.add(new Button("3"));
        jPanel3.add(new Button("3"));
        jPanel3.add(new Button("3"));

        container.add(jPanel1);
        container.add(jPanel2);
        container.add(jPanel3);
        this.setSize(500,400);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        new TestJPanel();
    }
}
  • JScroll
package com.gui.swing;

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

public class TestJScroll extends JFrame {
    public TestJScroll()   {
        Container container = this.getContentPane();

        //文本域
        JTextArea jTextArea = new JTextArea(20,50);
        //设置默认文本
        jTextArea.setText("春风秋月冬雪风,留着残荷听雨声,冷眼看浮生如梦烟雨化飞龙。");
        //Scroll面板
        JScrollPane scrollPane = new JScrollPane(jTextArea);
        container.add(scrollPane);
        this.setBounds(100,100,300,500);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

3.5、按钮

  • 图片按钮
package com.gui.swing;

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

public class TestJButton01 extends JFrame {
    public TestJButton01()   {
        Container container = this.getContentPane();
        //将一个图片变为图标
        URL resource = TestJButton01.class.getResource("tx.jpg");
        Icon icon = new ImageIcon(resource);
        //把这个图标放在按钮上
        JButton jButton = new JButton();
        jButton.setIcon(icon);
        //提示文本  当鼠标放上去生效
        jButton.setToolTipText("成果");

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

    public static void main(String[] args) {
        new TestJButton01();
    }
}
  • 单选按钮
package com.gui.swing;

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

public class TestJButton02 extends JFrame {
    public TestJButton02()   {
        Container container = this.getContentPane();

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

        //由于单选框只能选择一个,采用分组方式
        ButtonGroup group = new ButtonGroup();
        group.add(radioButton1);
        group.add(radioButton2);
        group.add(radioButton3);

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

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

    public static void main(String[] args) {
        new TestJButton02();
    }
}
  • 多选按钮
package com.gui.swing;

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

public class TestJButton03 extends JFrame {
    public TestJButton03()   {
        Container container = this.getContentPane();

        //多选框
        JCheckBox checkBox01 = new JCheckBox("1");
        JCheckBox checkBox02 = new JCheckBox("2");
        JCheckBox checkBox03 = new JCheckBox("3");

        container.add(checkBox01,BorderLayout.NORTH);
        container.add(checkBox02,BorderLayout.CENTER);
        container.add(checkBox03,BorderLayout.SOUTH);

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

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

3.6、列表

  • 下拉框
package com.gui.swing;

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

public class TestCombobox extends JFrame {
    public TestCombobox()   {
        Container container = this.getContentPane();

        JComboBox status = new JComboBox();

        status.addItem(null);
        status.addItem("正在热映");
        status.addItem("已下架");
        status.addItem("即将上映");
        
        container.add(status);

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

    public static void main(String[] args) {
        new TestCombobox();
    }
}
  • 列表框
package com.gui.swing;

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

public class TestCombobox02 extends  JFrame{
    public TestCombobox02()   {
        Container container = this.getContentPane();

        //生成列表的内容
//        String[] contents={"1","2","3"};
        Vector contents = new Vector();
        //列表中需要放入内容
        JList jList = new JList(contents);

        contents.add("张三");
        contents.add("李四");
        contents.add("王五");
        container.add(jList);

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

    public static void main(String[] args) {
        new TestCombobox02();
    }
}
  • 应用场景
  • 选择地区,或者一些单个选项
  • 列表,展示信息,一般式动态扩容

3.7、文本框

  • 文本框
package com.gui.swing;

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

public class TestTextDemo01 extends JFrame {
    public TestTextDemo01()   {
        Container container = this.getContentPane();

        JTextField textField = new JTextField("Hello");
        JTextField textField2 = new JTextField("Word");

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

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

    public static void main(String[] args) {
        new TestTextDemo01();
    }
}
  • 密码框
package com.gui.swing;

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

public class TestTextDemo02 extends JFrame {
    public TestTextDemo02()   {
        Container container = this.getContentPane();

        JPasswordField passwordField = new JPasswordField();
        passwordField.setEchoChar('*');

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

    public static void main(String[] args) {
        new TestTextDemo02();
    }
}
  • 文本域
package com.gui.swing;

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

public class TestTextDemo03 extends JFrame {
    public TestTextDemo03()   {
        Container container = this.getContentPane();
        //文本域
        JTextArea jTextArea = new JTextArea(20,50);
        //设置默认文本
        jTextArea.setText("春风秋月冬雪风,留着残荷听雨声,冷眼看浮生如梦烟雨化飞龙。");
        //Scroll面板
        JScrollPane scrollPane = new JScrollPane(jTextArea);
        container.add(scrollPane);
        this.setBounds(100,100,300,500);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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