14

GUI

组件:窗口、弹窗、面板、文本框、列表框、按钮、图片、监听事件、鼠标事件、键盘事件

1. 简介

GUI核心开发技术:Swing、AWT,不流行原因:①界面不美观;②需要JRE环境。

需要学习的原因:①可以写出自己心中想要的一些小工具;②工作时也可能需要维护到Swing界面;③了解MVC架构,了解监听。

2. AWT

2.1 AWT介绍

Abstract window tools:抽象窗口工具

包含了很多类和接口;元素(窗口、按钮、文本框);java.awt包。

2.2 组件和容器

2.2.1 Frame

 1 package com.gui.demo01;
 2  3 import java.awt.*;
 4  5 // GUI的第一个界面
 6 public class TestFrame {
 7     public static void main(String[] args) {
 8         Frame frame = new Frame("My first window");
 9 10         // 设置可见性
11         frame.setVisible(true);
12         // 设置窗口大小
13         frame.setSize(800,400);
14         // 设置背景颜色
15         frame.setBackground(new Color(196, 183, 133));
16         // 弹出的初始界面
17         frame.setLocation(200,200);
18         // 设置大小固定
19         frame.setResizable(false);
20     }
21 }

问题:窗口无法关闭,只能停止Java程序运行!

展示多个窗口,回顾封装:

 1 package com.gui.demo01;
 2  3 import java.awt.*;
 4  5 // 展示多个窗口
 6 public class TestFrame2 {
 7     public static void main(String[] args) {
 8         MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.BLUE);
 9         MyFrame myFrame2 = new MyFrame(100, 300, 200, 200, Color.CYAN);
10         MyFrame myFrame3 = new MyFrame(300, 100, 200, 200, Color.DARK_GRAY);
11         MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.GREEN);
12     }
13 }
14 15 class MyFrame extends Frame{
16     static int id = 0;
17     public MyFrame(int x, int y, int w, int h, Color color){
18         super("Myframe" + id);  // 可能存在多个窗口,需要一个计数器
19         setBounds(x, y, w, h);
20         setBackground(color);
21         setVisible(true);
22     }
23 }

2.2.2 面板Panel

解决了关闭事件:

 1 package com.gui.demo01;
 2  3 import java.awt.*;
 4 import java.awt.event.WindowAdapter;
 5 import java.awt.event.WindowEvent;
 6  7 // Panel可以看成一个空间,但是不能单独存在
 8 public class TestPanel {
 9     public static void main(String[] args) {
10         Frame frame = new Frame();
11         Panel panel = new Panel();
12 13         // 设置布局
14         frame.setLayout(null);
15 16         // 坐标
17         frame.setBounds(30,30,500,500);
18         frame.setBackground(new Color(96, 135, 79));
19 20         // Panel设置坐标,相对于frame
21         panel.setBounds(50,50,400,400);
22         panel.setBackground(new Color(120, 56, 56));
23 24         // frame.add();
25         frame.add(panel);
26 27         frame.setVisible(true);
28 29         // 监听事件,监听窗口关闭事件 System.exit(0)
30         // 适配器模式
31         frame.addWindowListener(new WindowAdapter() {
32             // 窗口点击关闭时需要做的事情
33             @Override
34             public void windowClosing(WindowEvent e) {
35                 // 结束程序
36                 System.exit(0);
37             }
38         });
39     }
40 }

2.2.3 布局管理器

  • 流式布局

 1 package com.gui.demo01;
 2  3 import java.awt.*;
 4  5 public class TestFlowLayout {
 6     public static void main(String[] args) {
 7         Frame frame = new Frame();
 8  9         // 组件-按钮
10         Button button1 = new Button("button1");
11         Button button2 = new Button("button2");
12         Button button3 = new Button("button3");
13 14         // 设置为流式布局
15         // frame.setLayout(new FlowLayout());  // 居中
16         // frame.setLayout(new FlowLayout(FlowLayout.LEFT));
17         frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
18 19         frame.setSize(200,200);
20 21         // 添加按钮
22         frame.add(button1);
23         frame.add(button2);
24         frame.add(button3);
25 26         frame.setVisible(true);
27     }
28 }
  • 东西南北中

 1 package com.gui.demo01;
 2  3 import java.awt.*;
 4  5 public class TestBorderLayout {
 6     public static void main(String[] args) {
 7         Frame frame = new Frame("BorderLayout");
 8  9         Button west = new Button("west");
10         Button east = new Button("east");
11         Button south = new Button("south");
12         Button north = new Button("north");
13         Button center = new Button("center");
14 15         frame.add(east, BorderLayout.EAST);
16         frame.add(west, BorderLayout.WEST);
17         frame.add(north, BorderLayout.NORTH);
18         frame.add(south, BorderLayout.SOUTH);
19         frame.add(center, BorderLayout.CENTER);
20 21         frame.setSize(200,200);
22         frame.setVisible(true);
23     }
24 }
  • 表格布局

 1 package com.gui.demo01;
 2  3 import java.awt.*;
 4  5 public class TestGridLayout {
 6     public static void main(String[] args) {
 7         Frame frame = new Frame();
 8  9         Button button1 = new Button("button1");
10         Button button2 = new Button("button2");
11         Button button3 = new Button("button3");
12         Button button4 = new Button("button4");
13         Button button5 = new Button("button5");
14         Button button6 = new Button("button6");
15 16         frame.setLayout(new GridLayout(3,2));
17         frame.add(button1);
18         frame.add(button2);
19         frame.add(button3);
20         frame.add(button4);
21         frame.add(button5);
22         frame.add(button6);
23 24         frame.pack();   // Java函数,自动填充
25         frame.setVisible(true);
26     }
27 }

练习

 

分析过程

  1. frame -> Grid

  2. 4个panel

    border:

    左:button

    中:panel

    右:button

代码实现:

 1 package com.gui.demo01;
 2  3 import java.awt.*;
 4  5 public class ExDemo {
 6     public static void main(String[] args) {
 7         // 总frame
 8         Frame frame = new Frame();
 9         frame.setSize(400,300);
10         frame.setLocation(300,400);
11         frame.setBackground(Color.BLACK);
12         frame.setVisible(true);
13         frame.setLayout(new GridLayout(2,1));
14 15         // 4个面板
16         Panel p1 = new Panel(new BorderLayout());
17         Panel p2 = new Panel(new GridLayout(2, 1));
18         Panel p3 = new Panel(new BorderLayout());
19         Panel p4 = new Panel(new GridLayout(2, 2));
20 21         //
22         p2.add(new Button("Button2"));
23         p2.add(new Button("Button3"));
24 25         p1.add(new Button("Button1"), BorderLayout.WEST);
26         p1.add(p2, BorderLayout.CENTER);
27         p1.add(new Button("Button4"), BorderLayout.EAST);
28 29         //
30         for (int i = 0; i < 4; i++) {
31             p4.add(new Button("Button" + (i+5)));
32         }
33 34         p3.add(new Button("Button5"), BorderLayout.WEST);
35         p3.add(p4, BorderLayout.CENTER);
36         p3.add(new Button("Button10"), BorderLayout.EAST);
37 38         frame.add(p1);
39         frame.add(p3);
40     }
41 }

总结

  • frame是一个顶级窗口;

  • panel无法单独显示,必须添加到某个容器中;

  • 布局管理器:流式、东西南北中、表格;

  • 大小、定位、背景颜色、可见性、监听。

2.2.4 事件监听

 1 package com.gui.demo02;
 2  3 import java.awt.*;
 4 import java.awt.event.ActionEvent;
 5 import java.awt.event.ActionListener;
 6 import java.awt.event.WindowAdapter;
 7 import java.awt.event.WindowEvent;
 8  9 public class TestActionEvent {
10     public static void main(String[] args) {
11         // 按下按钮,触发事件
12         Frame frame = new Frame();
13         Button button = new Button();
14 15         MyActionListener myActionListener = new MyActionListener();
16         button.addActionListener(myActionListener);
17 18         frame.add(button, BorderLayout.CENTER);
19         frame.pack();
20         windowClose(frame);     // 关闭窗口
21         frame.setVisible(true);
22     }
23 24     // 关闭窗体事件
25     private static void windowClose(Frame frame){
26         frame.addWindowListener(new WindowAdapter() {
27             @Override
28             public void windowClosing(WindowEvent e) {
29                 System.exit(0);
30             }
31         });
32     }
33 }
34 35 class MyActionListener implements ActionListener{
36 37     @Override
38     public void actionPerformed(ActionEvent e) {
39         System.out.println("Jungkook!");
40     }
41 }

多个按钮共用一个监听器:

 1 package com.gui.demo02;
 2 
 3 import java.awt.*;
 4 import java.awt.event.ActionEvent;
 5 import java.awt.event.ActionListener;
 6 
 7 public class TestActionEvent2 {
 8     public static void main(String[] args) {
 9         // 两个按钮,实现同一个监听
10         Frame frame = new Frame("Start-Stop");
11         Button button1 = new Button("start");
12         Button button2 = new Button("stop");
13 
14         MyMonitor myMonitor = new MyMonitor();
15 
16         // 显式地定义触发时会返回的命令,如果不定义,即返回默认值
17         // button1.setActionCommand("start button was clicked!");
18 
19         button1.addActionListener(myMonitor);
20         button2.addActionListener(myMonitor);
21 
22         frame.add(button1, BorderLayout.NORTH);
23         frame.add(button2, BorderLayout.SOUTH);
24         frame.pack();
25         frame.setVisible(true);
26     }
27 }
28 
29 class MyMonitor implements ActionListener{
30 
31     @Override
32     public void actionPerformed(ActionEvent e) {
33         // e.getActionCommand()获得按钮的信息
34         // System.out.println("msg:" + e.getActionCommand());
35         // 可以实现多个按钮共用一个监听器,并且实现各自不同的功能
36         if(e.getActionCommand().equals("start")){
37             System.out.println("msg: start was clicked!");
38         }else{
39             System.out.println("msg: stop was clicked!");
40         }
41     }
42 }

2.2.5 输入框TextField监听

实现一个单行的文本输入框,输入时具有隐私保护,按下Enter键进行事件响应,在控制台打印输入内容并清空文本框。

 1 package com.gui.demo02;
 2 
 3 import java.awt.*;
 4 import java.awt.event.ActionEvent;
 5 import java.awt.event.ActionListener;
 6 
 7 public class TestText01 {
 8     public static void main(String[] args) {
 9         new MyFrame();
10     }
11 }
12 
13 class MyFrame extends Frame{
14     public MyFrame(){
15         TextField textField = new TextField();
16         add(textField);
17 
18         // 监听这个文本框输入的文字
19         MyTextActionListener myTextActionListener = new MyTextActionListener();
20         textField.addActionListener(myTextActionListener);
21 
22         // 设置替换编码
23         textField.setEchoChar('*');
24 
25         setVisible(true);
26         pack();
27     }
28 }
29 
30 class MyTextActionListener implements ActionListener{
31 
32     @Override
33     public void actionPerformed(ActionEvent e) {
34         TextField field = (TextField) e.getSource();  // 获得一些资源,返回一个对象
35         System.out.println(field.getText());          // 获得输入框的文本
36         field.setText("");
37     }
38 }

2.2.6 简易计算器

组合+内部类回顾

实现加法运算:

 1 package com.gui.demo02;
 2 
 3 import java.awt.*;
 4 import java.awt.event.ActionEvent;
 5 import java.awt.event.ActionListener;
 6 
 7 public class TestSimpleCalc {
 8     public static void main(String[] args) {
 9         new MyCalculator();
10     }
11 }
12 
13 // 计算器类
14 class MyCalculator extends Frame{
15     public MyCalculator(){
16         // 三个文本框
17         TextField num1 = new TextField(10);
18         TextField num2 = new TextField(10);
19         TextField num3 = new TextField(15);
20         // 一个按钮
21         Button button = new Button("=");
22         // 按钮事件监听
23         button.addActionListener(new MyCalculatorListener(num1,num2,num3));
24         // 一个标签
25         Label label = new Label("+");
26 
27         // 布局
28         setLayout(new FlowLayout());
29         add(num1);
30         add(label);
31         add(num2);
32         add(button);
33         add(num3);
34 
35         pack();
36         setVisible(true);
37     }
38 }
39 
40 // 监听类
41 class MyCalculatorListener implements ActionListener{
42     // 获取三个变量
43     private TextField num1,num2,num3;
44     public MyCalculatorListener(TextField num1, TextField num2, TextField num3){
45         this.num1 = num1;
46         this.num2 = num2;
47         this.num3 = num3;
48     }
49     @Override
50     public void actionPerformed(ActionEvent e) {
51         // 获得加数和被加数
52         int n1 = Integer.parseInt(num1.getText());
53         int n2 = Integer.parseInt(num2.getText());
54         // 求和置于第三个文本框
55         num3.setText(""+(n1+n2));
56         // 清除前两个文本框
57         num1.setText("");
58         num2.setText("");
59     }
60 }

完全改造为面向对象:

 1 package com.gui.demo02;
 2 
 3 import java.awt.*;
 4 import java.awt.event.ActionEvent;
 5 import java.awt.event.ActionListener;
 6 
 7 public class TestSimpleCalc {
 8     public static void main(String[] args) {
 9         new MyCalculator().loadFrame();
10     }
11 }
12 
13 class MyCalculator extends Frame{
14     // 属性
15     TextField num1,num2,num3;
16 
17     // 方法
18     public void loadFrame(){
19         // 三个文本框
20         num1 = new TextField(10);
21         num2 = new TextField(10);
22         num3 = new TextField(15);
23         // 一个按钮
24         Button button = new Button("=");
25         // 按钮事件监听
26         button.addActionListener(new MyCalculatorListener(this));
27         // 一个标签
28         Label label = new Label("+");
29 
30         // 布局
31         setLayout(new FlowLayout());
32         add(num1);
33         add(label);
34         add(num2);
35         add(button);
36         add(num3);
37 
38         pack();
39         setVisible(true);
40     }
41 }
42 
43 class MyCalculatorListener implements ActionListener{
44     // 获取计算器对象本身,在一个类中组合另一个类
45     MyCalculator calculator = null;
46 
47     public MyCalculatorListener(MyCalculator calculator){
48         this.calculator = calculator;
49     }
50     @Override
51     public void actionPerformed(ActionEvent e) {
52         // 获得加数和被加数
53         int n1 = Integer.parseInt(calculator.num1.getText());
54         int n2 = Integer.parseInt(calculator.num2.getText());
55         // 求和置于第三个文本框
56         calculator.num3.setText(""+(n1+n2));
57         // 清除前两个文本框
58         calculator.num1.setText("");
59         calculator.num2.setText("");
60     }
61 }

内部类:更好的包装。内部类最大的好处就是可以畅通无阻地访问外部类地属性和方法!

 1 package com.gui.demo02;
 2 
 3 import java.awt.*;
 4 import java.awt.event.ActionEvent;
 5 import java.awt.event.ActionListener;
 6 
 7 public class TestSimpleCalc {
 8     public static void main(String[] args) {
 9         new MyCalculator().loadFrame();
10     }
11 }
12 
13 class MyCalculator extends Frame{
14     // 属性
15     TextField num1,num2,num3;
16 
17     // 方法
18     public void loadFrame(){
19         // 三个文本框
20         num1 = new TextField(10);
21         num2 = new TextField(10);
22         num3 = new TextField(15);
23         // 一个按钮
24         Button button = new Button("=");
25         // 按钮事件监听
26         button.addActionListener(new MyCalculatorListener());
27         // 一个标签
28         Label label = new Label("+");
29 
30         // 布局
31         setLayout(new FlowLayout());
32         add(num1);
33         add(label);
34         add(num2);
35         add(button);
36         add(num3);
37 
38         pack();
39         setVisible(true);
40     }
41 
42     // 内部类
43     class MyCalculatorListener implements ActionListener{
44         @Override
45         public void actionPerformed(ActionEvent e) {
46             // 获得加数和被加数
47             int n1 = Integer.parseInt(num1.getText());
48             int n2 = Integer.parseInt(num2.getText());
49             // 求和置于第三个文本框
50             num3.setText(""+(n1+n2));
51             // 清除前两个文本框
52             num1.setText("");
53             num2.setText("");
54         }
55     }
56 }

2.2.7 画笔

 1 package com.gui.demo03;
 2 
 3 import java.awt.*;
 4 
 5 public class TestPaint {
 6     public static void main(String[] args) {
 7         new MyPaint().loadFrame();
 8     }
 9 }
10 
11 class MyPaint extends Frame{
12     public void loadFrame(){
13         setBounds(200,200,600,500);
14         setVisible(true);
15     }
16 
17     // 画笔
18     @Override
19     public void paint(Graphics g) {
20         g.setColor(Color.YELLOW);
21         g.drawLine(100,100,200,200);
22         g.setColor(Color.GREEN);
23         g.fillOval(300,300,100,100);
24         // 画笔用完,注意还原到最初的颜色
25     }
26 }

2.2.8 鼠标监听

目的:实现鼠标画画,每次点击鼠标在窗口中绘制一个圆点。

 1 package com.gui.demo03;
 2 
 3 import java.awt.*;
 4 import java.awt.event.MouseAdapter;
 5 import java.awt.event.MouseEvent;
 6 import java.util.ArrayList;
 7 import java.util.Iterator;
 8 
 9 // 测试鼠标监听
10 public class TestMouseListener {
11     public static void main(String[] args) {
12         new MyMouse("画图");
13     }
14 }
15 
16 class MyMouse extends Frame{
17     ArrayList points;
18 
19     public MyMouse(String title){
20         super(title);
21         points = new ArrayList<>();
22         setBounds(200,200,400,300);
23         setVisible(true);
24 
25         // 鼠标监听器,针对本窗口
26         this.addMouseListener(new MyMouseListener());
27     }
28 
29     // 将集合里的点画到窗口上
30     @Override
31     public void paint(Graphics g) {
32         Iterator iterator = points.iterator();
33         while (iterator.hasNext()){
34             Point point = (Point) iterator.next();
35             g.setColor(Color.YELLOW);
36             g.fillOval(point.x,point.y,10,10);
37         }
38     }
39 
40     // 将点存到集合中
41     public void addPoint(Point point){
42         points.add(point);
43     }
44 
45     // 监听类,适配器模式
46     private class MyMouseListener extends MouseAdapter {
47         // 鼠标事件:按下,弹起,按住不放
48         @Override
49         public void mouseClicked(MouseEvent e) {
50             MyMouse frame = (MyMouse) e.getSource();
51             frame.addPoint(new Point(e.getX(), e.getY()));
52 
53             // 刷新,每次点击鼠标都要重绘一次
54             frame.repaint();
55         }
56     }
57 }

2.2.9 窗口监听

 1 package com.gui.demo03;
 2 
 3 import java.awt.*;
 4 import java.awt.event.WindowAdapter;
 5 import java.awt.event.WindowEvent;
 6 
 7 public class TestWindowListener {
 8     public static void main(String[] args) {
 9         new MyWindow();
10     }
11 }
12 
13 class MyWindow extends Frame{
14     public MyWindow(){
15         setBackground(Color.YELLOW);
16         setBounds(100,100,200,200);
17         setVisible(true);
18         // addWindowListener(new MyWindowListener());
19 
20         this.addWindowListener(
21                 // 匿名内部类
22                 new WindowAdapter() {
23                     @Override
24                     public void windowClosing(WindowEvent e) {
25                         System.out.println("You clicked ’ב");
26                     }
27                 }
28         );
29     }
30 
31     /*
32     class MyWindowListener extends WindowAdapter{
33         @Override
34         public void windowClosing(WindowEvent e) {
35             setVisible(false);      // 隐藏窗口
36         }
37     }
38      */
39 }

2.2.10 键盘监听

 1 package com.gui.demo03;
 2 
 3 import java.awt.*;
 4 import java.awt.event.KeyAdapter;
 5 import java.awt.event.KeyEvent;
 6 
 7 public class TestKeyListener {
 8     public static void main(String[] args) {
 9         new MyKey();
10     }
11 }
12 
13 class MyKey extends Frame{
14     public MyKey(){
15         setBounds(1,1,400,300);
16         setVisible(true);
17 
18         this.addKeyListener(
19                 new KeyAdapter() {
20                     @Override
21                     public void keyPressed(KeyEvent e) {
22                         // 获得当前按键
23                         int keyCode = e.getKeyCode();
24                         if (keyCode == KeyEvent.VK_UP){
25                             System.out.println("You clicked 'up'");
26                         }
27                     }
28                 }
29         );
30     }
31 }

3. Swing

3.1 窗口、面板

 1 package com.gui.demo04;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 
 6 public class JFrameDemo {
 7     // 初始化
 8     public void init(){
 9         JFrame jFrame = new JFrame("This is a JFrame window!");
10         jFrame.setVisible(true);
11         jFrame.setBounds(100,100,200,200);
12 
13         // 设置label
14         JLabel jLabel = new JLabel("JungKook!");
15         jFrame.add(jLabel);
16         // 标签居中对齐
17         jLabel.setHorizontalAlignment(SwingConstants.CENTER);
18 
19         Container contentPane = jFrame.getContentPane();
20         contentPane.setBackground(Color.YELLOW);
21 
22         // 关闭事件
23         jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
24     }
25 
26     public static void main(String[] args) {
27         // 建立一个窗口
28         new JFrameDemo().init();
29     }
30 }

3.2 弹窗

 1 package com.gui.demo04;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 import java.awt.event.ActionEvent;
 6 import java.awt.event.ActionListener;
 7 
 8 // 主窗口
 9 public class DialogDemo extends JFrame {
10     public DialogDemo(){
11         this.setVisible(true);
12         this.setSize(700,500);
13         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
14 
15         // 容器
16         Container contentPane = this.getContentPane();
17         // 绝对布局
18         contentPane.setLayout(null);
19         // 按钮
20         JButton jButton = new JButton("Click here!");
21         jButton.setBounds(30,30,200,50);
22         // 点击按钮时弹出一个弹窗
23         jButton.addActionListener(new ActionListener() {    // 监听器
24             @Override
25             public void actionPerformed(ActionEvent e) {
26                 new MyDialogDemo();
27             }
28         });
29         contentPane.add(jButton);
30     }
31 
32     public static void main(String[] args) {
33         new DialogDemo();
34     }
35 }
36 
37 // 弹窗
38 class MyDialogDemo extends JDialog{
39     public MyDialogDemo(){
40         this.setVisible(true);
41         this.setBounds(100,100,500,500);
42 
43         Container contentPane = this.getContentPane();
44         contentPane.setLayout(null);
45 
46         JLabel jungKook = new JLabel("JungKook");
47         jungKook.setBounds(100,100,300,300);
48         jungKook.setHorizontalAlignment(SwingConstants.CENTER);
49         contentPane.add(jungKook);
50     }
51 }

3.3 标签

label

JLabel jLabel = new JLabel("JungKook!");

图标ICON

 1 package com.gui.demo04;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 
 6 // 图标,需要实现类,JFrame继承
 7 public class IconDemo extends JFrame implements Icon {
 8     private int width;
 9     private int height;
10 
11     public IconDemo(){}     // 无参构造
12 
13     public IconDemo(int width, int height){
14         this.width = width;
15         this.height = height;
16     }
17 
18     public void init(){
19         IconDemo iconDemo = new IconDemo(15, 15);
20         // 图标可以放在标签上,也可以放在按钮上
21         JLabel iconTest = new JLabel("IconTest", iconDemo, SwingConstants.CENTER);
22 
23         Container contentPane = getContentPane();
24         contentPane.add(iconTest);
25 
26         this.setVisible(true);
27         this.setBounds(100,100,200,200);
28         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
29     }
30 
31     @Override
32     public void paintIcon(Component c, Graphics g, int x, int y) {
33         g.fillOval(x,y,width,height);
34     }
35 
36     @Override
37     public int getIconWidth() {
38         return this.width;
39     }
40 
41     @Override
42     public int getIconHeight() {
43         return this.height;
44     }
45 
46     public static void main(String[] args) {
47         new IconDemo().init();
48     }
49 }

图片标签

 1 package com.gui.demo04;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 import java.net.URL;
 6 
 7 public class ImageIconDemo extends JFrame {
 8     public ImageIconDemo(){
 9         // 获取图片地址
10         JLabel label = new JLabel("ImageIcon");
11         URL url = ImageIconDemo.class.getResource("JungKook.jpg");
12 
13         ImageIcon imageIcon = new ImageIcon(url);
14         label.setIcon(imageIcon);
15         label.setHorizontalAlignment(SwingConstants.CENTER);
16 
17         Container contentPane = getContentPane();
18         contentPane.add(label);
19 
20         setVisible(true);
21         setBounds(100,100,500,500);
22         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
23     }
24 
25     public static void main(String[] args) {
26         new ImageIconDemo();
27     }
28 }

3.4 面板

JPanel

 1 package com.gui.demo05;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 
 6 public class JPaneDemo extends JFrame {
 7     public JPaneDemo(){
 8         Container container = this.getContentPane();
 9         container.setLayout(new GridLayout(2,2,10,10));
10 
11         JPanel panel1 = new JPanel(new GridLayout(1, 3));
12         JPanel panel2 = new JPanel(new GridLayout(1, 2));
13         JPanel panel3 = new JPanel(new GridLayout(2, 1));
14         JPanel panel4 = new JPanel(new GridLayout(2, 2));
15 
16         panel1.add(new JButton("1"));
17         panel1.add(new JButton("1"));
18         panel1.add(new JButton("1"));
19         panel2.add(new JButton("2"));
20         panel2.add(new JButton("2"));
21         panel3.add(new JButton("3"));
22         panel3.add(new JButton("3"));
23         panel4.add(new JButton("4"));
24         panel4.add(new JButton("4"));
25         panel4.add(new JButton("4"));
26         panel4.add(new JButton("4"));
27 
28         container.add(panel1);
29         container.add(panel2);
30         container.add(panel3);
31         container.add(panel4);
32 
33         this.setVisible(true);
34         this.setSize(500,500);
35         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
36     }
37 
38     public static void main(String[] args) {
39         new JPaneDemo();
40     }
41 }

JScrollPanel

 1 package com.gui.demo05;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 
 6 public class JScrollDemo extends JFrame {
 7     public JScrollDemo(){
 8         Container container = this.getContentPane();
 9 
10         // 文本域
11         JTextArea textArea = new JTextArea(20, 50);
12         textArea.setText("JungKook!");
13 
14         // Scroll面板
15         JScrollPane scrollPane = new JScrollPane(textArea);
16         container.add(scrollPane);
17 
18         this.setVisible(true);
19         this.setSize(500,500);
20         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
21     }
22 
23     public static void main(String[] args) {
24         new JScrollDemo();
25     }
26 }

3.5 按钮

图片按钮

 1 package com.gui.demo05;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 import java.net.URL;
 6 
 7 public class JButtonDemo01 extends JFrame {
 8     public JButtonDemo01(){
 9         Container container = this.getContentPane();
10 
11         // 将一个图片变成一个图标
12         URL url = JButtonDemo01.class.getResource("JungKook.jpg");
13         ImageIcon icon = new ImageIcon(url);
14         // 把这个图标放在按钮上
15         JButton button = new JButton();
16         button.setIcon(icon);
17         button.setToolTipText("Image Button");
18         // 将按钮添加到容器
19         container.add(button);
20 
21         this.setVisible(true);
22         this.setSize(500,500);
23         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
24     }
25 
26     public static void main(String[] args) {
27         new JButtonDemo01();
28     }
29 }

单选按钮

 1 package com.gui.demo05;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 
 6 public class JButtonDemo02 extends JFrame {
 7     public JButtonDemo02(){
 8         Container container = this.getContentPane();
 9 
10         // 单选框
11         JRadioButton button01 = new JRadioButton("button01");
12         JRadioButton button02 = new JRadioButton("button02");
13         JRadioButton button03 = new JRadioButton("button03");
14 
15         // 由于单选框只能有一个选项被选中,因此使用分组,一个组中只能选择一个
16         ButtonGroup buttonGroup = new ButtonGroup();
17         buttonGroup.add(button01);
18         buttonGroup.add(button02);
19         buttonGroup.add(button03);
20 
21         container.add(button01,BorderLayout.NORTH);
22         container.add(button02,BorderLayout.CENTER);
23         container.add(button03,BorderLayout.SOUTH);
24 
25         this.setVisible(true);
26         this.setSize(500,500);
27         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
28     }
29 
30     public static void main(String[] args) {
31         new JButtonDemo02();
32     }
33 }

复选按钮

 1 package com.gui.demo05;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 
 6 public class JButtonDemo03 extends JFrame {
 7     public JButtonDemo03(){
 8         Container container = this.getContentPane();
 9 
10         // 复选框
11         JCheckBox checkBox01 = new JCheckBox("checkBox01");
12         JCheckBox checkBox02 = new JCheckBox("checkBox02");
13 
14         container.add(checkBox01,BorderLayout.NORTH);
15         container.add(checkBox02,BorderLayout.SOUTH);
16 
17         this.setVisible(true);
18         this.setSize(500,500);
19         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
20     }
21 
22     public static void main(String[] args) {
23         new JButtonDemo03();
24     }
25 }

3.6 列表

下拉框

 1 package com.gui.Demo06;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 
 6 public class TestComboBoxDemo01 extends JFrame {
 7     public TestComboBoxDemo01(){
 8         Container container = this.getContentPane();
 9 
10         JComboBox status = new JComboBox();
11 
12         status.addItem(null);
13         status.addItem("正在热映");
14         status.addItem("即将上映");
15         status.addItem("已下架");
16 
17         container.add(status);
18 
19         this.setVisible(true);
20         this.setSize(500,500);
21         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
22     }
23 
24     public static void main(String[] args) {
25         new TestComboBoxDemo01();
26     }
27 }

列表框

 1 package com.gui.Demo06;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 import java.util.Vector;
 6 
 7 public class TestComboBoxDemo02 extends JFrame {
 8     public TestComboBoxDemo02(){
 9         Container container = this.getContentPane();
10 
11         // 生成列表内容
12         // String[] contents = {"1", "2", "3"};     // 静态创建
13         Vector contents = new Vector();             // 动态添加
14         // 列表中放入内容
15         JList jList = new JList(contents);
16 
17         contents.add("First");
18         contents.add("Second");
19         contents.add("Third");
20 
21         container.add(jList);
22 
23         this.setVisible(true);
24         this.setSize(500,500);
25         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
26     }
27 
28     public static void main(String[] args) {
29         new TestComboBoxDemo02();
30     }
31 }

3.7 文本框

文本框

 1 package com.gui.Demo06;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 import java.util.Vector;
 6 
 7 public class TestTextDemo01 extends JFrame {
 8     public TestTextDemo01(){
 9         Container container = this.getContentPane();
10 
11         JTextField textField1 = new JTextField("hello!");
12         JTextField textField2 = new JTextField("JungKook^_^");
13 
14         container.add(textField1,BorderLayout.NORTH);
15         container.add(textField2,BorderLayout.SOUTH);
16 
17         this.setVisible(true);
18         this.setSize(500,500);
19         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
20     }
21 
22     public static void main(String[] args) {
23         new TestTextDemo01();
24     }
25 }

密码框

 1 package com.gui.Demo06;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 
 6 public class TestTextDemo02 extends JFrame {
 7     public TestTextDemo02(){
 8         Container container = this.getContentPane();
 9 
10         JPasswordField passwordField = new JPasswordField();
11         passwordField.setEchoChar('*');
12 
13         container.add(passwordField);
14 
15         this.setVisible(true);
16         this.setSize(500,500);
17         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
18     }
19 
20     public static void main(String[] args) {
21         new TestTextDemo02();
22     }
23 }

文本域:参见JScrollPanel举例。

 附:狂神b站视频链接

原文地址:https://www.cnblogs.com/java-learning-xx/p/13885929.html