
本教程详细介绍了如何在Java Swing应用中,通过`JOptionPane`的选项对话框来启动一个新的`JFrame`窗体。我们将构建一个带有动态时间显示、颜色切换以及启停功能的计时器应用,并着重讲解`JOptionPane`的返回值处理、Swing定时器的使用、事件调度线程(EDT)的正确实践,以及如何构建响应式用户界面。
在Java Swing应用程序中,有时我们需要在用户做出初步选择后才显示主界面。JOptionPane提供了一种简洁的方式来呈现模态对话框,引导用户进行选择。本教程将以一个计时器应用为例,演示如何通过JOptionPane的“设置”选项来启动一个功能完备的计时器界面。
JOptionPane.showOptionDialog方法会弹出一个自定义选项的对话框,并阻塞当前线程,直到用户做出选择或关闭对话框。它的返回值是一个整数,代表用户点击了哪个按钮。
import javax.swing.JOptionPane;
public class AppLauncher {
private static final String SETTINGS = "Settings";
private static final String CLOSE = "Close";
public static void main(String[] args) {
// 弹出选项对话框
int choice = JOptionPane.showOptionDialog(null,
"Choose option",
"Option dialog",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
new String[]{SETTINGS, CLOSE},
SETTINGS);
// 根据用户选择进行操作
if (choice == JOptionPane.YES_OPTION) { // 对应 "Settings" 按钮
// 在此处启动新的JFrame
System.out.println("Settings chosen. Launching application...");
// ... 后续代码将在此处调用新窗体
} else { // 对应 "Close" 按钮或对话框关闭
System.out.println("Close chosen or dialog closed. Exiting.");
System.exit(0);
}
}
}在上述代码中,JOptionPane.YES_OPTION通常与对话框中的第一个自定义选项(此处为"Settings")关联。当用户点击"Settings"时,choice将等于JOptionPane.YES_OPTION。
立即学习“Java免费学习笔记(深入)”;
注意事项: JOptionPane可以在非事件调度线程(EDT)中安全调用,因此在main方法中直接调用是允许的。
新的窗体将是一个JFrame,它包含一个显示时间的JLabel和控制计时器的JButton。
我们将使用BorderLayout作为JFrame的默认布局管理器。JLabel将放置在BorderLayout.CENTER,而控制按钮将放置在BorderLayout.PAGE_END的JPanel中。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.Timer; // 注意:这里是javax.swing.Timer
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TimerApplication {
private static final String CLOSE = "Close";
private static final String SETTINGS = "Settings";
private JButton startButton;
private JButton stopButton;
private JFrame frame;
private JLabel theWatch;
private Timer swingTimer; // 使用javax.swing.Timer
public TimerApplication() {
// 初始化Swing Timer,每1000毫秒(1秒)触发一次
// this::updateTimer 是方法引用,等同于 new ActionListener() { @Override public void actionPerformed(ActionEvent e) { updateTimer(e); } }
swingTimer = new Timer(1000, this::updateTimer);
swingTimer.setInitialDelay(0); // 立即激活
}
private void buildAndDisplayGui() {
frame = new JFrame("Timer App");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建显示时间的标签
theWatch = new JLabel(getCurrentTime(), SwingConstants.CENTER);
theWatch.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30)); // 添加边距
theWatch.setForeground(Color.red); // 初始颜色为红色
theWatch.setToolTipText("Timer is currently stopped."); // 停止时显示提示
frame.add(theWatch, BorderLayout.CENTER);
// 创建按钮面板
frame.add(createButtons(), BorderLayout.PAGE_END);
frame.pack(); // 根据组件的首选大小调整窗口大小
frame.setLocationByPlatform(true); // 窗口位置由平台决定
frame.setVisible(true); // 显示窗口
}
private JPanel createButtons() {
JPanel panel = new JPanel();
startButton = new JButton("Start");
startButton.setMnemonic(KeyEvent.VK_A); // 设置快捷键 Alt+A
startButton.setToolTipText("Starts the timer.");
startButton.addActionListener(this::startTimer);
panel.add(startButton);
stopButton = new JButton("Stop");
stopButton.setMnemonic(KeyEvent.VK_O); // 设置快捷键 Alt+O
stopButton.setToolTipText("Stops the timer.");
stopButton.addActionListener(this::stopTimer);
stopButton.setEnabled(false); // 初始状态下停止按钮不可用
panel.add(stopButton);
return panel;
}
// 获取当前时间并格式化
private String getCurrentTime() {
return LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss", Locale.ENGLISH));
}
// 启动计时器
private void startTimer(ActionEvent event) {
theWatch.setToolTipText(null);
theWatch.setForeground(Color.black); // 启动时切换颜色
startButton.setEnabled(false);
swingTimer.start();
stopButton.setEnabled(true);
}
// 停止计时器
private void stopTimer(ActionEvent event) {
swingTimer.stop();
theWatch.setForeground(Color.red); // 停止时切换颜色
theWatch.setToolTipText("Timer is currently stopped.");
startButton.setEnabled(true);
stopButton.setEnabled(false);
}
// 更新时间显示
private void updateTimer(ActionEvent event) {
theWatch.setText(getCurrentTime());
}
public static void main(String[] args) {
int choice = JOptionPane.showOptionDialog(null,
"Choose option",
"Option dialog",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
new String[]{SETTINGS, CLOSE},
SETTINGS);
if (choice == JOptionPane.YES_OPTION) {
// 设置系统默认外观,提升用户体验
String slaf = UIManager.getSystemLookAndFeelClassName();
try {
UIManager.setLookAndFeel(slaf);
}
catch (ClassNotFoundException |
IllegalAccessException |
InstantiationException |
UnsupportedLookAndFeelException x) {
System.out.println("WARNING (ignored): Failed to set [System] look-and-feel.");
}
// 确保Swing UI操作在事件调度线程上执行
EventQueue.invokeLater(() -> new TimerApplication().buildAndDisplayGui());
}
else {
System.exit(0);
}
}
}Swing组件不是线程安全的,所有涉及UI更新的代码都必须在事件调度线程(EDT)上执行。在main方法中,我们通过JOptionPane获取用户选择后,需要启动新的JFrame。由于main方法通常不在EDT上运行,因此必须使用EventQueue.invokeLater()来确保buildAndDisplayGui()方法在EDT上执行。
// 确保Swing UI操作在事件调度线程上执行 EventQueue.invokeLater(() -> new TimerApplication().buildAndDisplayGui());
这行代码将buildAndDisplayGui()方法的调用放入EDT的队列中,当EDT空闲时会执行它,从而避免潜在的线程问题。
通过本教程,我们学习了如何:
掌握这些技术,您就可以在Java Swing应用程序中灵活地从对话框启动复杂的用户界面,并构建响应式且用户友好的应用程序。
以上就是Java Swing中从JOptionPane启动新窗体的教程的详细内容,更多请关注php中文网其它相关文章!
Windows激活工具是正版认证的激活工具,永久激活,一键解决windows许可证即将过期。可激活win7系统、win8.1系统、win10系统、win11系统。下载后先看完视频激活教程,再进行操作,100%激活成功。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号