AWT
總體上Swing組件替代了絕大部分AWT組件,對AWT圖形用戶界面編程有極好的補充和加強。
package ch11;
import java.awt.*;
/**
* Created by Jiqing on 2016/12/1.
*/
public class FrameTest {
public static void main(String[] args) {
Frame f = new Frame("測試窗口");
// 設置窗口的大小、位置
f.setBounds(30,30,250,200);
// 將窗口顯示出來
f.setVisible(true);
}
}
AWT容器
圖形界面編程,類似於小朋友玩的拼圖游戲,容器類似於拼圖的母板,普通組件類似於拼圖圖塊。
package ch11;
import java.awt.*;
/**
* Created by Jiqing on 2016/12/1.
*/
public class PanelTest {
public static void main(String[] args) {
Frame f = new Frame("測試頁面");
Panel p = new Panel();
p.add(new TextField(20));
p.add(new Button("Click Me"));
f.add(p);
f.setBounds(30,30,250,120);
f.setVisible(true);
}
}
FlowLayout布局管理器
- 組件像水流一樣向某方向流動,遇到障礙就折回,重頭開始排列。
package ch11;
import java.awt.*;
/**
* Created by Jiqing on 2016/12/1.
*/
public class FlowLayoutTest {
public static void main(String[] args) {
Frame f = new Frame("測試窗口");
// 設置Frame使用FlowLayout布局管理器
f.setLayout(new FlowLayout(FlowLayout.LEFT,20,5));
// 向窗口添加10個按鈕
for (int i = 0;i<10;i++) {
f.add(new Button("Button"+i));
}
// 設置窗口為最佳大小
f.pack();
f.setVisible(true);
}
}
BorderLayout布局管理器
- BorderLayout將容器分為EAST、SOUTH、WEST、NORTH、CENTER五個區域。普通組件可以放在這五個區域中的任意一個。
- 默認添加到中間區域。
package ch11;
import java.awt.*;
import static java.awt.BorderLayout.*;
/**
* Created by Jiqing on 2016/12/1.
*/
public class BorderLayoutTest {
public static void main(String[] args) {
Frame f = new Frame("測試窗口");
// 設置Frame容器使用BorderLayout布局管理器
f.setLayout(new BorderLayout(50,50));
f.add(new Button("S"),SOUTH);
f.add(new Button("N"),NORTH);
// 默認添加到中間區域
f.add(new Button("M"));
f.add(new Button("E"),EAST);
f.add(new Button("W"),WEST);
f.pack();
f.setVisible(true);
}
}
GirdLayout布局管理器
GirdLayout布局管理器將容器分割成縱橫分割的網格,每個網格所占據的領域大小相同。
package ch11;
import java.awt.*;
import static java.awt.BorderLayout.*; // 默認布局
/**
* Created by Jiqing on 2016/12/1.
*/
public class GridLayoutTest {
public static void main(String[] args) {
Frame f = new Frame("計算器");
Panel p1 = new Panel();
p1.add(new TextField(30));
f.add(p1,NORTH); // import static java.awt.BorderLayout.*;
Panel p2 = new Panel();
p2.setLayout(new GridLayout(3,5,4,4));
String[] name = {"0","1","2","3","4","5","6","7","8","9","+","-","*","/","."};
for (int i = 0;i<name.length;i++) {
p2.add(new Button(name[i]));
}
f.add(p2);
f.pack();
f.setVisible(true);
}
}
GridBagLayout
-
與GridLayout不同,它可以跨越一個或多個網絡,並可以設置網絡的大小互不相同。
-
增加布局的靈活性
-
使用步驟
- 創建GridBagLayout布局管理器
- 創建GridBagConstraints對象
- 建立關聯
- 添加組件
package ch11;
import java.awt.*;
/**
* Created by Jiqing on 2016/12/2.
*/
public class GridBagTest {
private Frame f = new Frame("測試窗口");
private GridBagLayout gb = new GridBagLayout();
private GridBagConstraints gbc = new GridBagConstraints();
private Button[] bs = new Button[10];
public void init() {
f.setLayout(gb);
for (int i = 0;i<bs.length;i++) {
bs[i] = new Button("Button"+i);
}
// 所有組件都可以橫向、縱向擴大
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
addButton(bs[0]);
addButton(bs[1]);
addButton(bs[2]);
gbc.gridwidth = GridBagConstraints.REMAINDER;
addButton(bs[3]);
gbc.weightx = 0;
addButton(bs[4]);
gbc.gridwidth = 2;
addButton(bs[5]);
gbc.gridwidth = 1;
gbc.gridheight = 2;
gbc.gridwidth = GridBagConstraints.REMAINDER;
addButton(bs[6]);
gbc.gridwidth = 1;
gbc.gridheight =2;
gbc.weighty = 1;
addButton(bs[7]);
gbc.weighty = 0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridheight = 1;
addButton(bs[8]);
addButton(bs[9]);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
new GridBagTest().init();
}
private void addButton(Button button) {
gb.setConstraints(button,gbc);
f.add(button);
}
}
BoxLayout
- BoxLayout 可以在垂直和水平兩個方向擺放控件
package ch11;
import javax.swing.*;
import java.awt.*;
/**
* Created by Jiqing on 2016/12/2.
*/
public class BoxLayoutTest {
private Frame f = new Frame("測試");
public void init() {
f.setLayout(new BoxLayout(f,BoxLayout.Y_AXIS));
// 下面按鈕將垂直排列
f.add(new Button("First Button"));
f.add(new Button("Second Button"));
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
new BoxLayoutTest().init();
}
}
package ch11;
import javax.swing.*;
import java.awt.*;
/**
* Created by Jiqing on 2016/12/2.
*/
public class BoxTest {
private Frame f = new Frame("測試");
// 定義水平組件Box對象
private Box horizontal = Box.createHorizontalBox();
// 定義垂直組件Box對象
private Box verical = Box.createVerticalBox();
public void init() {
horizontal.add(new Button("H btn1"));
horizontal.add(new Button("H btn2"));
verical.add(new Button("V btn1"));
verical.add(new Button("V btn2"));
f.add(horizontal,BorderLayout.NORTH);
f.add(verical);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
new BoxTest().init();
}
}
常用的
package ch11;
import javax.swing.*;
import java.awt.*;
/**
* Created by Jiqing on 2016/12/2.
*/
public class CommonComponent {
Frame f = new Frame("測試");
Button ok = new Button("Confirm");
CheckboxGroup cbg = new CheckboxGroup();
Checkbox male = new Checkbox("Male",cbg,true);
Checkbox female = new Checkbox("Female",cbg,false);
Choice colorChooser = new Choice();
List colorList = new List(6,true);
TextArea ta = new TextArea(5,20);
TextField name = new TextField(50);
public void init() {
colorChooser.add("red");
colorChooser.add("green");
colorChooser.add("blue");
colorList.add("red");
colorList.add("green");
colorList.add("blue");
Panel bottom = new Panel();
bottom.add(name);
bottom.add(ok);
f.add(bottom,BorderLayout.SOUTH);
Panel checkPanel = new Panel();
checkPanel.add(colorChooser);
checkPanel.add(male);
checkPanel.add(female);
Box topLeft = Box.createVerticalBox();
topLeft.add(ta);
topLeft.add(checkPanel);
Box top = Box.createHorizontalBox();
top.add(topLeft);
top.add(colorList);
f.add(top);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
new CommonComponent().init();
}
}

Dialog對話框
- 對話框
package ch11;
import java.awt.*;
/**
* Created by Jiqing on 2016/12/2.
*/
public class DialogTest {
Frame f = new Frame("測試");
Dialog d1 = new Dialog(f,"Modal Dialog",true);
Dialog d2 = new Dialog(f,"Not Modal Dialog",false);
Button b1 = new Button("Open MD");
Button b2 = new Button("Open NMD");
public void init() {
d1.setBounds(20,30,300,400);
d2.setBounds(20,30,300,400);
b1.addActionListener(e->d1.setVisible(true));
b2.addActionListener(e->d2.setVisible(true));
f.add(b1);
f.add(b2,BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
new DialogTest().init();
}
}
package ch11;
import java.awt.*;
/**
* Created by Jiqing on 2016/12/2.
*/
public class FileDialogTest {
Frame f = new Frame("測試");
FileDialog d1 = new FileDialog(f,"load file",FileDialog.LOAD);
FileDialog d2 = new FileDialog(f,"save file",FileDialog.SAVE);
Button b1 = new Button("open");
Button b2 = new Button("save");
public void init() {
b1.addActionListener(e->{
d1.setVisible(true);
System.out.println(d1.getDirectory()+d1.getFile());
});
b2.addActionListener(e->{
d2.setVisible(true);
System.out.println(d2.getDirectory()+d2.getFile());
});
f.add(b1);
f.add(b2,BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
new FileDialogTest().init();
}
}

