五種JAVA GUI佈局管理的方式
阿新 • • 發佈:2020-06-16
1. 流式佈局(FlowLayout)
定義:
通俗地說,流式佈局就是根據視窗大小,自動改變視窗內元件的位置。例如:原視窗大小一行可以容納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(); //建立一個視窗 frame.setLayout(new FlowLayout(FlowLayout.LEFT)); //將窗口布局設定為流式佈局,並設定向左對齊 Button button1 = new Button("button1"); //建立若干個按鈕 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() { //事件監聽:關閉視窗 @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,51)); Button north = new Button("North"); north.setBackground(new Color(38,222,135)); Button south = new Button("South"); south.setBackground(new Color(38,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,500); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
3. 表格式佈局
定義:
將視窗拆分為若干個表格(拆分為自己需要的表格),再往裡新增元件。
示例:
GUI經常使用的“登入”、“註冊”介面
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)); //設定行列數 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,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,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,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佈局管理的方式的詳細內容,更多關於JAVA GUI佈局的資料請關注我們其它相關文章!