五種JAVA GUI布局管理的方式
1. 流式布局(FlowLayout)
定義:通俗地說,流式布局就是根據(jù)窗口大小,自動改變窗口內(nèi)組件的位置。例如:原窗口大小一行可以容納10個BUTTON,但將窗口縮小后,每行僅能容納5個BUTTON,此時原先的10個BUTTON中的五個就會自動排列到下一行。
示例:(省略panel的使用)
Hashset
package 布局管理;import java.awt.*;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class Layout { //流式布局 public static void main(String[] args) { Frame frame = new Frame(); //創(chuàng)建一個窗口 frame.setLayout(new FlowLayout(FlowLayout.LEFT)); //將窗口布局設置為流式布局,并設置向左對齊 Button button1 = new Button('button1'); //創(chuàng)建若干個按鈕 Button button2 = new Button('button2'); Button button3 = new Button('button3'); Button button4 = new Button('button4'); Button button5 = new Button('button5'); frame.add(button1); //將按鈕添加進窗口中 frame.add(button2); frame.add(button3); frame.add(button4); frame.add(button5); frame.setBounds(200,200,500,500); //設置窗口的位置與大小 frame.setVisible(true); //設置窗口可見性 frame.addWindowListener(new WindowAdapter() { //事件監(jiān)聽:關閉窗口 @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); }}
2. 東西南北中布局(BorderLayout)
定義:東西南北中布局,顧名思義。將窗口分為東西南北中四個“塊兒”,也可以稱作上下左右中布局,便于理解。
示例:(省略panel的使用)
package 布局管理;import java.awt.*;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class BorderLayout { //東西南北中布局 public static void main(String[] args) { Frame frame = new Frame(); frame.setLayout(new java.awt.BorderLayout()); Button east = new Button('East'); east.setBackground(new Color(140, 172, 51)); Button west = new Button('West'); west.setBackground(new Color(140, 172, 51)); Button north = new Button('North'); north.setBackground(new Color(38, 222, 135)); Button south = new Button('South'); south.setBackground(new Color(38, 222, 135)); Button center = new Button('Center'); frame.add(east, java.awt.BorderLayout.EAST); frame.add(west, java.awt.BorderLayout.WEST); frame.add(north, java.awt.BorderLayout.NORTH); frame.add(south, java.awt.BorderLayout.SOUTH); frame.add(center, java.awt.BorderLayout.CENTER); frame.setBounds(200,200,500,500); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); }}
3. 表格式布局
定義:將窗口拆分為若干個表格(拆分為自己需要的表格),再往里添加組件。
示例:GUI經(jīng)常使用的“登錄”、“注冊”界面
package 布局管理;import javafx.scene.control.PasswordField;import javafx.scene.layout.Pane;import sun.security.util.Password;import java.awt.*;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.nio.charset.Charset;public class GridLayout { public static void main(String[] args) {// System.out.println('file.encoding=' + System.getProperties().getProperty('file.encoding'));// System.out.println('默認是:'+ Charset.defaultCharset().name()); Frame frame = new Frame('表格布局'); frame.setLayout(new java.awt.GridLayout(4,1)); //設置行列數(shù) Panel panel1 = new Panel(); frame.add(panel1); Panel panel2 = new Panel(); frame.add(panel2); Panel panel3 = new Panel(); frame.add(panel3); Panel panel4 = new Panel(); frame.add(panel4); Label label = new Label('welcome to *** system'); label.setFont(new Font('宋體', Font.PLAIN, 26)); Label label1 = new Label('Account: '); TextField textField = new TextField(); textField.setColumns(20); Label label2 = new Label('Password: '); TextField textField1 = new TextField(); //AWT沒有passwordField textField1.setColumns(20); textField1.setEchoChar(’*’); Button button = new Button('Login'); panel1.add(label); panel2.add(label1); panel2.add(textField); panel3.add(label2); panel3.add(textField1); panel4.add(button); frame.setBounds(200,200,500,250); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); }}
4. 我不要布局!!!當設置 frame.setLayout(null) ;時,可以使用給panel設置坐標的方式控制布局,更具靈活性!
package AWT;import javafx.scene.layout.Pane;import java.awt.*;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;public class panel { public static void main(String[] args) { Frame frame = new Frame(); //new一個窗口出來 Panel panel = new Panel(); //new一個面板 Panel panel1 = new Panel(); //new一個面板 Panel panel2 = new Panel(); //new一個面板 frame.setLayout(null); //設置布局為空 frame.setBounds(200,200,500,500); //設置窗口位置大小 panel.setBounds(20,15,460,50); //設置第一個面板 panel.setBackground(new Color(253, 228,1)); //設置面板顏色 panel1.setBounds(20,70,100,415); panel1.setBackground(new Color(0, 71, 254)); panel2.setBounds(130,70,350,415); panel2.setBackground(new Color(1,1,1)); frame.add(panel); //面板加入到窗口中 frame.add(panel1); frame.add(panel2); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); { } }}
5. 我都要!!!
為了使界面更加美觀,可以使用多種布局的嵌套哦!
以上就是五種JAVA GUI布局管理的方式的詳細內(nèi)容,更多關于JAVA GUI布局的資料請關注好吧啦網(wǎng)其它相關文章!
相關文章:
1. bootstrap select2 動態(tài)從后臺Ajax動態(tài)獲取數(shù)據(jù)的代碼2. js select支持手動輸入功能實現(xiàn)代碼3. ASP常用日期格式化函數(shù) FormatDate()4. 網(wǎng)頁中img圖片使用css實現(xiàn)等比例自動縮放不變形(代碼已測試)5. html中的form不提交(排除)某些input 原創(chuàng)6. CSS3中Transition屬性詳解以及示例分享7. vue使用moment如何將時間戳轉為標準日期時間格式8. python 如何在 Matplotlib 中繪制垂直線9. jsp文件下載功能實現(xiàn)代碼10. 開發(fā)效率翻倍的Web API使用技巧
