如何仅移除JFrame窗口的最大化按钮?

37

我有一个 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
9个回答

75

让它不可调整大小:

frame.setResizable(false);

你仍将拥有最小化和关闭按钮。


在我的 Mac 上,它禁用了“+”按钮。它真的会移除最大化按钮吗? - khachik
它在Linux上可以运行...无论如何,OP想要实现的不是调整大小,对吧? - sjr
我并不是在说你发错了什么。只是很有趣。+1,好答案,无论如何。 - khachik
1
可能禁用,但不会删除。 - Jason K.

8

您无法从JFrame中删除按钮。请改用JDialog,它不具有最大化按钮。


3
JDialog也没有最小化按钮,这可能是一个问题也可能不是。 - Boann

3
在JFrame的属性中,将maximumSize和minimumSize设置为相同,并将resizable设置为false。完成!按钮将被禁用。

这不是一个解决特殊情况的方法,其中一个人希望仅在一个方向上调整jFrame的大小,比如垂直方向(也就是改变jFrame的高度 - 我的情况)。因为这样做会完全禁用它。 - fafa

2
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);
        }
    } }

你能稍微解释一下它具体是做什么的吗? - Matthieu
2
你使用了JDialog而不是JFrame。JDialog和JFrame具有不同的行为,因此仅仅使用JDialog并不是一个普遍适用的解决方案。 - leftbit

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();
    
    /*
     * Go through the components and find the title 
     * pane and remove the buttons.  
     */
    outerloop:
    for(Component comp : comps) {
        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 */
                    break outerloop;
                }
            }
        }
    }
}

只需删除“break”语句即可删除并更新必要的“if”。我使用这个来移除关闭按钮。 - Gino
1
仅当框架由LAF装饰时,才可能发生此操作。 - kleopatra
@kleopatra 确切地说:如果没有LAF,这是无法正常工作的,因为在主JFrame中,没有一个组件的类名包含单词“Title”(至少在那个特定的循环中)。 - fafa

1

打开JFrame的属性并将可调整大小设置为未选中。


0
将类型属性更改为utility。它只会显示关闭按钮。

你这句话到底是什么意思?光说而不举例子对很多人来说是没有用的。 - fafa

-1

这里描述了如何实现一个没有最大化和最小化按钮的“JFrame”。您只需要在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);
    }
  } 

}


2
这不是一个没有最小化/最大化按钮的 JFrame。它只是一个普通的 JDialog - Martin

-1
如果您正在使用Netbean,则只需在属性中取消选择可调整大小选项。这将仅禁用最小化/最大化按钮。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接