Java GUI编程之AWT和Swing

目的:为了了解MVC架构,了解监听,我们有必要学习Java GUI 编程。

一. AWT:抽象窗口工具箱。

1.弹出窗口实例:

package GUI;

import java.awt.*;

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

        //设置标题
        Frame myFrame = new Frame("myFrame");
        //设置可见性
        myFrame.setVisible(true);
        //设置窗口大小
        myFrame.setSize(400,400);
        //设置背景颜色
        myFrame.setBackground(new Color(214,214,214));
        //弹出窗口的初始位置
        //原点在左上角
        myFrame.setLocation(400,500);
        //设置大小固定,默认为true
        myFrame.setResizable(false);
    }
}

运行结果:
在这里插入图片描述
2.面板:panel,不能单独存在,必须存在于frame里面。

package GUI;

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

/**
 * panel:面板,不能单独存在,必须存在于frame里面
 */
public class TestPanel {
    public static void main(String[] args) {
        //弹出框
        Frame frame = new Frame("测试Panel");
        //面板
        Panel panel = new Panel();
        //设置使用布局(如果不设置,布局则没有效果)
        frame.setLayout(null);

        //窗口坐标和长宽(前两位为坐标)
        frame.setBounds(100,100,400,400);
        //窗口颜色
        frame.setBackground(new Color(64,151,61));

        //panel坐标和长宽
        panel.setBounds(50,50,300,300);
        //panel颜色
        panel.setBackground(Color.BLUE);

        //把panel添加到frame中
        frame.add(panel);
        //设置可见性
        frame.setVisible(true);

        //监听窗口关闭事件
        frame.addWindowListener(new WindowAdapter() {
            //点击窗口关闭按钮需要触发的事件
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);// 1为异常退出,0为正常退出
            }
        });
    }
}

运行结果:
在这里插入图片描述
3.三种布局管理器:流式布局、东西南北中、表格布局。

流式布局实例:

package 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(500,500);

        //添加按钮到布局中
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);

        frame.setVisible(true);
    }
}

运行结果:
在这里插入图片描述
“东西南北中”布局实例:

package 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(200,200);
        frame.setVisible(true);
    }
}

运行结果:
在这里插入图片描述
表格布局实例:

package GUI;

import java.awt.*;

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));

        //设置三行两列的表格布局(网格之间有空隙)
        //hgap:水平间隙;vgap:垂直间隙
        frame.setLayout(new GridLayout(3,2,5,5));

        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);
        frame.add(button6);

        frame.setSize(400,400);

        //不用frame.setSize(),frame.pack()会自动适应布局
        //frame.pack();
        frame.setVisible(true);
    }
}

运行结果:
在这里插入图片描述
布局练习:

package GUI;

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

public class TestDemo {
    public static void main(String[] args) {
        //窗体名称
        Frame frame = new Frame("testDemo");
        //窗体大小
        frame.setSize(500,500);
        //窗体初始位置(原点在页面左上角)
        frame.setLocation(300,300);
        //设置窗体总体布局为两行一列的表格
        frame.setLayout(new GridLayout(2,1));
        //设置颜色
        //frame.setBackground(Color.BLUE);
        //显示窗体
        frame.setVisible(true);

        //四个内嵌布局的面板
        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, 1));

        //将组件添加到面板中
        panel1.add(new Button("button1"),BorderLayout.WEST);
        panel1.add(new Button("button2"),BorderLayout.EAST);

        panel2.add(new Button("button3"));
        panel2.add(new Button("button4"));

        //将panel2添加到panel1中
        panel1.add(panel2,BorderLayout.CENTER);

        //将组件添加到面板中
        panel3.add(new Button("button5"),BorderLayout.WEST);
        panel3.add(new Button("button6"),BorderLayout.EAST);

        //将组件添加到面板中
        panel4.add(new Button("button7"));
        panel4.add(new Button("button8"));
        panel4.add(new Button("button9"));
        panel4.add(new Button("button10"));

        //将panel4添加到panel3中
        panel3.add(panel4,BorderLayout.CENTER);

        //将面板添加到frame中
        frame.add(panel1);
        frame.add(panel3);

        //监听事件,点击右上角X关闭frame
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

运行结果:
在这里插入图片描述
4.事件监听实例:

package GUI;

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

public class TestListener {
    public static void main(String[] args) {
        //设置窗体
        Frame frame = new Frame("TestListener");
        frame.setSize(300,300);
        frame.setLocation(200,200);
        frame.setVisible(true);

        //两个按钮
        Button button1 = new Button("button1");
        Button button2 = new Button("button2");

        //设置按钮布局
        frame.add(button1,BorderLayout.EAST);
        frame.add(button2,BorderLayout.WEST);

        //setActionCommand()为动作命名
        button1.setActionCommand("button1");
        button2.setActionCommand("button2");

        //设置监听
        MyListener myListener = new MyListener();
        button1.addActionListener(myListener);
        button2.addActionListener(myListener);
    }

    /**
     * 自定义监听类
     */
    public static class MyListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {

            //getActionCommand()判断动作
            if (e.getActionCommand().equals("button1")){
                System.out.println("点击了按钮button1");
            }else if(e.getActionCommand().equals("button2")){
                System.out.println("点击了按钮button2");
            }
        }
    }
}

运行结果:
在这里插入图片描述
分别点击button1和button2,控制台打印:
在这里插入图片描述
5.文本输入域监听:

package GUI;

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

public class TestTextField {
    public static void main(String[] args) {
        //启动
        new MyFrame();
    }
}


/**
 * 自定义一个Frame类,继承Frame
 */
class MyFrame extends Frame{

    public MyFrame(){
        //单行文本域
        TextField textField = new TextField();
        //将输入框添加到frame中
        add(textField);

        //实例化自定义的监听器
       MyListener myListener = new MyListener();
        //将文本框设置自定义的监听
        //回车键触发
        textField.addActionListener(myListener);

        //设置frame大小
        setSize(300,300);
        //设置frame可见性
        setVisible(true);
    }
}

/**
 * 自定义监听器
 */
class MyListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        //获得资源,返回对象
        TextField textField = (TextField) e.getSource();
        //获得输入框中的文本
        System.out.println(textField.getText());
    }
}

运行结果:
在这里插入图片描述
设置密码形式的输入:页面不能看到,但是后台可以获取,保证数据安全性

textField.setEchoChar('*');

在这里插入图片描述
设置触发监听后,文本输入域置为空:
在这里插入图片描述

6.加法计算器案例:

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 TestCalculator {
    public static void main(String[] args) {
        new Calculator();
    }
}

// 计算器类
class Calculator extends Frame{
    public Calculator(){

        // 三个文本框
        // 10代表可以存放的最大字符数
        TextField textField1 = new TextField(10);
        TextField textField2 = new TextField(10);
        TextField textField3 = new TextField(30);

        // 一个按钮
        Button button = new Button("=");

        // 一个标签
        Label label = new Label("+");

        // 使用流式布局
        setLayout(new FlowLayout());
        // 在布局中添加组件
        add(textField1);
        add(label);
        add(textField2);
        add(button);
        add(textField3);

        // 设置Frame(一定要放在组件的最后,否则自适应会失效)
        setTitle("加法计算器");
        // 布局自适应Frame
        pack();
        // 设置可见性
        setVisible(true);

        // 配置组件监听
        button.addActionListener(new MyCalculatorListener(textField1,textField2,textField3));

        //关闭Frame监听
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);//程序结束运行
            }
        });
    }
}

// 监听器类
class MyCalculatorListener implements ActionListener{

    //获取三个变量
    private TextField textField1;
    private TextField textField2;
    private TextField textField3;

    public MyCalculatorListener(TextField textField1, TextField textField2, TextField textField3) {
        this.textField1 = textField1;
        this.textField2 = textField2;
        this.textField3 = textField3;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("=")){
            //将两个数相加
            int res = Integer.parseInt(textField1.getText())+Integer.parseInt(textField2.getText());
            //将相加结果放在第三个文本域中
            textField3.setText(""+res);
        }
    }
}

使用面向对象的思想优化代码:

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 TestCalculator {
    public static void main(String[] args) {
        new Calculator().loadFrame();
    }
}

// 计算器类
class Calculator extends Frame{
	// 属性
    // 三个文本框
    TextField textField1;
    TextField textField2;
    TextField textField3;
    
	// 方法
    public void loadFrame(){
        // 10代表可以存放的最大字符数
        textField1 = new TextField(10);
        textField2 = new TextField(10);
        textField3 = new TextField(30);

        // 一个按钮
        Button button = new Button("=");

        // 一个标签
        Label label = new Label("+");

        // 使用流式布局
        setLayout(new FlowLayout());
        // 在布局中添加组件
        add(textField1);
        add(label);
        add(textField2);
        add(button);
        add(textField3);

        // 设置Frame(一定要放在组件的最后,否则自适应会失效)
        setTitle("加法计算器");
        // 布局自适应Frame
        pack();
        // 设置可见性
        setVisible(true);

        // 配置组件监听
        button.addActionListener(new MyCalculatorListener(this));

        //关闭Frame监听
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);//程序结束运行
            }
        });
    }
    }

// 监听器类
class MyCalculatorListener implements ActionListener{

    Calculator calculator = null;

    public MyCalculatorListener(Calculator calculator) {
        this.calculator = calculator;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("=")){
            //将两个数相加
            int res = Integer.parseInt(calculator.textField1.getText())+Integer.parseInt(calculator.textField2.getText());
            //将相加结果放在第三个文本域中
            calculator.textField3.setText(""+res);
        }
    }
}

使用内部类优化代码:内部类可以随意访问外部类的属性和方法,降低了编码成本。

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 TestCalculator {
    public static void main(String[] args) {
        new Calculator().loadFrame();
    }
}

// 计算器类
class Calculator extends Frame{
    // 三个文本框
    TextField textField1;
    TextField textField2;
    TextField textField3;

    public void loadFrame(){
        // 10代表可以存放的最大字符数
        textField1 = new TextField(10);
        textField2 = new TextField(10);
        textField3 = new TextField(30);

        // 一个按钮
        Button button = new Button("=");

        // 一个标签
        Label label = new Label("+");

        // 使用流式布局
        setLayout(new FlowLayout());
        // 在布局中添加组件
        add(textField1);
        add(label);
        add(textField2);
        add(button);
        add(textField3);

        // 设置Frame(一定要放在组件的最后,否则自适应会失效)
        setTitle("加法计算器");
        // 布局自适应Frame
        pack();
        // 设置可见性
        setVisible(true);

        // 配置组件监听
        button.addActionListener(new MyCalculatorListener());

        //关闭Frame
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);//程序结束运行
            }
        });
    }

    // 监听器内部类
    private class MyCalculatorListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getActionCommand().equals("=")){
                //将两个数相加
                int res = Integer.parseInt(textField1.getText())+Integer.parseInt(textField2.getText());
                //将相加结果放在第三个文本域中
                textField3.setText(""+res);
            }
        }
    }
}



运行结果:
在这里插入图片描述
7.画笔:

package GUI;

import java.awt.*;

public class TestPaint {
    public static void main(String[] args) {
        new MyPaint().loadFrame();
    }
}

class MyPaint extends Frame{

    public void loadFrame(){
        // 设置Frame坐标和大小
        setBounds(200,200,600,500);
        // 设置可见性
        setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
        g.setColor(Color.red);
        //空心圆
        g.drawOval(100,100,100,100);
        //实心圆
        g.fillOval(200,200,100,100);
        g.setColor(Color.BLUE);
        //矩形
        g.fillRect(300,300,100,100);
    }
}

运行结果:
在这里插入图片描述
8.鼠标监听:

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 TestMouse {
    public static void main(String[] args) {
        //new Frame("画笔-鼠标监听");
        new MyMouse("画笔-鼠标监听");
    }
}

class MyMouse extends Frame{

    ArrayList points;// 存鼠标点击的点

    public MyMouse(String title){
        // 设置Frame的名称
        super(title);
        setBounds(200,200,600,500);
        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) {
            MyMouse myMouse = (MyMouse) e.getSource();
            // 获取当前鼠标点击点的坐标
            myMouse.addPaint(new Point(e.getX(),e.getY()));
            //每次点击鼠标都要重画一遍(刷新)
            myMouse.repaint();
        }
    }
}

运行结果:
在这里插入图片描述
9.窗口监听:

package GUI;

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(String title){
       super(title);
        setBounds(100,100,200,200);
        setVisible(true);
       // addWindowListener(new MyWindowListener());
       this.addWindowListener(
           //匿名内部类
           new WindowAdapter() {
               // 关闭窗口
               @Override
               public void windowClosing(WindowEvent e) {
                   System.out.println("windowClosing");
               }
               // 激活窗口
               @Override
               public void windowActivated(WindowEvent e) {
                   // WindowFrame windowFrame = (WindowFrame) e.getSource();
                   // windowFrame.setTitle("被激活了");
                   System.out.println("windowActivated");
                   setTitle("被激活了");
               }
           });
    }
}

10.键盘监听:

package GUI;

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(String title){
        //Frame设置
        super(title);
        setBounds(100,100,200,200);
        setVisible(true);
        //监听事件
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int keyCode = e.getKeyCode();
                if (keyCode==KeyEvent.VK_UP){
                    System.out.println("您按了上键!");
                }
            }
        });
    }
}

运行结果:
在这里插入图片描述
二. Swing:
是一个为Java设计的GUI工具包;Swing提供许多比AWT更好的屏幕显示元素。

1.JFrame窗体:

package GUI.Swing;

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

public class TestJFrame extends JFrame{
    public static void main(String[] args) {
        new TestJFrame().init();
    }

    //自定义初始化方法,贴近真实项目的游戏开发
    public void init(){
        setTitle("测试JFrame");
        setBounds(100,100,200,200);
        setVisible(true);
        //Swing必须在容器中设置颜色,否则不能生效
        Container contentPane = getContentPane();
        contentPane.setBackground(Color.BLUE);
        JLabel label = new JLabel("this is 一段文字");
        //设置label水平居中
        label.setHorizontalAlignment(SwingConstants.CENTER);
        contentPane.add(label);
        //点击X按钮触发关闭事件
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

运行结果:
在这里插入图片描述

2.弹窗:

package GUI.Swing;

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

public class TestDialog extends JFrame{
    public TestDialog(String title){
        //窗口title
        super(title);
        //Frame基本设置
        setVisible(true);
        setSize(700,500);
        //关闭Frame事件
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //Swing编程需要容器
        Container contentPane = getContentPane();
        //绝对布局
        contentPane.setLayout(null);
        //按钮
        JButton jButton = new JButton("弹出框");
        //使用了绝对布局
        jButton.setBounds(30,30,30,30);
        jButton.setSize(100,100);
        //按钮监听事件
        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(){
        /**
         * 弹窗默认有关闭弹窗监听事件,不需要手动添加
         */
        setVisible(true);
        setBounds(100,100,100,100);
        JLabel jLabel = new JLabel("我是一个弹窗");
        //容器
        Container contentPane = getContentPane();
        contentPane.add(jLabel);
    }
}

运行结果:
在这里插入图片描述
3.图标:

实例1:

package GUI.Swing;

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

public class TestIcon extends JFrame implements Icon{
    private int width;
    private int height;

    public TestIcon(){
    }

    public TestIcon(int width, int height){
        this.width = width;
        this.height = height;
    }

    public void init(){
        /*
         *窗口基本设置
         */
        setVisible(true);
        setSize(200,200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //新建一个图标,初始化width和height
        TestIcon testIcon = new TestIcon(20, 20);
        //将图标加到标签上
        JLabel jLabel = new JLabel("TestIcon", testIcon, SwingConstants.CENTER);
        //Swing编程需要容器
        Container contentPane = getContentPane();
        contentPane.add(jLabel);
    }
    @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;
    }

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

运行结果:
在这里插入图片描述
实例2:

package GUI.Swing;

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

public class TestImageIcon extends JFrame{

    public TestImageIcon(){
        /*
         *窗口基本设置
         */
        setVisible(true);
        setSize(200,200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //获得当前类同级目录的资源
        URL url = TestImageIcon.class.getResource("pp.png");
        //System.out.println(url);
        //标签
        JLabel jLabel = new JLabel("TestImageIcon");
        //图片
        ImageIcon imageIcon = new ImageIcon(url);
        //把图片放到标签上
        jLabel.setIcon(imageIcon);
        //标签居中
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        //容器
        Container contentPane = getContentPane();
        contentPane.add(jLabel);
    }

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

运行结果:
在这里插入图片描述
4.面板:

package GUI.Swing;

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

public class TestPanel extends JFrame{

    public TestPanel(){
        /*
         *窗口基本设置
         */
        setVisible(true);
        setSize(200,200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //容器
        Container contentPane = getContentPane();
        //表格布局
        contentPane.setLayout(new GridLayout(2,1,10,10));

        //面板中添加一行三列的表格
        JPanel jPanel = new JPanel(new GridLayout(1, 3));
        //三个按钮
        jPanel.add(new JButton("JButton1"));
        jPanel.add(new JButton("JButton2"));
        jPanel.add(new JButton("JButton3"));
        contentPane.add(jPanel);
        contentPane.add(new JButton("我在最下面"));

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

运行结果:
在这里插入图片描述
5.滚动条:

package GUI.Swing;

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

public class TestScrollBar extends JFrame{

    public TestScrollBar(String title){
        super("TestScrollBar");
        /*
         *窗口基本设置
         */
        setVisible(true);
        setSize(200,200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //文本域
        JTextArea jTextArea = new JTextArea(20,30);
        jTextArea.setText("测试TestScrollBar!!!");

        //滚动条面板
        JScrollPane scrollPane = new JScrollPane(jTextArea);

        //容器
        Container contentPane = getContentPane();
        contentPane.add(scrollPane);
    }

    public static void main(String[] args) {
        new TestScrollBar("测试滚动条");
    }
}

运行结果:
在这里插入图片描述
6.图片按钮:

package GUI.Swing;

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

public class TestJButton extends JFrame{

    public TestJButton(){
        /*
         *窗口基本设置
         */
        setVisible(true);
        setSize(200,200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //将图片变成图标
        URL url = TestJButton.class.getResource("pp.png");
        Icon icon = new ImageIcon(url);
        //将图标添加到按钮上
        JButton jButton = new JButton();
        jButton.setIcon(icon);
        jButton.setToolTipText("图片按钮");

        //容器
        Container contentPane = getContentPane();
        contentPane.add(jButton);
    }
    public static void main(String[] args) {
       new TestJButton();
    }
}

运行结果:
在这里插入图片描述
7.单选框:

package GUI.Swing;

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

public class TestRadioButton extends JFrame{

    public TestRadioButton(){
        /*
         *窗口基本设置
         */
        setVisible(true);
        setSize(200,200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //三个单选框
        JRadioButton jRadioButton1 = new JRadioButton("篮球");
        JRadioButton jRadioButton2 = new JRadioButton("足球");
        JRadioButton jRadioButton3 = new JRadioButton("羽毛球");
        //一个组
        ButtonGroup buttonGroup = new ButtonGroup();
        //三个单选框必须在一个组中,因为三个单选框只能选择一个框
        buttonGroup.add(jRadioButton1);
        buttonGroup.add(jRadioButton2);
        buttonGroup.add(jRadioButton3);

        //容器
        Container contentPane = getContentPane();
        contentPane.add(jRadioButton1,BorderLayout.CENTER);
        contentPane.add(jRadioButton2,BorderLayout.NORTH);
        contentPane.add(jRadioButton3,BorderLayout.SOUTH);
    }

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

运行结果:
在这里插入图片描述
8.多选框:

package GUI.Swing;

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

public class TestCheckBox extends JFrame{

    public TestCheckBox(){
        /*
         *窗口基本设置
         */
        setVisible(true);
        setSize(200,200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //多选框
        JCheckBox checkBox1 = new JCheckBox("篮球");
        JCheckBox checkBox2 = new JCheckBox("足球");
        JCheckBox checkBox3 = new JCheckBox("乒乓球");

        //容器
        Container contentPane = getContentPane();
        contentPane.add(checkBox1,BorderLayout.CENTER);
        contentPane.add(checkBox2,BorderLayout.NORTH);
        contentPane.add(checkBox3,BorderLayout.SOUTH);
    }

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

运行结果:
在这里插入图片描述
9.下拉框:

package GUI.Swing;

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

public class TestComboBox extends JFrame{
    public TestComboBox(){
        /*
         *窗口基本设置
         */
        setVisible(true);
        setSize(200,200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //下拉框
        JComboBox<String> stringJComboBox = new JComboBox<>();
        //下拉框选项
        stringJComboBox.addItem("男");
        stringJComboBox.addItem("女");

        //容器
        Container contentPane = getContentPane();
        contentPane.add(stringJComboBox);

    }

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

运行结果:
在这里插入图片描述
10.列表框:

package GUI.Swing;

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

public class TestListBox extends JFrame{
    public TestListBox(){
        /*
         *窗口基本设置
         */
        setVisible(true);
        setSize(200,200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //列表内容
        //String[] lists = {"篮球","羽毛球","足球"};
        Vector strings = new Vector();
        strings.add("篮球");
        strings.add("羽毛球");
        strings.add("足球");

        //将内容放入列表
        JList jList = new JList(strings);

        //容器
        Container contentPane = getContentPane();
        contentPane.add(jList);
    }

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

运行结果:
在这里插入图片描述
11.文本框、密码框:

package GUI.Swing;

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

public class TestPasswordField extends JFrame{

    public TestPasswordField(){
        /*
         *窗口基本设置
         */
        setVisible(true);
        setSize(200,200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //文本框
        JTextField jTextField = new JTextField("文本框");

        //密码框
        JPasswordField jPasswordField = new JPasswordField("密码框");
        //也可以改变默认样式
        //jPasswordField.setEchoChar('*');
        
        //容器
        Container contentPane = getContentPane();
        contentPane.add(jTextField,BorderLayout.NORTH);
        contentPane.add(jPasswordField,BorderLayout.SOUTH);

    }

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

运行结果:
在这里插入图片描述
参考资料:

https://www.bilibili.com/video/BV1DJ411B75F?p=20
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值