启动画面顺序错误

时间:2015-12-14 22:44:59

标签: java swing joptionpane

我正在尝试完成此计划: 1st:请求用户在JOptionPane窗口中输入手机号码 第二步:显示消息"单击确定以跟踪(输入)的GPS坐标 第三:用户单击“确定”后,应弹出启动画面。 第4步:启动画面应完全完成,之后JOptionPane窗口应显示消息"位于GPS坐标内的地址为:"加上我输入的任何虚假地址。

现在,启动画面在其他所有内容中运行,并且全部无序。我希望启动画面在" 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 个答案:

答案 0 :(得分:2)

你做事的顺序非常重要。如果您查看代码,SplashScreen会在创建窗口时显示窗口,这是一个糟糕的主意并且您突然发现了。

首先,您需要更改执行操作的顺序,例如......

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没有阻止。

更简单的解决方案是在模式JDialog内显示启动画面,这会阻止在启动画面可见时执行代码,让您等待"等待&#34 ;直到它完成。

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

启动下一个窗口也不是启动画面的责任,它唯一的责任是显示它的活动进度(并可能返回它的结果&# 39;计算)

例如......

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设计中的一种幻觉。影响组件个体大小的因素太多,您无法控制。 Swing旨在与布局管理器一起工作,放弃这些将导致问题和问题的终结,您将花费越来越多的时间来纠正

答案 1 :(得分:0)

你已经清楚地说出了你想要的东西,为什么不这样做呢? :)

我猜你必须先显示2个JOptionPanes,将单元格数保存在全局变量中,或者将其传递给Splashscreen实例。 在您的启动画面完成后,显示第3个JOPtion窗格,其中包含您保存的cellNumber。

所以你应该编辑你的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)


                }

            }