简体   繁体   English

如何从 JFrame 中仅删除“最大化”按钮?

[英]How can I remove just the Maximize button from a JFrame?

I have a JFrame and want to remove the maximize button from that.我有一个JFrame ,想从中删除最大化按钮。

I wrote the code below, but it removed maximize, minimize, and close from my JFrame .我写了下面的代码,但它从我的JFrame删除了最大化、最小化和关闭

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

I want to only remove the maximize button from the JFrame .我只想从JFrame删除最大化按钮。

Make it not resizable:使其不可调整大小:

frame.setResizable(false);

You will still have the minimize and close buttons.您仍然可以使用最小化和关闭按钮。

You can't remove the button from a JFrame .您不能从JFrame删除按钮。 Use a JDialog instead.请改用JDialog It doesn't have a maximize button.它没有最大化按钮。

In JFrame properties -> maximumSize = minimumSize.在 JFrame 属性中 -> maximumSize = minimumSize。 And resizable = false.并且可调整大小 = false。 Done!完毕! The button is disabled.该按钮被禁用。

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);
        }
    } }
/**
 * 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;
                }
            }
        }
    }
}

转到 JFrame 的属性并取消选中可调整大小。

change type property to utility.将类型属性更改为实用程序。 It will only show the close button.它只会显示关闭按钮。

If you are using Netbean then just unselect the resizable option in properties.如果您使用的是 Netbean,则只需取消选择属性中的可调整大小选项。 It will only disable Minimize/Maximize Button.它只会禁用最小化/最大化按钮。

There is described how to implement a "JFrame" without maximize and minimize buttons. 这里描述了如何实现“的JFrame”没有最大化和最小化按钮。 You need just "incapsulate" a JFrame in JDialog :您只需要在 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);
    }
  } 

} }

frame.setUndecorated(true) frame.setUndecorated(true)

this will remove maximize button as well as close and minimize button. 这将删除最大化按钮以及关闭和最小化按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM