java的图形界面中,容器是用来存放 按钮,输入框等组件的。
窗体型容器有两个,一个是JFrame,一个是JDialog
步骤1:JFrame
步骤2:JDialog
步骤3:模态JDialog
步骤4:窗体大小不可变化
步骤5:练习-模态与大小变化
步骤6:答案-模态与大小变化
步骤 1 : JFrame
JFrame是最常用的窗体型容器,默认情况下,在下载区(点击进入)有最大化最小化按钮
![JFrame](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9zdGVwaW1hZ2V3bS5ob3cyai5jbi84ODcucG5n?x-oss-process=image/format,png)
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( 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 );
}
}
|
步骤 2 : JDialog
JDialog也是窗体型容器,下载区(点击进入)没有最大和最小化按钮
![JDialog](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9zdGVwaW1hZ2V3bS5ob3cyai5jbi84ODgucG5n?x-oss-process=image/format,png)
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( 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 );
}
}
|
步骤 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( 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 );
}
}
|
步骤 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( 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 );
}
}
|
更多内容,点击了解: https://how2j.cn/k/gui/gui-container/404.html