【发布时间】:2016-03-20 13:57:29
【问题描述】:
我正在尝试完成此程序以: 第一种:要求用户在 JOptionPane 窗口中输入手机号码 2nd:显示消息“单击确定跟踪(输入)的GPS坐标 第三:用户点击确定后,启动画面应该弹出。 第 4 步:启动画面应该完全完成,然后 JOptionPane 窗口应该显示消息“位于 GPS 坐标内的地址是:”加上我输入的任何虚假地址。
现在启动画面会在其他所有操作中运行,并且完全不正常。我希望在单击“确定”后执行启动画面,然后完成并继续处理最终的 JOptionPane 消息。任何帮助是极大的赞赏!!仅供参考-此程序旨在作为一个虚假的恶作剧。
public class SplashScreen extends JWindow {
static boolean isRegistered;
private static JProgressBar progressBar = new JProgressBar();
private static SplashScreen execute;
private static int count;
private static Timer timer1;
public SplashScreen() {
Container container = getContentPane();
container.setLayout(null);
JPanel panel = new JPanel();
panel.setBorder(new javax.swing.border.EtchedBorder());
panel.setBackground(Color.green);
panel.setBounds(10, 10, 348, 150);
panel.setLayout(null);
container.add(panel);
JLabel label = new JLabel("Tracking target GPS coordinates...");
label.setFont(new Font("Verdana", Font.BOLD, 14));
label.setBounds(15, 25, 280, 30);
panel.add(label);
progressBar.setMaximum(50);
progressBar.setBounds(55, 180, 250, 15);
container.add(progressBar);
loadProgressBar();
setSize(370, 215);
setLocationRelativeTo(null);
setVisible(true);
}
private void loadProgressBar() {
ActionListener al = new ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
count++;
progressBar.setValue(count);
System.out.println(count);
if (count == 300) {
createFrame();
execute.setVisible(false);
timer1.stop();
}
}
private void createFrame() throws HeadlessException {
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
};
timer1 = new Timer(50, al);
timer1.start();
}
public static void main(String[] args) {
execute = new SplashScreen();
String targetCell = JOptionPane.showInputDialog(null, "Enter "
+ "target cellphone number: ");
JOptionPane.showMessageDialog(null, "Click OK to "
+ "track the GPS coordinates of " + targetCell + "...");
JOptionPane.showMessageDialog(null, "The address located within "
+ "GPS coordinates is: " /** + "random fake address") **/);
}
};
【问题讨论】:
-
所以,在您真正需要之前不要创建
SplashScreen的实例。还要记住,Swing 不是线程安全的,而是单线程的。这意味着 1- 你应该只在事件调度线程的上下文中更新 UI 和 2- 你不应该用长时间运行的任务阻塞 EDT
标签: java swing splash-screen joptionpane