启动画面顺序错误

Splash Screen order is wrong

我正在尝试完成此程序以: 1st:要求用户在 JOptionPane 中输入手机号码 window 2nd: Show message "click OK to track the GPS coordinates of (输入) 第三:用户单击“确定”后,应弹出初始屏幕。 第四:启动画面应该完全完成,然后 JOptionPane window 应该显示消息 "The address located within GPS coordinates is:" 加上我输入的任何假地址。

现在启动画面在其他一切期间都在运行,而且一切都乱七八糟。我希望启动画面在单击 "OK" 后执行,然后完成并继续处理最终的 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") **/); 

    }
};

你的意思都说清楚了,何乐而不为呢? :)

我想您必须先显示 2 个 JOptionPanes,将单元格编号保存在全局变量中或将其传递给 Splashscreen 实例。 在启动画面完成后,显示带有您保存的单元格号的第三个 JOPtion 窗格。

所以你应该编辑你的 actionPefromed 方法,类似于:

public void actionPerformed(java.awt.event.ActionEvent evt) {
                count++;

                progressBar.setValue(count);

                System.out.println(count);

                if (count == 300) {
                    execute.setVisible(false);
                    timer1.stop();
                      //splash screen finished so show JoptionPane here
                    JOptionPane.showMessageDialog(null, "The address located within "
            + "GPS coordinates is: " + targetCell)


                }

            }

做事的顺序非常重要。如果您查看您的代码,SplashScreen 在创建时使 window 可见,这是一个坏主意,您突然发现了这一点。

首先,您需要改变做事的顺序,例如...

String targetCell = JOptionPane.showInputDialog(null, "Enter "
        + "target cellphone number: ");
JOptionPane.showMessageDialog(null, "Click OK to "
        + "track the GPS coordinates of " + targetCell + "...");
// Show and wait for splash screen to complete
JOptionPane.showMessageDialog(null, "The address located within "
        + "GPS coordinates is: " /**
 * + "random fake address") *
 */
);

您还需要以某种方式让您的初始屏幕提供有关何时完成其任务的事件通知,因为 JWindow 正在 none 阻塞。

一个更简单的解决方案是在模态 JDialog 中显示启动画面,这会在启动画面可见时阻止代码的执行,让您 "wait"直到完成。

作为一般经验法则,您应该避免从像 JWindow 这样的顶级容器扩展,而更喜欢像 JPanel 这样的东西,这使您可以灵活地在任何容器中显示组件你要。

启动下一个 window 启动画面也不是责任,它唯一的责任是显示 activity 的进度(可能 return 结果这是计算)

例如...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                String targetCell = JOptionPane.showInputDialog(null, "Enter "
                        + "target cellphone number: ");
                JOptionPane.showMessageDialog(null, "Click OK to "
                        + "track the GPS coordinates of " + targetCell + "...");
                SplashPane.showSplashScreen();
                JOptionPane.showMessageDialog(null, "The address located within "
                        + "GPS coordinates is: " /**
                 * + "random fake address") *
                 */
                );
            }
        });
    }

    public static class SplashPane extends JPanel {

        private JProgressBar progressBar = new JProgressBar();
        private Timer timer1;

        public SplashPane() {
            setLayout(new BorderLayout(0, 4));
            setBorder(new EmptyBorder(10, 10, 10, 10));

            JPanel panel = new JPanel(new GridBagLayout());
            panel.setBorder(new CompoundBorder(
                    new javax.swing.border.EtchedBorder(),
                    new EmptyBorder(30, 20, 30, 20)));
            panel.setBackground(Color.green);
            JLabel label = new JLabel("Tracking target GPS coordinates...");
            label.setFont(new Font("Verdana", Font.BOLD, 14));
            panel.add(label);
            add(panel);

            add(progressBar, BorderLayout.SOUTH);

            loadProgressBar();
        }

        private void loadProgressBar() {
            ActionListener al = new ActionListener() {
                private int count;

                @Override
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    count = Math.min(100, ++count);
                    progressBar.setValue(count);

                    System.out.println(count);
                    if (count == 100) {
                        SwingUtilities.windowForComponent(SplashPane.this).dispose();
                        timer1.stop();
                    }
                }
            };
            timer1 = new Timer(150, al);
            timer1.start();
        }

        public static void showSplashScreen() {
            JDialog frame = new JDialog();
            frame.setModal(true);
            frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
            frame.setUndecorated(true);
            frame.add(new SplashPane());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }

    }
}

避免使用 null 布局,像素完美布局是现代 ui 设计中的一种错觉。影响组件个体大小的因素太多,none 是您可以控制的。 Swing 旨在与核心的布局管理器一起工作,丢弃这些将导致无穷无尽的问题和问题,您将花费越来越多的时间来尝试纠正