繁体   English   中英

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

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

我有一个JFrame ,想从中删除最大化按钮。

我写了下面的代码,但它从我的JFrame删除了最大化、最小化和关闭

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

我只想从JFrame删除最大化按钮。

使其不可调整大小:

frame.setResizable(false);

您仍然可以使用最小化和关闭按钮。

您不能从JFrame删除按钮。 请改用JDialog 它没有最大化按钮。

在 JFrame 属性中 -> maximumSize = minimumSize。 并且可调整大小 = false。 完毕! 该按钮被禁用。

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 的属性并取消选中可调整大小。

将类型属性更改为实用程序。 它只会显示关闭按钮。

如果您使用的是 Netbean,则只需取消选择属性中的可调整大小选项。 它只会禁用最小化/最大化按钮。

这里描述了如何实现“的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);
    }
  } 

}

frame.setUndecorated(true)

这将删除最大化按钮以及关闭和最小化按钮。

暂无
暂无

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

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