2021-06-16

java第二十八天

一、学习内容:
1、I IFrame是最常用的一 种顶层容器, 它的作用是创建-个顶层的Windows窗体,它的外观就像平常Windows系统下见到的窗体,带有标题栏和控制按钮等。
JFrame类提供了下面两种格式的构造方法。
(1)JFrame( ):创建不指定标题的窗体。
(2)JFrame(String title) :创建指定标题的窗体。

2、EXIT_ON_CLOSE:隐藏窗体,并停止程序
DO_NOTHING_ON_CLOSE:无任何操作
HIDE_ON_CLOSE:隐藏窗体但不停止程序
DISPOSE_ON_CLOSE:释放窗体资源
二、代码练习:
1、

package Demo01;

import java.awt.Container;

import javax.swing.JDialog;
import javax.swing.JLabel;

public class JDialogTest extends JDialog {

	public static void main(String[] args) {
		JDialog jd=new JDialog();
		jd.setVisible(true);
		jd.setBounds(200,200,400,600);
		
		Container c=jd.getContentPane();//获取窗体容器
		c.add(new JLabel("这是一个标签"));
	}

}

2、

package Demo01;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;

public class JDialogTest2 extends JDialog {
	public JDialogTest2(JFrame frame) {
		super(frame,"对话框标签",false);
		setVisible(true);//设置窗体可见
		setBounds(100,100,100,100);
		//setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	
	public static void main(String[] args) {
		JFrame f=new JFrame("父窗体");
		f.setBounds(50,50,300,300);
		Container c=f.getContentPane();
		JButton btn=new JButton("弹出对话框");
		c.setLayout(new FlowLayout());
		c.add(btn);
		f.setVisible(true);
		f.setDefaultCloseOperation(EXIT_ON_CLOSE);
		btn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				JDialogTest2 jd=new JDialogTest2(f);
				jd.setVisible(true);
			}
		});
		
	}

}

3、

package Demo01;

import java.awt.Color;
import java.awt.Container;

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

public class jFrameTest extends JFrame{
	public jFrameTest() {
		//JFrame f=new JFrame("大数据五班");
		//设置窗体可见
		setVisible(true);
		//注意:点击关闭没有结束程序的运行
		/*
		 * EXIT_ON_CLOSE:隐藏窗体,并停止程序
		 * DO_NOTHING_ON_CLOSE:无任何操作
		 * HIDE_ON_CLOSE:隐藏窗体,但是不停止程序
		 * DISPOSE_ON_CLOSE:释放窗体资源
		 */
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//setLocation(200,600);//设置位置,单位是像素,从左上角0.0开始
		//setSize(400,300);//设置大小,单位:像素
		
		setBounds(200,200,400,600);
		Container c=getContentPane();
		c.setBackground(Color.pink);//设置背景颜色
		JLabel l=new JLabel("自动登录");
		c.add(l);//添加组件
		//c.remove(l);//移除组件
		c.validate();
		//f.setContentPane(c);
		setResizable(false);
		
		System.out.println("X="+getX()+" Y="+getY());
	}
	
	public static void main(String[] args) {
		//JFrame f=new JFrame("大数据五班");//创建窗体对象,可在参数中设置窗体标题
		new jFrameTest();
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值