GUI编程

GUI编程

1、简介

Gui的核心技术:Swing AWT

2、AWT

2.1、Awt介绍

1.包含了很多类和接口!GUI!

2.元素:窗口,按钮,文本框

3.java.awt

2.2、组件和容器

2.3、布局管理器

2.4、事件监听

5、输入框TextField监听

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

2.7、画笔

2.8、鼠标监听

2.9、窗口监听

2.10、键盘监听


3、Swing

3.1、窗口、面板

3.2、弹窗

3.3、标签

1
new JLabel("xxx");

图标****ICON

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.java.gui.lesson04;

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

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() {
       IconDemo iconDemo = new IconDemo(15, 15);
       // 图标放在标签,也可以放在按钮上
       JLabel jLabel = new JLabel("iconTest", iconDemo, SwingConstants.CENTER);
       Container container = getContentPane();
       container.add(jLabel);
       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, this.width, this.height);
  }

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

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

图片****ICON

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.java.gui.lesson04;

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

public class ImageIconDemo extends JFrame {
   public ImageIconDemo() throws HeadlessException {
       // 获取图片的地址
       JLabel label = new JLabel("ImageIcon");
       URL url = ImageIconDemo.class.getResource("tx.jpg");

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

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

3.4、面板

JPanel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.java.gui.lesson05;

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

public class JPanelDemo extends JFrame {
   public JPanelDemo() throws HeadlessException {
       Container container = this.getContentPane();
       container.setLayout(new GridLayout(2, 1, 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, 1));
       JPanel jPanel4 = new JPanel(new GridLayout(3, 2));

       jPanel1.add(new JButton("1"));
       jPanel1.add(new JButton("1"));
       jPanel1.add(new JButton("1"));

       jPanel2.add(new JButton("2"));
       jPanel2.add(new JButton("2"));

       jPanel3.add(new JButton("3"));
       jPanel3.add(new JButton("3"));

       jPanel4.add(new JButton("4"));
       jPanel4.add(new JButton("4"));
       jPanel4.add(new JButton("4"));
       jPanel4.add(new JButton("4"));
       jPanel4.add(new JButton("4"));
       jPanel4.add(new JButton("4"));

       container.add(jPanel1);
       container.add(jPanel2);
       container.add(jPanel3);
       container.add(jPanel4);

       this.setSize(500, 500);
       this.setVisible(true);
       this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }

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

JScrollPanel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.java.gui.lesson05;

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

public class JScrollDemo extends JFrame {
   public JScrollDemo() throws HeadlessException {
       Container container = this.getContentPane();

       // 文本域
       JTextArea jTextArea = new JTextArea(20, 50);
       jTextArea.setText("默认文本");

       JScrollPane scrollPane = new JScrollPane(jTextArea);

       container.add(scrollPane);

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

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

3.5、按钮

图片按钮

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.java.gui.lesson05;

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

public class JButtonDemo01 extends JFrame {
   public JButtonDemo01() throws HeadlessException {
       Container container = this.getContentPane();

       // 图标
       URL resource = JButtonDemo01.class.getResource("tx.jpg");
       ImageIcon icon = new ImageIcon(resource);

       // 把图片放在按钮上
       JButton jButton = new JButton();
       jButton.setIcon(icon);
       jButton.setToolTipText("图片按钮");

       container.add(jButton);

       this.setSize(500, 300);
       this.setVisible(true);
       this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

  }

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

单选按钮

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.java.gui.lesson05;

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

public class JButtonDemo02 extends JFrame {
   public JButtonDemo02() throws HeadlessException {
       Container container = this.getContentPane();

       // 图标
       URL resource = JButtonDemo02.class.getResource("tx.jpg");
       ImageIcon icon = new ImageIcon(resource);

       // 单选框
       JRadioButton radioButton01 = new JRadioButton("JRadioButton01");
       JRadioButton radioButton02 = new JRadioButton("JRadioButton02");
       JRadioButton radioButton03 = new JRadioButton("JRadioButton03");

       // 由于单选框只能选择一个,分组,一个组中只能选择一个
       ButtonGroup buttonGroup = new ButtonGroup();
       buttonGroup.add(radioButton01);
       buttonGroup.add(radioButton02);
       buttonGroup.add(radioButton03);

       container.add(radioButton01, BorderLayout.CENTER);
       container.add(radioButton02, BorderLayout.NORTH);
       container.add(radioButton03, BorderLayout.SOUTH);

       this.setSize(500, 300);
       this.setVisible(true);
       this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

  }

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

复选按钮

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.java.gui.lesson05;

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

public class JButtonDemo03 extends JFrame {
   public JButtonDemo03() throws HeadlessException {
       Container container = this.getContentPane();

       // 图标
       URL resource = JButtonDemo03.class.getResource("tx.jpg");
       ImageIcon icon = new ImageIcon(resource);

       // 多选框
       JCheckBox jCheckBox01 = new JCheckBox("jCheckBox01");
       JCheckBox jCheckBox02 = new JCheckBox("jCheckBox01");

       container.add(jCheckBox01, BorderLayout.NORTH);
       container.add(jCheckBox02, BorderLayout.SOUTH);

       this.setSize(500, 300);
       this.setVisible(true);
       this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

  }

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

3.6、列表

下拉框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.java.gui.lesson06;

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

public class TestComboboxDemo01 extends JFrame {
   public TestComboboxDemo01() throws HeadlessException {
       Container container = this.getContentPane();

       JComboBox status = new JComboBox();

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

       container.add(status);

       this.setSize(500, 350);
       this.setVisible(true);
       this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }

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

列表框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.java.gui.lesson06;

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

public class TestComboboxDemo02 extends JFrame {
   public TestComboboxDemo02() throws HeadlessException {
       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.setSize(500, 350);
       this.setVisible(true);
       this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }

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

3.7、文本框

文本框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.java.gui.lesson06;

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

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

       JTextField textFiled1 = new JTextField("hello");
       JTextField textFiled2 = new JTextField("world", 20);

       container.add(textFiled1, BorderLayout.NORTH);
       container.add(textFiled2, BorderLayout.SOUTH);

       this.setSize(500, 350);
       this.setVisible(true);
       this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }

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

密码框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.java.gui.lesson06;

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

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

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

       container.add(passwordField);

       this.setSize(500, 350);
       this.setVisible(true);
       this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }

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

文本域

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.java.gui.lesson05;

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

public class JScrollDemo extends JFrame {
   public JScrollDemo() throws HeadlessException {
       Container container = this.getContentPane();

       // 文本域
       JTextArea jTextArea = new JTextArea(20, 50);
       jTextArea.setText("默认文本");

       JScrollPane scrollPane = new JScrollPane(jTextArea);

       container.add(scrollPane);

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

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