GUI编程——面板

面板——JPanel、JScroll

 

JPanel

 1 package com.kuang.lesson05;
 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 panel1=new JPanel(new GridLayout(1,3));
13         JPanel panel2=new JPanel(new GridLayout(1,2));
14         JPanel panel3=new JPanel(new GridLayout(2,1));
15         JPanel panel4=new JPanel(new GridLayout(3,2));
16 
17         panel1.add(new JButton("1"));
18         panel1.add(new JButton("1"));
19         panel1.add(new JButton("1"));
20         panel2.add(new JButton("2"));
21         panel2.add(new JButton("2"));
22         panel3.add(new JButton("3"));
23         panel3.add(new JButton("3"));
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 
39     public static void main(String[] args) {
40         new JPanelDemo();
41     }
42 }

 

JScroll

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

 

posted @ 2021-04-15 20:03  cengxuyuan  阅读(90)  评论(0)    收藏  举报