1. AWT(Abstract Window Toolkit)
AWT 是 Java 最早的 GUI 工具包,提供了一组用于创建窗口、按钮、菜单等基本控件的类。AWT 是基于本地窗口系统的,因此它的外观和行为会因操作系统而异。
1.1 AWT 核心组件
-
Frame(窗口)
AWT 的顶级窗口类,类似于 Swing 中的JFrame
。import java.awt.Frame; public class AWTFrameExample { public static void main(String[] args) { Frame frame = new Frame("AWT Frame Example"); frame.setSize(300, 200); frame.setVisible(true); } }
-
Button(按钮)
AWT 的按钮组件。import java.awt.Frame; import java.awt.Button; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class AWTButtonExample { public static void main(String[] args) { Frame frame = new Frame("AWT Button Example"); Button button = new Button("Click Me"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("Button clicked!"); } }); frame.add(button); frame.setSize(300, 200); frame.setVisible(true); } }
-
Label(标签)
用于显示文本信息。import java.awt.Frame; import java.awt.Label; public class AWTLableExample { public static void main(String[] args) { Frame frame = new Frame("AWT Label Example"); Label label = new Label("Hello, AWT!"); frame.add(label); frame.setSize(300, 200); frame.setVisible(true); } }
-
Panel(面板)
用于组织其他组件。import java.awt.Frame; import java.awt.Panel; import java.awt.Button; public class AWTPanelExample { public static void main(String[] args) { Frame frame = new Frame("AWT Panel Example"); Panel panel = new Panel(); Button button = new Button("Click Me"); panel.add(button); frame.add(panel); frame.setSize(300, 200); frame.setVisible(true); } }
1.2 AWT 布局管理器
AWT 提供了多种布局管理器,用于控制组件的排列方式。
-
FlowLayout(流式布局)
按从左到右的顺序排列组件。import java.awt.FlowLayout; import java.awt.Frame; import java.awt.Button; public class AWTFlowLayoutExample { public static void main(String[] args) { Frame frame = new Frame("AWT FlowLayout Example"); frame.setLayout(new FlowLayout()); frame.add(new Button("Button 1")); frame.add(new Button("Button 2")); frame.add(new Button("Button 3")); frame.setSize(300, 200); frame.setVisible(true); } }
-
BorderLayout(边界布局)
将组件放置在窗口的上、下、左、右和中心位置。import java.awt.BorderLayout; import java.awt.Frame; import java.awt.Button; public class AWTBorderLayoutExample { public static void main(String[] args) { Frame frame = new Frame("AWT BorderLayout Example"); frame.setLayout(new BorderLayout()); frame.add(new Button("North"), BorderLayout.NORTH); frame.add(new Button("South"), BorderLayout.SOUTH); frame.add(new Button("East"), BorderLayout.EAST); frame.add(new Button("West"), BorderLayout.WEST); frame.add(new Button("Center"), BorderLayout.CENTER); frame.setSize(300, 200); frame.setVisible(true); } }
-
GridLayout(网格布局)
将组件放置在网格中。import java.awt.GridLayout; import java.awt.Frame; import java.awt.Button; public class AWTGridLayoutExample { public static void main(String[] args) { Frame frame = new Frame("AWT GridLayout Example"); frame.setLayout(new GridLayout(2, 3)); for (int i = 1; i <= 6; i++) { frame.add(new Button("Button " + i)); } frame.setSize(300, 200); frame.setVisible(true); } }
1.3 AWT 事件处理
AWT 使用监听器接口来处理事件。
import java.awt.Frame;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AWTEventExample {
public static void main(String[] args) {
Frame frame = new Frame("AWT Event Example");
Button button = new Button("Click Me");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked!");
}
});
frame.add(button);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
2. Swing
Swing 是 Java 的一个轻量级 GUI 工具包,它基于 AWT,但提供了更丰富的组件和更好的跨平台一致性。Swing 组件是用纯 Java 编写的,因此它们的外观和行为不会因操作系统而异。
2.1 Swing 核心组件
-
JFrame(窗口)
Swing 的顶级窗口类。import javax.swing.JFrame; public class SwingFrameExample { public static void main(String[] args) { JFrame frame = new JFrame("Swing Frame Example"); frame.setSize(300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
-
JButton(按钮)
Swing 的按钮组件。import javax.swing.JFrame; import javax.swing.JButton; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class SwingButtonExample { public static void main(String[] args) { JFrame frame = new JFrame("Swing Button Example"); JButton button = new JButton("Click Me"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("Button clicked!"); } }); frame.add(button); frame.setSize(300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
-
JLabel(标签)
用于显示文本或图标。import javax.swing.JFrame; import javax.swing.JLabel; public class SwingLabelExample { public static void main(String[] args) { JFrame frame = new JFrame("Swing Label Example"); JLabel label = new JLabel("Hello, Swing!"); frame.add(label); frame.setSize(300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
-
JPanel(面板)
用于组织其他组件。import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton; public class SwingPanelExample { public static void main(String[] args) { JFrame frame = new JFrame("Swing Panel Example"); JPanel panel = new JPanel(); JButton button = new JButton("Click Me"); panel.add(button); frame.add(panel); frame.setSize(300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
2.2 Swing 布局管理器
Swing 继承了 AWT 的布局管理器,并添加了一些新的布局管理器。
-
BorderLayout(边界布局)
与 AWT 的BorderLayout
类似。import javax.swing.JFrame; import javax.swing.JButton; import java.awt.BorderLayout; public class SwingBorderLayoutExample { public static void main(String[] args) { JFrame frame = new JFrame("Swing BorderLayout Example"); frame.setLayout(new BorderLayout()); frame.add(new JButton("North"), BorderLayout.NORTH); frame.add(new JButton("South"), BorderLayout.SOUTH); frame.add(new JButton("East"), BorderLayout.EAST); frame.add(new JButton("West"), BorderLayout.WEST); frame.add(new JButton("Center"), BorderLayout.CENTER); frame.setSize(300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
-
GridLayout(网格布局)
与 AWT 的GridLayout
类似。import javax.swing.JFrame; import javax.swing.JButton; import java.awt.GridLayout; public class SwingGridLayoutExample { public static void main(String[] args) { JFrame frame = new JFrame("Swing GridLayout Example"); frame.setLayout(new GridLayout(2, 3)); for (int i = 1; i <= 6; i++) { frame.add(new JButton("Button " + i)); } frame.setSize(300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
-
BoxLayout(盒布局)
用于将组件排列成一行或一列。import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton; import javax.swing.BoxLayout; public class SwingBoxLayoutExample { public static void main(String[] args) { JFrame frame = new JFrame("Swing BoxLayout Example"); JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.add(new JButton("Button 1")); panel.add(new JButton("Button 2")); panel.add(new JButton("Button 3")); frame.add(panel); frame.setSize(300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
2.3 Swing 事件处理
Swing 使用监听器接口来处理事件,与 AWT 类似。
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SwingEventExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Swing Event Example");
JButton button = new JButton("Click Me");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked!");
}
});
frame.add(button);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
2.4 Swing 的高级组件
-
JTextField(文本框)
用于输入单行文本。import javax.swing.JFrame; import javax.swing.JTextField; public class SwingTextFieldExample { public static void main(String[] args) { JFrame frame = new JFrame("Swing TextField Example"); JTextField textField = new JTextField("Enter text here", 20); frame.add(textField); frame.setSize(300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
-
JTextArea(文本区域)
用于输入多行文本。import javax.swing.JFrame; import javax.swing.JTextArea; public class SwingTextAreaExample { public static void main(String[] args) { JFrame frame = new JFrame("Swing TextArea Example"); JTextArea textArea = new JTextArea("Enter text here", 10, 30); frame.add(textArea); frame.setSize(400, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
-
JCheckBox(复选框)
用于选择多个选项。import javax.swing.JFrame; import javax.swing.JCheckBox; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; public class SwingCheckBoxExample { public static void main(String[] args) { JFrame frame = new JFrame("Swing CheckBox Example"); JCheckBox checkBox = new JCheckBox("Check me"); checkBox.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { System.out.println("Checked: " + checkBox.isSelected()); } }); frame.add(checkBox); frame.setSize(300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
-
JRadioButton(单选按钮)
用于选择一个选项。import javax.swing.JFrame; import javax.swing.JRadioButton; import javax.swing.ButtonGroup; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; public class SwingRadioButtonExample { public static void main(String[] args) { JFrame frame = new JFrame("Swing RadioButton Example"); JRadioButton radioButton1 = new JRadioButton("Option 1"); JRadioButton radioButton2 = new JRadioButton("Option 2"); ButtonGroup group = new ButtonGroup(); group.add(radioButton1); group.add(radioButton2); radioButton1.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { System.out.println("Selected: " + radioButton1.getText()); } }); radioButton2.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { System.out.println("Selected: " + radioButton2.getText()); } }); frame.add(radioButton1); frame.add(radioButton2); frame.setSize(300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
-
JComboBox(下拉列表)
用于选择一个选项。import javax.swing.JFrame; import javax.swing.JComboBox; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; public class SwingComboBoxExample { public static void main(String[] args) { JFrame frame = new JFrame("Swing ComboBox Example"); String[] options = {"Option 1", "Option 2", "Option 3"}; JComboBox<String> comboBox = new JComboBox<>(options); comboBox.addActionListener(e -> { System.out.println("Selected: " + comboBox.getSelectedItem()); }); frame.add(comboBox); frame.setSize(300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
-
JTable(表格)
用于显示表格数据。import javax.swing.JFrame; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class SwingTableExample { public static void main(String[] args) { JFrame frame = new JFrame("Swing Table Example"); String[] columnNames = {"Name", "Age", "City"}; Object[][] data = { {"John", 25, "New York"}, {"Alice", 30, "Los Angeles"}, {"Bob", 35, "Chicago"} }; DefaultTableModel model = new DefaultTableModel(data, columnNames); JTable table = new JTable(model); frame.add(new JScrollPane(table)); frame.setSize(400, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
3. AWT 2D 绘图
AWT 2D 提供了强大的二维图形绘制功能,包括绘制形状、文本、图像等。
3.1 绘图组件
-
Canvas(画布)
用于绘制图形。import java.awt.Canvas; import java.awt.Color; import java.awt.Frame; import java.awt.Graphics; public class AWTCanvasExample extends Canvas { public AWTCanvasExample() { setSize(400, 300); setBackground(Color.WHITE); } @Override public void paint(Graphics g) { g.setColor(Color.RED); g.fillRect(50, 50, 100, 100); g.setColor(Color.BLUE); g.fillOval(200, 50, 100, 100); g.setColor(Color.GREEN); g.drawLine(50, 200, 350, 200); } public static void main(String[] args) { Frame frame = new Frame("AWT Canvas Example"); AWTCanvasExample canvas = new AWTCanvasExample(); frame.add(canvas); frame.setSize(400, 300); frame.setVisible(true); } }
3.2 绘图方法
-
绘制矩形
g.fillRect(x, y, width, height); g.drawRect(x, y, width, height);
-
绘制圆形
g.fillOval(x, y, width, height); g.drawOval(x, y, width, height);
-
绘制线条
g.drawLine(x1, y1, x2, y2);
-
绘制文本
g.drawString(text, x, y);
-
绘制图像
g.drawImage(image, x, y, width, height, observer);
3.3 颜色和字体
-
设置颜色
g.setColor(Color.RED);
-
设置字体
g.setFont(new Font("Arial", Font.BOLD, 20));
3.4 转换和变换
-
平移
g.translate(x, y);
-
旋转
g.rotate(angle, x, y);
-
缩放
g.scale(xScale, yScale);
3.5 综合示例
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Font;
public class AWT2DExample extends Canvas {
public AWT2DExample() {
setSize(400, 300);
setBackground(Color.WHITE);
}
@Override
public void paint(Graphics g) {
// 绘制矩形
g.setColor(Color.RED);
g.fillRect(50, 50, 100, 100);
// 绘制圆形
g.setColor(Color.BLUE);
g.fillOval(200, 50, 100, 100);
// 绘制线条
g.setColor(Color.GREEN);
g.drawLine(50, 200, 350, 200);
// 绘制文本
g.setColor(Color.BLACK);
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString("Hello, AWT 2D!", 50, 250);
// 绘制带变换的图形
g.translate(100, 100);
g.rotate(Math.toRadians(45));
g.setColor(Color.ORANGE);
g.fillRect(0, 0, 50, 50);
}
public static void main(String[] args) {
Frame frame = new Frame("AWT 2D Example");
AWT2DExample canvas = new AWT2DExample();
frame.add(canvas);
frame.setSize(400, 300);
frame.setVisible(true);
}
}
4. 总结
-
AWT
- 提供了基本的 GUI 组件和事件处理机制。
- 使用本地窗口系统,外观和行为依赖于操作系统。
- 适合简单的 GUI 应用程序。
-
Swing
- 基于 AWT,但提供了更丰富的组件和更好的跨平台一致性。
- 使用纯 Java 实现,外观和行为不依赖于操作系统。
- 支持复杂的布局和高级组件,适合复杂的 GUI 应用程序。
-
AWT 2D
- 提供了强大的二维图形绘制功能。
- 支持绘制形状、文本、图像等。
- 适合需要自定义绘图的应用程序。