Swing对话框的使用与创建
发布时间: 2025-08-18 00:07:27 阅读量: 3 订阅数: 11 

### Swing 对话框的使用与创建
#### 1. Swing 中的预定义对话框
Swing 提供了一组现成的简单对话框,可用于向用户获取单一信息。`JOptionPane` 类有四个静态方法来显示这些简单对话框:
- `showMessageDialog`:显示一条消息并等待用户点击“确定”。
- `showConfirmDialog`:显示一条消息并获取用户的确认(如“确定/取消”)。
- `showOptionDialog`:显示一条消息并从一组选项中获取用户的选择。
- `showInputDialog`:显示一条消息并获取用户的单行输入。
典型的对话框包含以下组件:
- 图标
- 消息
- 一个或多个选项按钮
输入对话框还会有一个用于用户输入的组件,可能是一个文本框,用户可以在其中输入任意字符串,也可能是一个组合框,用户可以从中选择一个项目。
对话框的具体布局和标准消息类型的图标选择取决于可插拔的外观风格。对话框左侧的图标取决于以下五种消息类型之一:
- `ERROR_MESSAGE`
- `INFORMATION_MESSAGE`
- `WARNING_MESSAGE`
- `QUESTION_MESSAGE`
- `PLAIN_MESSAGE`
其中,`PLAIN_MESSAGE` 类型没有图标。每种对话框类型也都有一个方法允许你提供自己的图标。
对于每种对话框类型,你可以指定一条消息。这条消息可以是字符串、图标、用户界面组件或任何其他对象。消息对象的显示方式如下:
| 消息对象类型 | 显示方式 |
| ---- | ---- |
| String | 绘制字符串 |
| Icon | 显示图标 |
| Component | 显示组件 |
| Object[] | 显示数组中的所有对象,一个叠在另一个上面 |
| 其他对象 | 应用 `toString` 方法并显示结果字符串 |
对话框底部的按钮取决于对话框类型和选项类型。调用 `showMessageDialog` 和 `showInputDialog` 时,你将分别获得一组标准按钮(“确定”和“确定/取消”)。调用 `showConfirmDialog` 时,你可以在四种选项类型中选择:
- `DEFAULT_OPTION`
- `YES_NO_OPTION`
- `YES_NO_CANCEL_OPTION`
- `OK_CANCEL_OPTION`
使用 `showOptionDialog` 时,你可以指定任意一组选项。你需要为选项提供一个对象数组,每个数组元素的渲染方式如下:
| 数组元素类型 | 渲染方式 |
| ---- | ---- |
| String | 制作一个以该字符串为标签的按钮 |
| Icon | 制作一个以该图标为标签的按钮 |
| Component | 显示组件 |
| 其他对象 | 应用 `toString` 方法并制作一个以结果字符串为标签的按钮 |
这些函数的返回值如下:
| 方法 | 返回值 |
| ---- | ---- |
| `showMessageDialog` | 无 |
| `showConfirmDialog` | 一个表示所选选项的整数 |
| `showOptionDialog` | 一个表示所选选项的整数 |
| `showInputDialog` | 用户提供或选择的字符串 |
`showConfirmDialog` 和 `showOptionDialog` 返回整数以指示用户选择了哪个按钮。对于选项对话框,如果用户选择了某个选项,返回值是所选选项的索引;如果用户关闭了对话框而没有选择选项,则返回 `CLOSED_OPTION`。对于确认对话框,返回值可以是以下之一:
- `OK_OPTION`
- `CANCEL_OPTION`
- `YES_OPTION`
- `NO_OPTION`
- `CLOSED_OPTION`
使用这些对话框的步骤如下:
1. 选择对话框类型(消息、确认、选项或输入)。
2. 选择图标(错误、信息、警告、问题、无或自定义)。
3. 选择消息(字符串、图标、自定义组件或它们的组合)。
4. 对于确认对话框,选择选项类型(默认、是/否、是/否/取消或确定/取消)。
5. 对于选项对话框,选择选项(字符串、图标或自定义组件)和默认选项。
6. 对于输入对话框,选择是使用文本框还是组合框。
7. 在 `JOptionPane` API 中找到合适的方法调用。
例如,假设你要显示一个显示消息并要求用户确认或取消的对话框,这是一个确认对话框,图标为问题图标,消息为字符串,选项类型为 `OK_CANCEL_OPTION`。调用代码如下:
```java
int selection = JOptionPane.showConfirmDialog(parent,
"Message", "Title",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (selection == JOptionPane.OK_OPTION) ...
```
#### 2. 示例程序
下面是一个示例程序,展示了如何使用 `JOptionPane` 类的对话框。
##### 2.1 OptionDialogFrame.java
```java
package optionDialog;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
/**
* A frame that contains settings for selecting various option dialogs.
*/
public class OptionDialogFrame extends JFrame
{
private ButtonPanel typePanel;
private ButtonPanel messagePanel;
private ButtonPanel messageTypePanel;
private ButtonPanel optionTypePanel;
private ButtonPanel optionsPanel;
private ButtonPanel inputPanel;
private String messageString = "Message";
private Icon messageIcon = new ImageIcon("blue-ball.gif");
private Object messageObject = new Date();
private Component messageComponent = new SampleComponent();
public OptionDialogFrame()
{
JPanel gridPanel = new JPanel();
gridPanel.setLayout(new GridLayout(2, 3));
typePanel = new ButtonPanel("Type", "Message", "Confirm", "Option", "Input");
messageTypePanel = new ButtonPanel("Message Type", "ERROR_MESSAGE", "INFORMATION_MESSAGE",
"WARNING_MESSAGE", "QUESTION_MESSAGE", "PLAIN_MESSAGE");
messagePanel = new ButtonPanel("Message", "String", "Icon", "Component", "Other",
"Object[]");
optionTypePanel = new ButtonPanel("Confirm", "DEFAULT_OPTION", "YES_NO_OPTION",
"YES_NO_CANCEL_OPTION", "OK_CANCEL_OPTION");
optionsPanel = new ButtonPanel("Option", "String[]", "Icon[]", "Object[]");
inputPanel = new ButtonPanel("Input", "Text field", "Combo box");
gridPanel.add(typePanel);
gridPanel.add(messageTypePanel);
gridPanel.add(messagePanel);
gridPanel.add(optionTypePanel);
gridPanel.add(optionsPanel);
gridPanel.add(inputPanel);
// add a panel with a Show button
JPanel showPanel = new JPanel();
JButton showButton = new JButton("Show");
showButton.addActionListener(new ShowAction());
showPanel.add(showButton);
add(gridPanel, BorderLayout.CENTER);
add(showPanel, BorderLayout.SOUTH);
pack();
}
/**
* Gets the currently selected message.
* @return a string, icon, component, or object array, depending on the Message panel selection
*/
public Object getMessage()
{
String s = messagePanel.getSelection();
if (s.equals("String")) return messageString;
else if (s.equals("Icon")) return messageIcon;
else if (s.equals("Component")) return messageComponent;
else if (s.equals("Object[]")) return new Object[] { messageString, messageIcon,
messageComponent, messageObject };
else if (s.equals("Other")) return messageObject;
else return null;
}
/**
* Gets the currently selected options.
* @return an array of strings, icons, or objects, depending on the Option panel selection
*/
public Object[] getOptions()
{
String s = optionsPanel.getSelection();
if (s.equals("String[]")) return new String[] { "Yellow", "Blue", "Red" };
else if (s.equals("Icon[]")) return new Icon[] { new ImageIcon("yellow-ball.gif"),
new ImageIcon("blue-ball.gif"), new ImageIcon("red-ball.gif") };
else if (s.equals("Object[]")) return new Object[] { messageString, messageIcon,
messageComponent, messageObject };
else return null;
}
/**
* Gets the selected message or option type
* @param panel the Message Type or Confirm panel
* @return the selected XXX_MESSAGE or XXX_OPTION constant from the JOptionPane class
*/
public int getType(ButtonPanel panel)
{
String s = panel.getSelection();
try
{
return JOptionPane.class.getField(s).getInt(null);
}
catch (Exception e)
{
return -1;
}
}
/**
* The action listener for the Show button shows a Confirm, Input, Message, or Option dialog
* depending on the Type panel selection.
*/
pr
```
0
0
相关推荐










