JPanel面板:重点,布局有间距
1 package com.gui.lesson4;
2
3 import javax.swing.*;
4 import java.awt.*;
5
6 public class JPanelDemo extends JFrame {
7 public JPanelDemo() {
8 Container container = this.getContentPane();
9
10 container.setLayout(new GridLayout(2, 1, 10, 10));//后面两个参数的意思,间距
11
12 JPanel jPanel = new JPanel(new GridLayout(1, 3));
13 JPanel jPane2 = new JPanel(new GridLayout(1, 2));
14 JPanel jPane3 = new JPanel(new GridLayout(2, 1));
15 JPanel jPane4 = new JPanel(new GridLayout(3, 2));
16
17 jPanel.add(new JButton("1"));
18 jPanel.add(new JButton("1"));
19 jPanel.add(new JButton("1"));
20 jPane2.add(new JButton("2"));
21 jPane2.add(new JButton("2"));
22 jPane3.add(new JButton("3"));
23 jPane3.add(new JButton("3"));
24 jPane4.add(new JButton("4"));
25 jPane4.add(new JButton("4"));
26 jPane4.add(new JButton("4"));
27 jPane4.add(new JButton("4"));
28 jPane4.add(new JButton("4"));
29 jPane4.add(new JButton("4"));
30
31 container.add(jPanel);
32 container.add(jPane2);
33 container.add(jPane3);
34 container.add(jPane4);
35
36 setVisible(true);
37 setSize(500, 500);
38 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
39
40 }
41
42 public static void main(String[] args) {
43 new JPanelDemo();
44 }
45 }
文本域,JScrollPanel面板:滚动
1 package com.gui.lesson4;
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("欢迎学习Java");
13
14 //Scroll面板
15 JScrollPane scrollPane = new JScrollPane(textArea);
16 container.add(scrollPane);
17
18 this.setVisible(true);
19 this.setBounds(100, 100, 300, 150);
20 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
21 }
22
23 public static void main(String[] args) {
24 new JScrollDemo();
25 }
26 }