【转】Swing中弹出对话框的几种方式_JOptionPane.showMessageDialog等详解

本文深入探讨了 Java Swing 中的 JOptionPane 类如何用于创建消息对话框、确认对话框、输入对话框和选项对话框。通过展示具体实例,详细解释了各个方法的参数及其作用,旨在提供一种直观且实用的对话框使用指南。

在swing中,基于业务的考量,会有对话框来限制用户的行为及对用户的动作进行提示.

Swing中提供了JOptionPane类来实现类似Windows平台下的MessageBox的功能,同样在Java中也有,利用JOptionPane类中的各个static方法来生成各种标准的对话框,实现显示出信息、提出问题、警告、用户输入参数等功能。这些对话框都是模式对话框。
ConfirmDialog --- 确认对话框,提出问题,然后由用户自己来确认(按"Yes"或"No"按钮)
InputDialog --- 提示输入文本
MessageDialog --- 显示信息
OptionDialog -- 组合其它三个对话框类型。
  这四个对话框可以采用showXXXDialog()来显示,如showConfirmDialog()显示确认对话框、showInputDialog()显示输入文本对话框、showMessageDialog()显示信息对话框、showOptionDialog()显示选择性的对话框。它们所使用的参数说明如下:
① ParentComponent:指示对话框的父窗口对象,一般为当前窗口。也可以为null即采用缺省的Frame作为父窗口,此时对话框将设置在屏幕的正中。
② message:指示要在对话框内显示的描述性的文字
③ String title:标题条文字串。
④ Component:在对话框内要显示的组件(如按钮)
⑤ Icon:在对话框内要显示的图标
⑥ messageType:一般可以为如下的值ERROR_MESSAGE、INFORMATION_MESSAGE、WARNING_MESSAGE、QUESTION_MESSAGE、PLAIN_MESSAGE、
⑦ optionType:它决定在对话框的底部所要显示的按钮选项。一般可以为DEFAULT_OPTION、YES_NO_OPTION、YES_NO_CANCEL_OPTION、OK_CANCEL_OPTION。
使用实例:
(1)显示MessageDialog
JOptionPane.showMessageDialog(null, "在对话框内显示的描述性的文字", "标题条文字串", JOptionPane.ERROR_MESSAGE);
(2)显示ConfirmDialog
JOptionPane.showConfirmDialog(null, "choose one", "choose one", JOptionPane.YES_NO_OPTION);
(3)显示OptionDialog:该种对话框可以由用户自己来设置各个按钮的个数并返回用户点击各个按钮的序号(从0开始计数)
Object[] options = {"确定","取消","帮助"};
int response=JOptionPane.showOptionDialog(this, "这是个选项对话框,用户可以选择自己的按钮的个数", "选项对话框标题",JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if(response==0)
{ this.setTitle("您按下了第OK按钮 ");
}
else if(response==1)
{ this.setTitle("您按下了第Cancel按钮 ");
}
else if(response==2)
{ this.setTitle("您按下了第Help按钮 ");
}
(4)显示InputDialog 以便让用户进行输入
String inputValue = JOptionPane.showInputDialog("Please input a value");
(5)显示InputDialog 以便让用户进行选择地输入
Object[] possibleValues = { "First", "Second", "Third" }; //用户的选择项目
Object selectedValue = JOptionPane.showInputDialog(null, "Choose one", "Input",JOptionPane.INFORMATION_MESSAGE, null, possibleValues, possibleValues[0]);
setTitle("您按下了"+(String)selectedValue+"项目");

 转自:http://blog.youkuaiyun.com/zhaobisha/article/details/2714881

修改密码界面 import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class ChangePasswordFrame extends JFrame { private ATMSystem atmSystem; private JPasswordField oldPasswordField; private JPasswordField newPasswordField; private JPasswordField confirmPasswordField; public ChangePasswordFrame(ATMSystem atmSystem) { this.atmSystem = atmSystem; initializeUI(); } private void initializeUI() { setTitle("修改密码"); setSize(400, 350); setLocationRelativeTo(null); setResizable(false); JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(10, 10, 10, 10); gbc.anchor = GridBagConstraints.WEST; // 旧密码 gbc.gridx = 0; gbc.gridy = 0; panel.add(new JLabel("旧密码:"), gbc); gbc.gridx = 1; oldPasswordField = new JPasswordField(15); panel.add(oldPasswordField, gbc); // 新密码 gbc.gridx = 0; gbc.gridy = 1; panel.add(new JLabel("新密码(至少6位):"), gbc); gbc.gridx = 1; newPasswordField = new JPasswordField(15); panel.add(newPasswordField, gbc); // 确认新密码 gbc.gridx = 0; gbc.gridy = 2; panel.add(new JLabel("确认新密码:"), gbc); gbc.gridx = 1; confirmPasswordField = new JPasswordField(15); panel.add(confirmPasswordField, gbc); // 按钮面板 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10)); JButton confirmButton = new JButton("确认修改"); confirmButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String oldPassword = new String(oldPasswordField.getPassword()); String newPassword = new String(newPasswordField.getPassword()); String confirmPassword = new String(confirmPasswordField.getPassword()); try { if (atmSystem.changePassword(oldPassword, newPassword, confirmPassword)) { JOptionPane.showMessageDialog(ChangePasswordFrame.this, "密码修改成功!"); dispose(); } else { JOptionPane.showMessageDialog(ChangePasswordFrame.this, "密码修改失败!请检查:\n1. 旧密码是否正确\n2. 新密码是否符合要求\n3. 两次输入的新密码是否一致", "错误", JOptionPane.ERROR_MESSAGE); } } catch (IllegalStateException ex) { JOptionPane.showMessageDialog(ChangePasswordFrame.this, ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE); dispose(); } } }); buttonPanel.add(confirmButton); JButton cancelButton = new JButton("取消"); cancelButton.addActionListener(e -> dispose()); buttonPanel.add(cancelButton); add(panel, BorderLayout.CENTER); add(buttonPanel, BorderLayout.SOUTH); } } 相关技术介绍
06-15
package demo1; import javax.swing.*; import java.awt.event.ActionEvent; public class TemperatureController { private TemperatureMonitorGUI gui; private SerialPortReader reader; private double currentTemp = 25.0; // 初始温度 private boolean heating = false; private Timer timer; public TemperatureController(TemperatureMonitorGUI gui, String portName, int baudRate) { this.gui = gui; this.reader = new SerialPortReader(portName, baudRate, temp -> { currentTemp = temp; gui.updateTemperature(currentTemp); checkTemperature(); }); gui.addStartButtonActionListener(this::startHeating); } public void start() { if (!reader.open()) { gui.setStatus("Error opening serial port."); } else { gui.updateTemperature(currentTemp); // 初始化显示 } } public void stop() { reader.close(); if (timer != null) timer.stop(); } private void startHeating(ActionEvent e) { double targetTemp = gui.getTargetTemperature(); int timeLimit = gui.getTimeLimitInSeconds(); if (Double.isNaN(targetTemp) || timeLimit <= 0) { JOptionPane.showMessageDialog(gui, "请输入有效的目标温度和时间!"); return; } heating = true; gui.setStatus("Heating in progress..."); final double finalTargetTemp = targetTemp; timer = new Timer(1000, evt -> { if (heating && currentTemp < finalTargetTemp - 0.5) { currentTemp += 0.5; // 模拟加热 } else if (heating && currentTemp >= finalTargetTemp - 1.0) { currentTemp += 0.1; // 小功率加热 } gui.updateTemperature(currentTemp); if (currentTemp >= finalTargetTemp) { ((Timer) evt.getSource()).stop(); heating = false; gui.setStatus("Target reached! Keeping warm..."); JOptionPane.showMessageDialog(gui, "目标温度已达到!正在保温"); } }); timer.start(); } private void checkTemperature() { if (heating && currentTemp >= gui.getTargetTemperature()) { timer.stop(); heating = false; gui.setStatus("Target reached! Keeping warm..."); JOptionPane.showMessageDialog(gui, "目标温度已达到!正在保温"); } } }
最新发布
07-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值