窗体

窗体

  • 组成GUI的组件如按钮、标签等不能独立使用,必须放在容器内。

  • 容器本身也是一个组件,具有组件的所有性质,另外还具有容纳其它功能。

  • 所有的组件都可以通过add()方法向容器中添加组件

  • 在Swing中,常用的三种类型的容器是JFrame,JPanel,JApplet

  • Swing GUI形成顶层容器-中间层容器-基本组件的层次包容关系,具有Swing GUI的应用必须有至少一个顶层容器

  • 顶层Swing容器是JFrame、JDialog或JApplet的实例

  • 中间层容器是由通用容器构成。

  • JFrame是最常用的顶层容器,一般作为Java Application的主窗口。

1.显示窗口 setVisible(true);

2.设置单击关闭窗口按钮操作:退出应用系统 setDefaultCloseOperation(EXIT_ON_CLOSE) (WindowConstants.DO_NOTHING_ON_CLOSE; 不做任何动作。WindowConstants.DISPOSE_ON_CLOSE; 关闭窗口,释放资源。 WindowConstants.HIDE_ON_CLOSE;隐藏窗口。)

3.setVisible(true); //显示窗体

4.setLocationRelativeTo(null);//窗体显示在屏幕中央

5.setSize(w,h);//设置窗体大小

6.setResizable(true 或者 false);//可以自由拖动窗体大小

 

JLabel :标签组件

 

JButton :按钮组件

 

JTextArea ,JTextField :文本域组件

 

 

package src1;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Prectice1 extends JFrame
{
 JPanel p;
 JLabel lab1;
public Prectice1()
  {
init();

}
public void init()
{
p=new JPanel();
lab1=new JLabel("hello world");
p.add(lab1);
p.setVisible(true);
this.getContentPane().add(p);//将中间容器p设置为当前窗体的面板.
this.setDefaultCloseOperation(EXIT_ON_CLOSE);//设置单击关闭窗口按钮操作:退出应用系统
this.setResizable(false);//可以自由拖动窗体大小
this.setSize(410,300);//设置窗体大小
this.setLocationRelativeTo(null);//窗体显示在屏幕中央
this.setVisible(true); //显示窗体
}
   public static void main(String[] args)
  {
Prectice1 prectice1=new Prectice1();
}
}
 
posted @ 2022-05-08 21:25  zjw_rp  阅读(89)  评论(0编辑  收藏  举报