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

馋奶兔 提交于 2019-11-27 04:27:12

问题


I have a JFrame and want to remove the maximize button from that.

I wrote the code below, but it removed maximize, minimize, and close from my 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.


回答1:


Make it not resizable:

frame.setResizable(false);

You will still have the minimize and close buttons.




回答2:


You can't remove the button from a JFrame. Use a JDialog instead. It doesn't have a maximize button.




回答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);
        }
    } }



回答4:


In JFrame properties -> maximumSize = minimumSize. And resizable = false. Done! The button is disabled.




回答5:


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



回答6:


There is described how to implement a "JFrame" without maximize and minimize buttons. You need just "incapsulate" a JFrame in JDialog :

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

}




回答7:


Go to JFrame's property and set resizeable unchecked.




回答8:


If you are using Netbean then just unselect the resizable option in properties. It will only disable Minimize/Maximize Button.




回答9:


frame.setUndecorated(true)

this will remove maximize button as well as close and minimize button.



来源:https://stackoverflow.com/questions/5625436/how-can-i-remove-just-the-maximize-button-from-a-jframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!