Java-SWING 的容器 JFRAME和JDIALOG

Java-SWING 的容器 JFRAME和JDIALOG


java的图形界面中,容器是用来存放 按钮,输入框等组件的。

窗体型容器有两个,一个是JFrame,一个是JDialog


JFrame

JFrame是最常用的窗体型容器,默认情况下,在右上角有最大化最小化按钮
在这里插入图片描述

package gui;

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

public class JFrameText {
	public static void main(String[] args) {
		JFrame f=new JFrame("LOL");
		f.setSize(400,300);
		f.setLocation(200,200);
		f.setLayout(null);
		JButton b=new JButton("秒杀");
		b.setBounds(50,50,280,30);
		
		f.add(b);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);
	}

}


JDialog

JDialog也是窗体型容器,右上角没有最大和最小化按钮
在这里插入图片描述

package gui;

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

public class JDialogText {
	public static void main(String[] args) {
		JDialog d=new JDialog();
		d.setTitle("LOL");
		
		d.setSize(400,300);
		d.setLocation(200,200);
		d.setLayout(null);
		JButton b=new JButton("秒杀");
		b.setBounds(50,50,280,30);
		
		d.add(b);
		d.setVisible(true);
	}

}

模态JDialog

当一个对话框被设置为模态的时候,其背后的父窗体,是不能被激活的,除非该对话框被关闭
在这里插入图片描述

package gui;

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

public class ModalJDialogText {
	public static void main(String[] args) {
		JFrame f=new JFrame("LOL");
		f.setSize(800,600);
		f.setLocation(100,100);
		
		// 根据外部窗体实例化JDialog
		JDialog d=new JDialog(f);
		// 设置为模态
		d.setModal(true);
		
		d.setTitle("模态的对话框");
		d.setSize(400,300);
		d.setLocation(200, 200);
		d.setLayout(null);
		JButton b=new JButton("秒杀");
		b.setBounds(50,50,280,30);
		d.add(b);
		
		f.setVisible(true);
		d.setVisible(true);
	}
	

}

窗体大小不可变化

通过调用方法 setResizable(false); 做到窗体大小不可变化
在这里插入图片描述

package gui;

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

public class JFrameText {
	public static void main(String[] args) {
		JFrame f=new JFrame("LOL");
		f.setSize(400,300);
		f.setLocation(200,200);
		f.setLayout(null);
		JButton b=new JButton("秒杀");
		b.setBounds(50,50,280,30);
		
		f.add(b);
		
		// 窗体大小不可变化
        f.setResizable(false);
        
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);
	}

}

更多内容,点击了解: SWING 的容器 JFRAME和JDIALOG

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值