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

发布于 2024-10-31 09:02:49 字数 406 浏览 6 评论 0原文

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

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

天煞孤星 2024-11-07 09:02:49

使其不可调整大小:

frame.setResizable(false);

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

Make it not resizable:

frame.setResizable(false);

You will still have the minimize and close buttons.

陌若浮生 2024-11-07 09:02:49

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

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

长发绾君心 2024-11-07 09:02:49

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

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

活雷疯 2024-11-07 09:02:49
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);
        }
    } }
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);
        }
    } }
╭⌒浅淡时光〆 2024-11-07 09:02:49

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

Go to JFrame's property and set resizeable unchecked.

童话 2024-11-07 09:02:49
/**
 * 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;
                }
            }
        }
    }
}
/**
 * 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;
                }
            }
        }
    }
}
深白境迁sunset 2024-11-07 09:02:49

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

}

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

}

っ左 2024-11-07 09:02:49

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文