栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java swing 窗体程序 4 面板

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java swing 窗体程序 4 面板

面板也是一个容器,可作为容器容纳其他组件,但也必须被添加到其他容器中。Swing中常用面板有JPanel面板和JScrollPane面板。

1. JPanel
  JPanel面板可以聚集一些组件来布局。继承自java.awt.Container类。

下面举个例子。

import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.Jframe;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class JPanelDemo extends Jframe {

    public JPanelDemo() {
        Container container = this.getContentPane();
        container.setLayout(new GridLayout(2, 1, 10, 10));    // 整个容器为2行1列

        JPanel p1 = new JPanel(new GridLayout(1, 3));    // 初始化一个面板,设置1行3列的网格布局
        JPanel p2 = new JPanel(new GridLayout(1, 2));    // 初始化一个面板,设置1行2列的网格布局
        JPanel p3 = new JPanel(new GridLayout(2, 1));    // 初始化一个面板,设置2行1列的网格布局
        JPanel p4 = new JPanel(new GridLayout(3, 2));    // 初始化一个面板,设置3行2列的网格布局

        p1.add(new JButton("1"));    // 在JPanel面板中添加按钮
        p1.add(new JButton("1"));    // 在JPanel面板中添加按钮
        p1.add(new JButton("1"));    // 在JPanel面板中添加按钮
        p2.add(new JButton("2"));    // 在JPanel面板中添加按钮
        p2.add(new JButton("2"));    // 在JPanel面板中添加按钮
        p3.add(new JButton("3"));    // 在JPanel面板中添加按钮
        p3.add(new JButton("3"));    // 在JPanel面板中添加按钮
        p4.add(new JButton("4"));    // 在JPanel面板中添加按钮
        p4.add(new JButton("4"));    // 在JPanel面板中添加按钮
        p4.add(new JButton("4"));    // 在JPanel面板中添加按钮
        p4.add(new JButton("4"));    // 在JPanel面板中添加按钮
        p4.add(new JButton("4"));    // 在JPanel面板中添加按钮
        p4.add(new JButton("4"));    // 在JPanel面板中添加按钮

        container.add(p1);        // 在容器中添加面板
        container.add(p2);        // 在容器中添加面板
        container.add(p3);        // 在容器中添加面板
        container.add(p4);        // 在容器中添加面板

        this.setVisible(true);
        this.setSize(500, 350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JPanelDemo();
    }

}

其中,容器的GridLayout布局设置了横纵都为10的间距,JPanel的GridLayout布局没有设置网格间距。

2. JScrollPane
  若遇到一个较小的容器窗体中显示一个较大部分内容的情况,可用JScrollPane面板。这是一个带滚动条的面板,就像平时浏览网页,经常遇到的滚动条一样。

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

import java.awt.Container;

import javax.swing.Jframe;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Jtextarea;
import javax.swing.ScrollPaneConstants;
import javax.swing.WindowConstants;

public class JScrollPaneDemo extends Jframe {

    public JScrollPaneDemo() {
        Container container = this.getContentPane();

        Jtextarea tArea = new Jtextarea(20, 50);        // 创建文本区域组件
        tArea.setText("http://www.cnblogs.com/adamjwh/");

        JScrollPane sp = new JScrollPane(tArea);
        container.add(sp);

        this.setVisible(true);
        this.setSize(300, 150);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JScrollPaneDemo();
    }

}

其中Jtextarea是创建一个文本区域组件,大小为20*50,setText()方法是给该文本区域填值。这里在new一个JScrollPane时,就将文本区域组件添加到其上。运行结果:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/324941.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号