目录
上:Java程序设计复习提纲(上:入门语法) - 孤飞 - 博客园 (cnblogs.com
-
数据类型和关键字
-
数组与字符串
中:Java程序设计复习提纲(中:面向对象) - 孤飞 - 博客园 (cnblogs.com
- 面向对象和类
下:本文
- 图形界面
GUI概述
以下是关于Java GUI设计的一些基本知识:
-
容器(Containers):容器是用来包含其他组件或容器的组件。例如,面板(Panel)和框架(Frame)就是两种常用的容器。容器也可以设置布局管理器(Layout Manager),用来控制其包含的组件如何排列。
-
事件处理(Event Handling):用户与GUI程序的交互主要通过事件(Event)来进行。例如,点击按钮、输入文本、选中复选框等操作都会产生事件。程序需要提供事件处理器(Event Handler)来响应这些事件。
-
绘图和动画:Java GUI程序可以使用2D绘图API来绘制各种图形(例如线、形状、文本等),并可以使用动画API来创建动画效果。
组件(Components):组件是GUI设计的基本元素,比如按钮、文本框、标签、复选框、下拉列表等。每一个组件都有很多属性可以设置,例如大小、位置、颜色等。
import javax.swing.JFrame;
import javax.swing.JButton;
public class SimpleGUI {
public static void main(String[] args {
// Create a new frame (a window
JFrame frame = new JFrame("My First GUI";
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE;
frame.setSize(300, 200;
// Create a new button
JButton button = new JButton("Click me!";
// Add the button to the frame
frame.getContentPane(.add(button;
// Make the frame visible
frame.setVisible(true;
}
}
在这个例子中,我们创建了一个新的窗口,然后在窗口中添加了一个按钮。当运行这个程序时,你将会看到一个窗口,窗口中有一个可以点击的按钮。
容器
-
JFrame:这是一个顶层容器,通常作为应用程序的主窗口。一个JFrame对象有标题栏、最小化/最大化/关闭按钮、状态栏等。它可以包含其他容器和组件。默认情况下,关闭JFrame窗口不会终止程序,需要通过调用
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE
方法设置当关闭窗口时终止程序。
JFrame frame = new JFrame("Title"; // create a new frame with the title "Title"
- JPanel:JPanel是一种通用的轻量级容器,可以用于组织其他组件和容器。通常,你会在一个JFrame窗口中添加一个或多个JPanel,然后在JPanel中添加按钮、标签等组件。
JPanel panel = new JPanel(; // create a new panel
- JDialog:JDialog是一种顶层容器,通常用于创建弹出窗口。一个JDialog可以是模态的(当打开JDialog时,用户不能与其他窗口交互)或非模态的。
JDialog dialog = new JDialog(frame, "Dialog", true; // create a new modal dialog with the title "Dialog"
- JScrollPane:JScrollPane是一种特殊的容器,可以为其包含的组件提供滚动条。当组件的大小超过JScrollPane的大小时,滚动条会自动出现。
JScrollPane scrollPane = new JScrollPane(textArea; // create a new scroll pane that contains a text area
容器的布局可以通过布局管理器(Layout Manager)来管理。布局管理器可以控制容器中的组件如何排列和调整大小。Java提供了几种内置的布局管理器,如FlowLayout、BorderLayout、GridLayout、BoxLayout等。
add方法实现。例如,frame.add(button
将一个按钮添加到一个框架中。
组件
-
JButton:按钮组件,常用于触发某种行为或命令。
JButton button = new JButton("Click me!";
- JLabel:标签组件,用于显示文本或图像。
JLabel label = new JLabel("Hello, World!";
- JTextField:文本字段组件,用于输入和编辑单行文本。
JTextField textField = new JTextField(20; // 20 columns wide
- JTextArea:文本区域组件,用于输入和编辑多行文本。
JTextArea textArea = new JTextArea(5, 20; // 5 rows and 20 columns
- JCheckBox:复选框组件,允许用户选择多个选项。
JCheckBox checkBox = new JCheckBox("Check me";
- JRadioButton:单选按钮组件,允许用户从多个选项中选择一个。
JRadioButton radioButton = new JRadioButton("Select me";
- JComboBox:下拉列表框组件,允许用户从下拉列表中选择一个选项。
String[] choices = {"Choice 1", "Choice 2", "Choice 3"};
JComboBox comboBox = new JComboBox(choices;
- JSlider:滑块组件,允许用户通过拖动滑块选择一个值。
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 50, 25; // min: 0, max: 50, initial: 25
- JMenu, JMenuItem, JMenuBar:菜单组件,用于创建菜单栏和菜单项。
JMenuBar menuBar = new JMenuBar(;
JMenu menu = new JMenu("Menu";
JMenuItem menuItem = new JMenuItem("Menu Item";
menu.add(menuItem;
menuBar.add(menu;
所有的这些组件都是从javax.swing.JComponent
类继承的,这意味着他们都有一些公共的属性和方法,例如背景色、前景色、字体、工具提示文本等。另外,这些组件都可以注册事件监听器,以响应用户的输入(如点击、键入等)。
布局
-
FlowLayout:这是默认的布局管理器,将组件从左到右、从上到下排列,类似于英文文本的阅读顺序。
FlowLayout flowLayout = new FlowLayout(;
container.setLayout(flowLayout;
- BorderLayout:将容器分为5个区域:North(上)、South(下)、East(右)、West(左)和Center(中)。
BorderLayout borderLayout = new BorderLayout(;
container.setLayout(borderLayout;
- GridLayout:将容器分为等大小的网格(行和列),每个组件填充一个网格单元。
GridLayout gridLayout = new GridLayout(3, 2; // 3 rows and 2 columns
container.setLayout(gridLayout;
- BoxLayout:将组件沿一个方向(水平或垂直)排列。
BoxLayout boxLayout = new BoxLayout(container, BoxLayout.X_AXIS; // horizontal layout
container.setLayout(boxLayout;
- GridBagLayout:这是最灵活的布局管理器,允许组件跨多个行和列,并可以指定组件的填充和间距。但是,这也是最复杂的布局管理器。
GridBagLayout gridBagLayout = new GridBagLayout(;
container.setLayout(gridBagLayout;
每个容器都有一个默认的布局管理器,例如JFrame和JDialog默认使用BorderLayout,而JPanel和JApplet默认使用FlowLayout。你可以通过调用容器的setLayout
方法更改布局管理器。
事件监听与处理
在Java GUI编程中,事件监听和处理是非常重要的概念。事件是用户与应用程序交互的方式,例如点击按钮、键入文本、移动鼠标等。事件监听器(Event Listener)是一个接口,包含了响应特定事件的方法。当事件发生时,事件源(Event Source,如按钮或文本框)会调用监听器的方法。
-
ActionEvent:当用户对具有动作的对象(如按钮或菜单项)进行操作时,就会发生此事件。对应的监听器是
ActionListener
。
button.addActionListener(new ActionListener( {
@Override
public void actionPerformed(ActionEvent e {
// handle the button click
}
};
-
MouseEvent:当用户对鼠标进行操作(如点击、移动或拖动)时,就会发生此事件。对应的监听器是
MouseListener
和MouseMotionListener
。
button.addMouseListener(new MouseAdapter( {
@Override
public void mouseClicked(MouseEvent e {
// handle the mouse click
}
};
-
KeyEvent:当用户对键盘进行操作(如按键或释放键)时,就会发生此事件。对应的监听器是
KeyListener
。
textField.addKeyListener(new KeyAdapter( {
@Override
public void keyPressed(KeyEvent e {
// handle the key press
}
};
-
ItemEvent:当用户对可选项目(如复选框或列表)进行操作时,就会发生此事件。对应的监听器是
ItemListener
。
checkBox.addItemListener(new ItemListener( {
@Override
public void itemStateChanged(ItemEvent e {
// handle the item state change
}
};
-
WindowEvent:当用户对窗口进行操作(如打开、关闭或最大化)时,就会发生此事件。对应的监听器是
WindowListener
。
frame.addWindowListener(new WindowAdapter( {
@Override
public void windowClosing(WindowEvent e {
// handle the window closing
}
};
注意:MouseAdapter
、KeyAdapter
和WindowAdapter
是MouseListener
、KeyListener
和WindowListener
的适配器类。适配器类是空实现了监听器接口的类,这样你可以只覆盖你感兴趣的方法,而不是所有的方法。例如,如果你只关心鼠标点击,那么你可以只覆盖mouseClicked
方法,而不需要覆盖mouseEntered
、mouseExited
、mousePressed
和mouseReleased
方法。
例子
我们使用上面提到的容器、组件、布局和事件监听处理来实现一个简易加减法计算器
程序代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator {
public static void main(String[] args {
// 创建一个新的JFrame实例作为主窗口
JFrame frame = new JFrame("Simple Calculator";
// 创建一个新的JPanel实例作为主面板
JPanel panel = new JPanel(;
// 设置主面板的布局为GridLayout
panel.setLayout(new GridLayout(2, 3, 10, 10;
// 创建组件
JTextField textField1 = new JTextField(10;
textField1.setFont(new Font("Arial", Font.PLAIN, 20;
JTextField textField2 = new JTextField(10;
textField2.setFont(new Font("Arial", Font.PLAIN, 20;
JButton addButton = new JButton("+";
addButton.setFont(new Font("Arial", Font.PLAIN, 30;
JButton subtractButton = new JButton("-";
subtractButton.setFont(new Font("Arial", Font.PLAIN, 30;
JTextField resultField = new JTextField(10;
resultField.setFont(new Font("Arial", Font.PLAIN, 20;
resultField.setEditable(false;
// 添加组件到主面板
panel.add(textField1;
panel.add(textField2;
panel.add(resultField;
panel.add(addButton;
panel.add(subtractButton;
// 添加事件监听器到加号按钮
addButton.addActionListener(new ActionListener( {
@Override
public void actionPerformed(ActionEvent e {
try {
int num1 = Integer.parseInt(textField1.getText(;
int num2 = Integer.parseInt(textField2.getText(;
int sum = num1 + num2;
resultField.setText(Integer.toString(sum;
} catch (NumberFormatException ex {
JOptionPane.showMessageDialog(frame, "Invalid input. Please enter only integers.";
textField1.setText("";
textField2.setText("";
}
}
};
// 添加事件监听器到减号按钮
subtractButton.addActionListener(new ActionListener( {
@Override
public void actionPerformed(ActionEvent e {
try {
int num1 = Integer.parseInt(textField1.getText(;
int num2 = Integer.parseInt(textField2.getText(;
int diff = num1 - num2;
resultField.setText(Integer.toString(diff;
} catch (NumberFormatException ex {
JOptionPane.showMessageDialog(frame, "Invalid input. Please enter only integers.";
textField1.setText("";
textField2.setText("";
}
}
};
// 将主面板添加到主窗口
frame.add(panel;
// 设置主窗口的大小、位置和关闭操作
frame.setSize(600, 200;
frame.setLocationRelativeTo(null;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE;
// 显示主窗口
frame.setVisible(true;
}
}