zoukankan      html  css  js  c++  java
  • 第13章 Swing程序设计----常用面板

    面板也是一个Swing容器,它可以作为容器容纳其他组件,但它也必须被添加到其他容器中

    Swing常用的面板包括JPanel面板和JScrollPanel面板

    1、JPanel面板

     1 import java.awt.*;
     2 
     3 import javax.swing.*;
     4 
     5 public class JPanelTest extends JFrame {
     6     /**
     7      * 
     8      */
     9     private static final long serialVersionUID = 1L;
    10 
    11     public JPanelTest() {
    12         Container c = getContentPane();
    13         // 将整个容器设置为2行1列的网格布局
    14         c.setLayout(new GridLayout(2, 1, 10, 10));
    15         // 初始化一个面板,设置1行3列的网格布局
    16         JPanel p1 = new JPanel(new GridLayout(1, 3, 10, 10));
    17         JPanel p2 = new JPanel(new GridLayout(1, 2, 10, 10));
    18         JPanel p3 = new JPanel(new GridLayout(1, 2, 10, 10));
    19         JPanel p4 = new JPanel(new GridLayout(2, 1, 10, 10));
    20         p1.add(new JButton("1")); // 在面板中添加按钮
    21         p1.add(new JButton("1"));
    22         p1.add(new JButton("2"));
    23         p1.add(new JButton("3"));
    24         p2.add(new JButton("4"));
    25         p2.add(new JButton("5"));
    26         p3.add(new JButton("6"));
    27         p3.add(new JButton("7"));
    28         p4.add(new JButton("8"));
    29         p4.add(new JButton("9"));
    30         c.add(p1); // 在容器中添加面板
    31         c.add(p2);
    32         c.add(p3);
    33         c.add(p4);
    34         setTitle("在这个窗体中使用了面板");
    35         setSize(420, 200);
    36         setVisible(true);
    37         setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    38     }
    39     
    40     public static void main(String[] args) {
    41         new JPanelTest();
    42     }
    43 }

    在该类中创建4个JPanel面板组件,并将其添加到窗体中。

    2、JScrollPane面板

    在设置界面时,可能遇到在一个较小的容器窗体中显示一个较大部分的内容的情况这是可以使用JScrollPane面板。

    JScrollPane面板是带滚动条的面板,它也是一种容器,但是JScrollPane只能放置一个组件,并且不可以使用布局管理器

    如果需要在JScrollPane面板中放置多个组件,需要将多个组件放置在JPanel面板上,然后将JPanel面板作为一个整体组件添加在JScrollPane组件上。

     1 import java.awt.*;
     2 
     3 import javax.swing.*;
     4 
     5 public class JScrollPaneTest extends JFrame {
     6     /**
     7      * 
     8      */
     9     private static final long serialVersionUID = 1L;
    10 
    11     public JScrollPaneTest() {
    12         Container c = getContentPane(); // 创建容器
    13         JTextArea ta = new JTextArea(20, 50); // 创建文本区域组件
    14         JScrollPane sp = new JScrollPane(ta); // 创建JScrollPane面板对象
    15         c.add(sp); // 将该面板添加到该容器中
    16         
    17         setTitle("带滚动条的文字编译器");
    18         setSize(200, 200);
    19         setVisible(true);
    20         setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    21     }  
    23     public static void main(String[] args) {
    24         new JScrollPaneTest();       
    26     }    
    28 }

  • 相关阅读:
    oracle inside(5)
    oracle inside(4)
    WF & WCF(1)
    oracle inside(7)
    oracle inside(11)
    ArrayList使用toArray()构造数组时的问题
    在android下抓包
    为什么中国的程序员技术偏低
    [置顶] 女人与程序的对应关系!
    Java synchronized详解
  • 原文地址:https://www.cnblogs.com/chamie/p/4702113.html
Copyright © 2011-2022 走看看