2015-12-14 21 views
0

我想這個計劃完成到: 1:要求用戶輸入的JOptionPane的窗口 第二屆一個手機號碼:顯示消息「,單擊OK跟蹤(輸入) 3的GPS座標:只有在用戶點擊OK啓動畫面應該彈出 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") **/); 

    } 
}; 
+0

因此,請勿創建「SplashScreen」實例直到你真的需要它。另外請記住,Swing不是線程安全的並且是單線程的。這意味着1-您只應從事件分派線程的上下文中更新UI,並且2-您不應該用長時間運行的任務阻止EDT – MadProgrammer

回答

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內顯示啓動畫面,這將阻止在啓動畫面變得可見的位置執行代碼,允許您「等待」,直到完成。

根據經驗,一般情況下,你應該避免從頂級容器,如JWindow延伸,而是喜歡像JPanel,這爲您提供了靈活性,以顯示你想要的任何容器組件。

這也是閃屏啓動下一窗口的不負責,這是唯一的責任就是顯示進度它的活動(也可能返回它的計算結果)

例如...

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旨在與佈局經理一起工作,放棄這些將導致問題和問題無法結束,您將花費越來越多的時間來嘗試糾正

+0

非常感謝!它效果很好 – Peanutcalota

0

你已經清楚地說出你想要什麼,那爲什麼不直接做呢? :)

我想你將不得不首先顯示2個JOptionPanes,將單元格編號保存在全局變量中或將其傳遞給Splashscreen實例。 閃屏完成後,用保存的cellNumber顯示第三個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) 


       } 

      }