2011-04-11 3 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

답변

56

그것은 크기 조정하지 확인 :

frame.setResizable(false); 

당신은 여전히 ​​최소화 및 닫기 버튼을해야합니다.

+0

내 Mac에서 '+'버튼을 사용할 수 없습니다. 그것은 최대화 버튼을 정말로 제거합니까? – khachik

+0

리눅스에서 그렇습니다 ... 어떤 경우에도 OP가 올바르게 달성하려고하는 크기는 없습니다. – sjr

+0

나는 당신이 틀린 말을 게시했다고 말하려고하지 않습니다. 그냥 재미있어. +1, 좋은 대답, 어쨌든. – khachik

8

JFrame에서 단추를 제거 할 수 없습니다. 대신 JDialog을 사용하십시오. 최대화 버튼이 없습니다.

+1

JDialog에도 최소화가 없습니다. 버튼을 클릭하면 문제가되지 않을 수도 있습니다. – Boann

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

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

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

을 제거하지 않습니다. 필요한 경우 "if"를 제거하고 업데이트하려면 "break"문을 제거하십시오. 나는 이것을 사용하여 닫기 버튼을 제거한다. – Gino

+0

프레임이 LAF로 장식 된 경우에만 가능 – kleopatra

3

JFrame 속성에서 -> maximumSize = minimumSize입니다. 그리고 resizable = false. 끝난! 버튼이 비활성화됩니다.

+1

편집기에 있습니까? 가 netbeans에서 작동하지 않습니다 ... –

-1

Netbean을 사용하는 경우 속성에서 크기 조정 가능 옵션의 선택을 취소하면됩니다. 최소화/최대화 버튼 만 비활성화됩니다.