2013-09-23 48 views
1
public static void main(String args[]){ 
    JFrame frame = new JFrame(); 
    frame.setExtendedState(JFrame.MAXIMISED_BOTH); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setVisible(true); 
} 

我用這個代碼,以最大限度地提高一個JFrame,但不是真正最大化框架,它只是設置窗口大小的屏幕,而無需實際更改狀態,所以點擊最大化按鈕並不會再次縮減比例。JFrame.setExtendedState實際上並沒有最大化

我使用了錯誤的命令嗎?

+0

這可能是一個平臺的問題,您使用的是什麼操作系統寫frame.setExtendedState(JFrame.MAXIMIZED_BOTH);?還可能有一個'setSize'調用干擾進程 – MadProgrammer

+0

我使用Windows 7 –

+0

它適用於我。爲了更快地獲得更好的幫助,請發佈證明問題的[SSCCE](http://sscce.org)。很多時候,只要創建'SSCCE'就會發現問題。 – splungebob

回答

0

默認情況下,您必須將其最大化。因爲最大化按鈕可以立即使用。

frame.setExtendedState(JFrame.MAXIMIZED_BOTH)適用於Linux x64。這裏是我測試的程序:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class Test implements ActionListener { 

    public static void main(String... args) { 
    new Test(); 
    } 

    private JFrame frame; 

    public Test() { 
    frame = new JFrame(); 
    frame.add(new JLabel("Hi!"), BorderLayout.CENTER); 
    JButton button = new JButton("maximize"); 
    button.addActionListener(this); 
    frame.add(button, BorderLayout.SOUTH); 
    frame.pack(); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setVisible(true); 
    } 


    @Override 
    public void actionPerformed(ActionEvent e) { 
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
    } 
} 
+0

這似乎什麼都不做。 –

+0

我希望這是你的環境中的東西。我編輯了答案,添加了我測試過的例子。 – WeaponsGrade

0

你試過嗎?

f.setExtendedState(f.getExtendedState() | JFrame.MAXIMIZED_BOTH); 
+0

這產生了與我在問題中描述的結果相同的結果,管道代表btw是什麼? –

+0

它會對左側和右側組件進行按位或運算。所以,基本上你是「添加」MAXIMIZED_BOTH而不是覆蓋所有其他狀態。 –

-1

它適用於我在WinXP機器上運行Java 7。

根據記錄,這是一個SSCCE應該是什麼樣子:根據您提供的示例

import javax.swing.*; 

public class JFrameExtendedDemo 
{ 
    public static void main(String[] args) 
    { 
    SwingUtilities.invokeLater(new Runnable() 
    { 
     public void run() 
     { 
     JFrame f = new JFrame(); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.setSize(300, 200); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 

     // Then: 
     f.setExtendedState(JFrame.MAXIMIZED_BOTH); 
     } 
    }); 
    } 
} 
+0

只是一個簡單的問題:爲什麼我應該使用invokeLater作爲從main運行它的姿勢? –

+0

教程的開始位置在這裏:[Swing中的併發](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html) – splungebob

+0

它確實使窗口成爲全屏,它只是沒有實際上將狀態設置爲最大化,所以你不能再小心翼翼地再小一點 –

0

和在Windows 7上運行...

「最大化」的狀態(這是裁剪版本窗口原來是相當大)的

enter image description here

「正常」 狀態

enter image description here

import java.awt.EventQueue; 
import javax.swing.JFrame; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class ExtendedFrame { 

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

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

       JFrame frame = new JFrame(); 
//    frame.setExtendedState(JFrame.MAXIMISED_BOTH); 
       frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setVisible(true); 
      } 
     }); 
    } 
} 
+0

經過一些測試後,設置擴展狀態會立即將其設置爲最大化狀態,但當我在稍後的時間點調用它時,它只調整它。 –

0

這爲我工作:

我們需要的的setSize()和setExtendedState一起 的JFrame幀=新的JFrame()相結合;

frame.setExtendedState(JFrame.MAXIMIZED_BOTH); // aligns itself with windows task bar 
// set maximum screen 
frame.setSize((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(), (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()); 
1

你有frame.setExtendedState(JFrame.MAXIMISED_BOTH);

錯誤而應該

相關問題