JavaGUI常用组件介绍
立即解锁
发布时间: 2025-08-17 02:24:59 阅读量: 2 订阅数: 14 

### Java GUI 常用组件介绍
在 Java 编程中,有许多实用的 Swing 组件可用于创建图形用户界面(GUI)。下面将详细介绍几种常见的 GUI 组件及其使用方法。
#### 1. JCheckBox
JCheckBox 类用于表示复选框按钮,它在呈现一组二进制(是/否、真/假)选项时非常有用。例如,在一个选择编程语言的界面中,用户可以通过点击相应的复选框来选择自己会使用的编程语言。
- **创建复选框**:
```java
JCheckBox cbBtn = new JCheckBox("Java");
```
- **检查复选框状态**:可以使用 `isSelected` 方法来检查复选框是否被选中。
```java
if (cbBtn.isSelected()) {
System.out.println("You can program in" + cbBtn.getText());
} else {
System.out.println("You cannot program in " + cbBtn.getText());
}
```
- **获取和设置复选框文本**:使用 `getText` 方法获取复选框的文本,使用 `setText` 方法更改文本。
以下是一个完整的示例代码,展示了如何创建包含多个复选框和一个确定按钮的界面,并在点击确定按钮时显示所选的编程语言:
```java
/*
* 示例程序:演示 JCheckBox 的使用
* 文件:Ch14JCheckBoxSample1.java
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Ch14JCheckBoxSample1 extends JFrame implements ActionListener {
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 200;
private static final int FRAME_X_ORIGIN = 150;
private static final int FRAME_Y_ORIGIN = 250;
private JCheckBox[] checkBox;
public static void main(String[] args) {
Ch14JCheckBoxSample1 frame = new Ch14JCheckBoxSample1();
frame.setVisible(true);
}
public Ch14JCheckBoxSample1() {
Container contentPane;
JPanel checkPanel, okPanel;
JButton okButton;
String[] btnText = {"Java", "C++", "Smalltalk", "Ada"};
// 设置窗口属性
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setTitle("Program Ch14JCheckBoxSample1");
setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
contentPane = getContentPane();
contentPane.setBackground(Color.WHITE);
contentPane.setLayout(new BorderLayout());
// 创建并放置四个复选框
checkPanel = new JPanel(new GridLayout(0, 1));
checkPanel.setBorder(BorderFactory.createTitledBorder("Can Program In"));
checkBox = new JCheckBox[btnText.length];
for (int i = 0; i < checkBox.length; i++) {
checkBox[i] = new JCheckBox(btnText[i]);
checkPanel.add(checkBox[i]);
}
// 创建并放置确定按钮
okPanel = new JPanel(new FlowLayout());
okButton = new JButton("OK");
okButton.addActionListener(this);
okPanel.add(okButton);
contentPane.add(checkPanel, BorderLayout.CENTER);
contentPane.add(okPanel, BorderLayout.SOUTH);
// 注册关闭窗口操作
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent event) {
StringBuffer skill = new StringBuffer("You can program in\n");
for (int i = 0; i < checkBox.length; i++) {
if (checkBox[i].isSelected()) {
skill.append(checkBox[i].getText() + "\n ");
}
}
JOptionPane.showMessageDialog(this, skill.toString());
}
}
```
除了上述操作,JCheckBox 对象还会生成动作事件和项目事件。项目事件在复选框的选中状态发生改变时触发,可以通过实现 `ItemListener` 接口来处理这些事件。以下是一个处理项目事件的示例代码:
```java
public void itemStateChanged(ItemEvent event) {
if (event.getStateChange() == ItemEvent.SELECTED) {
System.out.println("You checked the box");
} else {
System.out.println("You unchecked the box");
}
}
```
#### 2. JRadioButton
JRadioButton 类用于表示单选按钮,与复选框不同的是,同一组单选按钮中只能选择一个。单选按钮常用于让用户从一组可能的选项中选择一个。
- **创建和使用单选按钮**:使用方法与 JCheckBox 类类似,但需要将单选按钮添加到一个按钮组(`ButtonGroup`)中。
```java
ButtonGroup languageGroup = new ButtonGroup();
JPanel radioPanel = new JPanel();
JRadioButton[] radioButton = new JRadioButton[4];
String[] btnText = {"Java", "C++", "Smalltalk", "Ada"};
for (int i = 0; i < radioButton.length; i++) {
radioButton[i] = new JRadioButton(btnText[i]);
languageGroup.add(radioButton[i]);
radioPanel.add(radioButton[i]);
}
```
以下是一个完整的示例代码,展示了如何创建包含多个单选按钮和一个确定按钮的界面,并在点击确定按钮时显示所选的编程语言:
```java
/*
* 示例程序:演示 JRadioButton 的使用
* 文件:Ch14JRadioButtonSample.java
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Ch14JRadioButtonSample extends JFrame implements ActionListener, ItemListener {
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 200;
private static final int FRAME_X_ORIGIN = 150;
private static final int FRAME_Y_ORIGIN = 250;
private JRadioButton[] radioButton;
public static void main(String[] args) {
Ch14JRadioButtonSample frame = new Ch14JRadioButtonSample();
frame.setVisible(true);
}
public Ch14JRadioButtonSample() {
Container contentPane;
JPanel radioPanel, okPanel;
ButtonGroup languageGroup;
JButton okButton;
String[] btnText = {"Java", "C++", "Smalltalk", "Ada"};
// 设置窗口属性
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setTitle("Program Ch14JRadioButton");
setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
contentPane = getContentPane();
contentPane.setBackground(Color.WHITE);
contentPane.setLayout(new BorderLayout());
// 创建并放置四个单选按钮
radioPanel = new JPanel(new GridLayout(0, 1));
radioPanel.setBorder(BorderFactory.createTitledBorder("Pick your favorite"));
languageGroup = new ButtonGroup();
radioButton = new JRadioButton[btnText.length];
for (int i = 0; i < radioButton.length; i++) {
radioButton[i] = new JRadioButton(btnText[i]);
radioButton[i].addItemListener(this);
languageGroup.add(radioButton[i]);
radioPanel.add(radioButton[i]);
}
radioButton[0].setSelected(true);
// 创建并放置确定按钮
okPanel = new JPanel(new FlowLayout());
okButton = new JButton("OK");
okButton.addActionListener(this);
okPanel.add(okButton);
contentPane.add(radioPanel, BorderLayout.CENTER);
contentPane.add(okPanel, BorderLayout.SOUTH);
// 注册关闭窗口操作
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent event) {
String favorite = null;
int i = 0;
while (favorite == null) {
if (radioButton[i].isSelected()) {
favorite = radioButton[i].getText();
}
i++;
}
JOptionPane.showMessageDialog(this, "Your favorite language is " + favorite);
}
public void itemStateChanged(ItemEvent event) {
JRadioButton source = (JRadioButton) event.getSource();
String state;
if (event.getStateChange() == ItemEvent.SELECTED) {
state = "is selected";
} else {
state = "is deselected";
}
JOptionPane.showMessageDialog(this, "JRadioButton '" + source.getText() + "' " + state);
}
}
```
#### 3. JComboBox
JComboBox 类用于创建组合框,也称为下拉列表,它允许用户从一组可能的选项中选择一个。组合框与单选按钮类似,但交互方式不同。
- **创建组合框**:可以通过传递一个字符串数组来创建组合框。
```java
String[] comboBoxItem = {"Java", "C++", "Smalltalk", "Ada"};
JComboBox comboBox = new JComboBox(comboBoxItem);
```
- **获取所选项目**:使用 `getSelectedItem` 方法获取当前选中的项目,并进行类型转换。
```java
String selection = (String) comboBox.getSelectedItem();
```
- **获取所选项目的索引**:使用 `getSelectedIndex` 方法获取所选项目的索引。
以下是一个完整的示例代码,展示了如何创建包含一个组合框和一个确定按钮的界面,并在点击确定按钮时显示所选的项目及其索引:
```java
/*
* 示例程序:演示 JComboBox 的使用
* 文件:Ch14JComboBoxSample.java
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Ch14JComboBoxSample extends JFrame implements ActionListener, ItemListener {
priv
```
0
0
复制全文
相关推荐








