2011-04-11 60 views
29

我有一個JFrame,並想從中刪除最大化按鈕。如何從JFrame中刪除最大化按鈕?

我寫下面的代碼,但是它刪除了最大化,最小化,並且從我的JFrame關閉了

JFrame frame = new JFrame(); 
frame.add(kart); 
frame.setUndecorated(true); 
frame.setVisible(true); 
frame.setSize(400, 400); 

我只想從JFrame中刪除最大化按鈕。

+1

這*可能*幫助:http://geekycoder.wordpress.com/2009/07/17/java-tips-disabling-the-maximize-button-of-jframe/ – MByD 2011-04-11 18:05:57

回答

56

使它不能調整大小:

frame.setResizable(false); 

您仍然有最小化和關閉按鈕。

+0

在我的Mac上禁用了「+」按鈕。它真的會刪除最大化按鈕嗎? – khachik 2011-04-11 18:27:29

+0

它在Linux上......在任何情況下,調整大小是O​​P試圖實現的權利? – sjr 2011-04-11 18:49:29

+0

我不是想說你發佈了錯誤的東西。只是有趣。 +1,很好的答案,無論如何。 – khachik 2011-04-11 18:52:00

8

您無法從JFrame中刪除按鈕。改爲使用JDialog。它沒有最大化按鈕。

+1

JDialog也沒有最小化按鈕,這可能是也可能不是問題。 – Boann 2013-02-03 01:47:11

3
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent;  
import javax.swing.JDialog; import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class Test extends JDialog { 
    public Test(JFrame frame, String str) { 
     super(frame, str); 
     addWindowListener(new WindowAdapter() { 
      public void windowClosing(WindowEvent evt) { 
       System.exit(0); 
      } 
     }); 
    } 

    public static void main(String[] args) { 
     try { 
      Test myFrame = new Test(new JFrame(), "Removing maximize button"); 
      JPanel panel = new JPanel(); 
      panel.setSize(100, 100); 
      myFrame.add(panel); 
      myFrame.setSize(100, 100); 
      myFrame.setVisible(true); 
     } catch (IllegalArgumentException e) { 
      System.exit(0); 
     } 
    } } 
+0

你能解釋一下它的功能嗎? – Matthieu 2017-08-17 10:22:09

1

There描述瞭如何在不最大化和最小化按鈕的情況下實現「JFrame」。 你只需要 「incapsulate」 中的JDialog一個JFrame:

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

public class RemoveMaxAndMinButton extends JDialog{ 
    public RemoveMaxAndMinButton(JFrame frame, String str){ 
    super(frame,str); 
    addWindowListener(new WindowAdapter(){ 
      public void windowClosing(WindowEvent evt){ 
     System.exit(0); 
      } 
     }); 
    } 
    public static void main(String[] args){ 
    try{ 
     RemoveMaxAndMinButton frame = new RemoveMaxAndMinButton(new JFrame(), 
      "Remove the Minimize and Maximize button from the Title Bar"); 
     JPanel panel = new JPanel(); 
     panel.setSize(200,200); 
     JLabel lbl = new JLabel("RoseIndia.Net"); 
     panel.add(lbl); 
     frame.add(panel); 
     frame.setSize(400, 400); 
     frame.setVisible(true); 
    } 
    catch(IllegalArgumentException e){ 
     System.exit(0); 
    } 
    } 

}

+0

這不是沒有最小化/最大化按鈕的'JFrame'。這只是一個普通的'JDialog'。 – Martin 2016-09-15 14:55:49

1
/** 
* Removes the buttons from the JDialog title frame. This is a work around 
* to removing the close button 
* 
* This is confirmed to work with the Metal L&F 
*/ 
public void removeAllTitleFrameButtons() { 

    /* Get the components of the dialog */ 
    Component[] comps = this.getRootPane().getComponents(); 

    /* Indicator to break from loop */ 
    boolean breakFromLoop = false; 

    /* 
    * Go through the components and find the title 
    * pane and remove the buttons. 
    */ 
    for(Component comp : comps) { 
     /* Shall we break from loop */ 
     if(breakFromLoop) break; 
     if(comp.getClass().getName().indexOf("JLayeredPane") >0) { 
      for(Component jcomp : ((JLayeredPane)comp).getComponents()) { 
       if(jcomp.getClass().getName().indexOf("Title") > 0) { 

        /* Get the XXXXTitlePane Components */ 
        Component[] titlePaneComps = ((JComponent)jcomp).getComponents(); 

        for(Component tpComp : titlePaneComps) { 
         if(tpComp instanceof JButton) { 
          ((JButton)tpComp).setVisible(false);       
         } 
        } 
        /* No need to continue processing */ 
        breakFromLoop = true; 
        break; 
       } 
      } 
     } 
    } 
} 
+0

---只是刪除「break」語句以刪除並在必要時更新「if」。我用它來刪除關閉按鈕。 – Gino 2012-04-13 12:56:27

+0

只有當框架由LAF裝飾時纔可能 – kleopatra 2012-10-26 10:27:01

3

在JFrame屬性中 - > maximumSize = minimumSize。可調整大小= false。完成!該按鈕被禁用。

+1

您是否在編輯器中? 在netbeans中不起作用... – 2017-07-23 09:14:40

-1

如果您使用的是Netbean,那麼只需在屬性中取消選擇可調整大小的選項。它只會禁用最小化/最大化按鈕。

相關問題