簡體   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