Swing 的容器 JFrame和JDialog
java的圖形界面中,容器是用來存放 按鈕,輸入框等組件的,
表單型容器有兩個,一個是JFrame,一個是JDialog
步驟 1 : 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(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也是表單型容器,右上角沒有最大和最小化按鈕

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);
}
}
更多內容,點擊了解: Swing 的容器 JFrame和JDialog
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/93422.html
標籤:Java
