1. 程式人生 > 其它 >GUI-Swing-面板Jpanel/滾動條Jscroll

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 }

 

結果: