图形界面系列教材 (三)- Swing 的容器 JFrame和JDialog

 

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

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

步骤1:JFrame
步骤2:JDialog
步骤3:模态JDialog
步骤4:窗体大小不可变化
步骤5:练习-模态与大小变化
步骤6:答案-模态与大小变化

步骤 1 : JFrame

JFrame是最常用的窗体型容器,默认情况下,在下载区(点击进入)有最大化最小化按钮

JFrame

package gui;

 

import javax.swing.JButton;

import javax.swing.JFrame;

 

public class TestGUI {

    public static void main(String[] args) {

         

        //普通的窗体,带最大和最小化按钮

        JFrame f = new JFrame("LoL");

        f.setSize(400300);

        f.setLocation(200200);

        f.setLayout(null);

        JButton b = new JButton("一键秒对方基地挂");

        b.setBounds(505028030);

 

        f.add(b);

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 

        f.setVisible(true);

    }

}

步骤 2 : JDialog

JDialog也是窗体型容器,下载区(点击进入)没有最大和最小化按钮

JDialog

package gui;

 

import javax.swing.JButton;

import javax.swing.JDialog;

 

public class TestGUI {

    public static void main(String[] args) {

         

        //普通的窗体,带最大和最小化按钮,而对话框却不带

        JDialog d = new JDialog();

        d.setTitle("LOL");

        d.setSize(400300);

        d.setLocation(200200);

        d.setLayout(null);

        JButton b = new JButton("一键秒对方基地挂");

        b.setBounds(505028030);

 

        d.add(b);

 

        d.setVisible(true);

    }

}

步骤 3 : 模态JDialog

当一个对话框被设置为模态的时候,其背后的父窗体,是不能被激活的,除非该对话框被关闭

package gui;

 

import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JFrame;

 

public class TestGUI {

    public static void main(String[] args) {

        JFrame f = new JFrame("外部窗体");

        f.setSize(800600);

        f.setLocation(100100);

 

        // 根据外部窗体实例化JDialog

        JDialog d = new JDialog(f);

        // 设置为模态

        d.setModal(true);

 

        d.setTitle("模态的对话框");

        d.setSize(400300);

        d.setLocation(200200);

        d.setLayout(null);

        JButton b = new JButton("一键秒对方基地挂");

        b.setBounds(505028030);

        d.add(b);

 

        f.setVisible(true);

        d.setVisible(true);

 

    }

}

步骤 4 : 窗体大小不可变化

通过调用方法 setResizable(false); 做到窗体大小不可变化

package gui;

 

import javax.swing.JButton;

import javax.swing.JFrame;

 

public class TestGUI {

    public static void main(String[] args) {

 

        JFrame f = new JFrame("LoL");

        f.setSize(400300);

        f.setLocation(200200);

        f.setLayout(null);

        JButton b = new JButton("一键秒对方基地挂");

        b.setBounds(505028030);

 

        f.add(b);

        // 窗体大小不可变化

        f.setResizable(false);

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 

        f.setVisible(true);

    }

}


更多内容,点击了解: https://how2j.cn/k/gui/gui-container/404.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值