AWT 常用组件 :
代码示例 :
import javax.swing.*;
import java.awt.*;
public class HelloAWT {
public static void main(String[] args) {
// Frame 默认的布局管理器就是 BorderLayout
Frame frame = new Frame("AWT 界面编程");
Box box = Box.createVerticalBox();
frame.add(box);
// 多行文本
TextArea textArea = new TextArea(5, 30);
textArea.setText("多行文本\n第一行\n第二行");
box.add(textArea);
// 下拉框
Choice choice = new Choice();
choice.add("下拉框1");
choice.add("下拉框2");
choice.add("下拉框3");
box.add(choice);
// 复选框
Checkbox checkbox = new Checkbox("复选框");
box.add(checkbox);
// 单选框, 默认选择第一项
CheckboxGroup checkboxGroup = new CheckboxGroup();
Checkbox checkbox1 = new Checkbox("单选1", checkboxGroup, true);
Checkbox checkbox2 = new Checkbox("单选2", checkboxGroup, false);
Checkbox checkbox3 = new Checkbox("单选3", checkboxGroup, false);
box.add(checkbox1);
box.add(checkbox2);
box.add(checkbox3);
// 文本框
TextField textField = new TextField(20);
textField.setText("文本框");
box.add(textField);
// 按钮
Button button = new Button("按钮");
box.add(button);
// 列表, 3 行, 可多选
List list = new List(3, true);
list.add("列表项1");
list.add("列表项2");
list.add("列表项3");
box.add(list);
// 自动设置 Frame 窗口合适的大小
frame.pack();
frame.setVisible(true);
}
}
执行效果 :
向多行文本框中输入文本 :
下拉框展示 :
复选框展示 :
单选展示 :
列表项多选 :