GUI-Swing-面板Jpanel/滚动条Jscroll

面板

代码:

 1 package com.luckylu.gui;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 
 6 public class JPanelDeml extends JFrame {
 7     public JPanelDeml() {
 8         Container container = this.getContentPane();
 9         container.setLayout(new GridLayout(2,1,10,10)); //后面的间距参数
10 
11         JPanel panel1 = new JPanel(new GridLayout(1, 3));
12 
13        panel1.add(new JButton("1-1"));
14        panel1.add(new JButton("1-2"));
15        panel1.add(new JButton("1-3"));
16 
17        container.add(panel1);
18 
19        this.setVisible(true);
20        this.setTitle("JPanel例子");
21        this.setBounds(200,200,500,500);
22        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
23 
24     }
25 
26     public static void main(String[] args) {
27         new JPanelDeml();
28     }
29 }

结果:

 

 

 例子2

 1 package com.luckylu.gui;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 
 6 public class JPanelDeml extends JFrame {
 7     public JPanelDeml() {
 8         Container container = this.getContentPane();
 9         container.setLayout(new GridLayout(2,1,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(3, 2));
15 
16 
17        panel1.add(new JButton("1-1"));
18        panel1.add(new JButton("1-2"));
19        panel1.add(new JButton("1-3"));
20        panel2.add(new JButton("2-1"));
21        panel2.add(new JButton("2-2"));
22        panel3.add(new JButton("3-1"));
23        panel3.add(new JButton("3-2"));
24        panel4.add(new JButton("4-1"));
25        panel4.add(new JButton("4-2"));
26        panel4.add(new JButton("4-3"));
27        panel4.add(new JButton("4-4"));
28        panel4.add(new JButton("4-5"));
29        panel4.add(new JButton("4-6"));
30 
31        container.add(panel1);
32        container.add(panel2);
33        container.add(panel3);
34        container.add(panel4);
35 
36        this.setVisible(true);
37        this.setTitle("JPanel例子");
38        this.setBounds(200,200,500,500);
39        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
40 
41     }
42 
43     public static void main(String[] args) {
44         new JPanelDeml();
45     }
46 }

结果

 

 

 

带滚动条的面板

代码:

 1 package com.luckylu.gui;
 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("请输入~~~~"); //设置文本域的默认文本
13 
14         //Scroll面板
15         JScrollPane scrollPane = new JScrollPane(textArea);  //创建面板并将文本域装入面板
16         container.add(scrollPane);  //将面板装入容器
17 
18         this.setVisible(true);
19         this.setTitle("带滚动条的文本框");
20         this.setBounds(200,200,300,280);
21         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
22 
23     }
24 
25     public static void main(String[] args) {
26         new JScrollDemo();
27     }
28 }

 

结果:

 

 

posted @ 2022-04-19 13:59  luckylu1983  阅读(204)  评论(0编辑  收藏  举报