Java-常用面板

来源:互联网 发布:全国人口普查数据查询 编辑:程序博客网 时间:2024/05/16 17:38

面板也是一个Swing容器,可以在面板中设置布局格式,再把面板放到container中,可以认为是容器中的容器,Java中常用面板有JPanel和JScrollPane。

1.JPanel:

举个例子:

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 Testone extends JFrame{public Testone(){setTitle("面板使用");setSize(300,300);setVisible(true);setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);Container a=getContentPane();a.setLayout(new GridLayout(2,1,10,10));//容器设置为2行1列JPanel b=new JPanel(new GridLayout(1,4,10,10));//面板容器设置为1行4列JPanel c=new JPanel(new GridLayout(1,2,10,10));//面板容器设置为1行2列JPanel d=new JPanel(new GridLayout(1,2,10,10));//面板容器设置为1行2列JPanel e=new JPanel(new GridLayout(2,1,10,10));//面板容器设置为2行1列b.add(new JButton("1")); b.add(new JButton("1"));b.add(new JButton("2"));b.add(new JButton("3"));c.add(new JButton("4"));c.add(new JButton("5"));d.add(new JButton("6"));d.add(new JButton("7"));e.add(new JButton("8"));e.add(new JButton("9"));a.add(b);a.add(c);a.add(d);a.add(e);}public static void main(String[] args){new Testone();}}


运行结果:


2.JScrollPane:带滚动条的面板,只能放置一个组件,并且不可以使用布局管理器

举个例子:

import java.awt.Container;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.WindowConstants;public class Testone extends JFrame{public Testone(){setSize(300,300);setTitle("带滚动条的文字编辑器");setVisible(true);Container a=getContentPane();JTextArea b=new JTextArea(20,50);//创建文本区域组件JScrollPane c=new JScrollPane(b);a.add(c);setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);}public static void main(String[] args){new Testone();}}


运行效果:


0 0
原创粉丝点击