
本文详细介绍了如何在java swing应用中,通过`joptionpane`的选项对话框来控制新`jframe`表单的显示。教程涵盖了如何构建一个具有实时更新时钟、启动/停止功能以及动态颜色变化的独立窗口,并深入探讨了`javax.swing.timer`的使用、事件调度线程(edt)的最佳实践以及布局管理器的应用。
在Java Swing应用程序开发中,我们经常需要从一个简单的用户交互点(例如一个对话框)启动一个更复杂的UI界面。本教程将指导您如何利用JOptionPane来呈现一个初始选择对话框,并根据用户的选择打开一个新的JFrame窗口,该窗口将显示一个实时更新的时钟,并提供控制时钟启动、停止及显示颜色切换的功能。
JOptionPane提供了一种简单的方式来弹出标准对话框。在本例中,我们将使用showOptionDialog方法,它允许我们自定义按钮文本,并根据用户点击的按钮返回一个整数值。
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.EventQueue; // 导入 EventQueue
public class StopWhat { // 主类名,避免与 javax.swing.Timer 冲突
private static final String SETTINGS = "Settings";
private static final String CLOSE = "Close";
// ... (其他成员变量和方法将在后续部分定义)
public static void main(String[] args) {
// 显示选项对话框
int choice = JOptionPane.showOptionDialog(
null, // 父组件,null 表示桌面中央
"Choose option", // 提示信息
"Option dialog", // 对话框标题
JOptionPane.YES_NO_OPTION, // 选项类型,这里使用 YES_NO_OPTION 来匹配两个自定义按钮
JOptionPane.QUESTION_MESSAGE, // 消息类型图标
null, // 自定义图标,null 表示使用默认图标
new String[]{SETTINGS, CLOSE}, // 自定义按钮文本
SETTINGS // 默认选中按钮
);
// 根据用户选择执行相应操作
if (choice == JOptionPane.YES_OPTION) { // 如果用户选择了 "Settings"
// 尝试设置系统外观,提升用户体验
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | IllegalAccessException |
InstantiationException | UnsupportedLookAndFeelException x) {
System.out.println("WARNING (ignored): Failed to set [System] look-and-feel.");
}
// 在事件调度线程中构建并显示GUI
EventQueue.invokeLater(() -> new StopWhat().buildAndDisplayGui());
} else { // 如果用户选择了 "Close" 或关闭了对话框
System.exit(0); // 退出应用程序
}
}
}关键点解析:
接下来,我们将实现StopWhat类中用于构建和管理新JFrame表单的逻辑。这个表单将包含一个显示时间的JLabel和用于控制时钟的按钮。
立即学习“Java免费学习笔记(深入)”;
import java.awt.BorderLayout;
import java.awt.Color;
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.JPanel;
import javax.swing.SwingConstants;
import javax.swing.Timer; // 明确指定使用 Swing Timer
// 承接上文的 StopWhat 类定义
public class StopWhat {
// ... (SETTINGS, CLOSE 常量和 main 方法)
private JButton startButton;
private JButton stopButton;
private JFrame frame;
private JLabel theWatch;
private Timer timer; // 使用 javax.swing.Timer
public StopWhat() {
// 初始化 Swing Timer,每1000毫秒(1秒)触发一次 ActionEvent
// this::updateTimer 是方法引用,表示 Timer 触发时调用 updateTimer 方法
timer = new Timer(1000, this::updateTimer);
timer.setInitialDelay(0); // 立即触发第一次事件,无需等待初始延迟
}
/**
* 构建并显示主GUI界面。
*/
private void buildAndDisplayGui() {
frame = new JFrame("Timer App");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 关闭窗口时退出程序
// 创建显示时间的 JLabel
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); // 使窗口可见
}
/**
* 创建包含启动和停止按钮的面板。
* @return 包含按钮的 JPanel
*/
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;
}
/**
* 获取当前时间并格式化为 HH:mm:ss。
* @return 格式化后的当前时间字符串
*/
private String getCurrentTime() {
return LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss", Locale.ENGLISH));
}
/**
* 启动计时器。
* @param event ActionEvent
*/
private void startTimer(ActionEvent event) {
theWatch.setToolTipText(null); // 清除提示
theWatch.setForeground(Color.black); // 将标签颜色设为黑色
startButton.setEnabled(false); // 启动按钮禁用
timer.start(); // 启动 Swing Timer
stopButton.setEnabled(true); // 停止按钮启用
}
/**
* 停止计时器。
* @param event ActionEvent
*/
private void stopTimer(ActionEvent event) {
timer.stop(); // 停止 Swing Timer
theWatch.setForeground(Color.red); // 将标签颜色设为红色
theWatch.setToolTipText("Timer is currently stopped."); // 设置停止提示
startButton.setEnabled(true); // 启动按钮启用
stopButton.setEnabled(false); // 停止按钮禁用
}
/**
* 更新时间显示。
* @param event ActionEvent
*/
private void updateTimer(ActionEvent event) {
theWatch.setText(getCurrentTime()); // 更新 JLabel 的文本为当前时间
}
}javax.swing.Timer是专门为Swing应用程序设计的计时器。它在事件调度线程(EDT)上触发其ActionListener,这使得在ActionListener中直接更新UI组件变得安全且高效。
Java 8引入的java.time包提供了现代化的日期和时间API,比旧的java.util.Date和java.util.Calendar更加直观和易用。
JFrame的默认布局管理器是BorderLayout。它将容器分为五个区域:NORTH(北)、SOUTH(南)、EAST(东)、WEST(西)和CENTER(中)。
在代码中,我们使用了Java 8的方法引用(this::updateTimer、this::startTimer、this::stopTimer)来简化ActionListener的实现。这比匿名内部类或lambda表达式更简洁,尤其适用于只有一个抽象方法的接口。
通过本教程,您应该已经掌握了如何在Java Swing中利用JOptionPane作为入口点,启动并管理一个功能丰富的JFrame表单,并实现实时更新、交互控制等功能。这些技术是构建任何复杂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号