博为峰Java技术题 ——JavaSE Java Swing在顶层容器中添加组件Ⅱ

博为峰小博老师:测试

下面将经过一个Swing程序来说述如何在顶层容器中添加内容面板。其程序代码以下所示:code

/**对象

 * 这段代码主要是介绍如何在一个顶层容器内获取一个面板,也能够说是在顶层容器内产生一个默认的内容面板.blog

 */ip

public class BWFcontainers {get

static int width=300;io

static int height=200;class

public static void main(String[] args){容器

JFrame jf=new JFrame("添加内容面板测试");//建立一个顶层容器类对象程序

jf.setSize(widthheight);//设置顶层容器类对象的大小

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置顶层容器类时象的关闭功能

JPanel contentPane=new JPanel(); //建立一个中间容器类对象

jf.setContentPane(contentPane);//将中间容器组件对象contentPane设置为内容面板

jf.setVisible(true);//设置顶层容器类对象的可见性

}

}

上面程序的运行结果以下图所示:

上面的程序段主要用于将一个内容面板添加到顶层容器中去,接下来就能够在内容面板中添加其余组件,代码以下所示:

/**

 * 这段代码主要是为介绍如何在己经建立好的内容面板中添加普通组件.

 */

public class BWFcontainers {

static int width=300;

static int height=200;

public static void main(String[] args){

JFrame jf=new JFrame("添加内容面板测试");//建立一个顶层容器类对象

jf.setSize(widthheight);//设置顶层容器类对象的大小

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置顶层容器类时象的关闭功能

JPanel contentPane=new JPanel(); //建立一个中间容器类对象

contentPane.add(new JButton("肯定"));//建立按钮组件,并在内容面板中添加

contentPane.add(new JButton("取消"));

jf.setContentPane(contentPane);//将中间容器组件对象contentPane设置为内容面板

jf.setVisible(true);//设置顶层容器类对象的可见性

}

}

上面程序的运行结果以下图所示:

相关文章
相关标签/搜索