2012-12-23 46 views
1

當我點擊按鈕時,設置爲全屏的應用程序將進入任務欄/最小化,因此我需要在任務欄中首先單擊它,然後才能看到我觸發的JOptionPane。你認爲這是什麼問題?我希望它能夠順利運行而不會被最小化或轉到任務欄。這裏GrahicsDevice和JOptionPane問題

public static void main(String[] args) { 
     final JFrame frame = new JFrame(); 
     frame.setTitle("Sample"); 
     GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); 
     device.setFullScreenWindow(frame); 
     device.setDisplayMode(new DisplayMode(800, 600, 32, 60)); 
     frame.setVisible(true); 

     JButton btn = new JButton(); 
     btn.setText("Btn"); 
     JPanel panel = new JPanel(); 

     panel.add(btn); 
     frame.add(panel); 

     btn.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       JOptionPane.showMessageDialog(frame, "Sample"); 
       throw new UnsupportedOperationException("Not supported yet."); 
      } 

     }); 
    } 
+0

我該如何接受答案? – Jong

+2

閱讀此[鏈接](http://meta.stackexchange.com/a/65088/155831)。您可以重新訪問以前的問題並接受。 – Reimeus

+0

你嘗試過'JDialog'而不是'JOptionPane'嗎? –

回答

1

一種解決方法是在意見所使用的

frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 

代替

device.setFullScreenWindow(frame); 

此外,在已添加的所有成分setVisible(true)應該會出現。

+0

該應用程序具有800x600的固定大小。它不能延長國家。不管怎麼說,還是要謝謝你。 – Jong

1

我能想到的唯一工作是將WindowAdpater添加到JFrame,這將覆蓋windowIconified(..)。還有一個boolean用作程序的標誌,以便知道何時由於顯示JOptionPane而使窗口被圖標化。

它確實很黑,但只有在幾次屏幕閃爍後,我們纔看到JOptionPaneJFrame很好地協同工作。

下面是代碼:

import java.awt.DisplayMode; 
import java.awt.GraphicsDevice; 
import java.awt.GraphicsEnvironment; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class Test { 

    private static boolean programmatic = false; 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       final JFrame frame = new JFrame(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setTitle("Sample"); 
       GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); 
       device.setFullScreenWindow(frame); 
       device.setDisplayMode(new DisplayMode(800, 600, 32, 60)); 

       frame.addWindowListener(new WindowAdapter() { 
        @Override 
        public void windowIconified(WindowEvent we) { 
         //super.windowIconified(we); 
         if (programmatic) { 
          programmatic = false; 
          frame.setState(JFrame.NORMAL); 
         } 
        } 
       }); 

       JButton btn = new JButton(); 
       btn.setText("Btn"); 
       final JPanel panel = new JPanel(); 

       panel.add(btn); 
       frame.add(panel); 

       btn.addActionListener(new ActionListener() { 
        @Override 
        public void actionPerformed(ActionEvent e) { 
         programmatic = true; 
         JOptionPane.showMessageDialog(panel, "Sample"); 
        } 
       }); 
       frame.setVisible(true); 
      } 
     }); 
    } 
} 

和思考有關它的更多,JDialog也再現了結果,我認爲它是由於JOptionPanemodality S和JDialog秒。也許使用JDialog並設置模式會做的伎倆。

+0

不錯,但我正在全屏幕上工作。我希望它能夠平穩運行而不會閃爍。謝謝。 – Jong

+0

@jong不幸的是,我不認爲它可以做.....我可能會誤解,但我已經嘗試了整個晚上無濟於事,其中包括使用谷歌搜索許多人有同樣的問題,但沒有解決方案....如果有什麼嘗試使用jpanel並根據需要設計樣式 –

+0

謝謝David Kroukamp。有沒有辦法提升你的聲譽? – Jong