简体   繁体   English

如何在JFrame中删除最小化按钮并保持最大化和关闭按钮

[英]How to remove minimize button and keep maximize and close button in JFrame

I want to remove only minimize button from JFrame but want maximize and close button in JFrame title bar. 我只想从JFrame中删除最小化按钮,但是想要JFrame标题栏中的最大化和关闭按钮。

Here I am talking about removing not disabling. 在这里我说的是删除不禁用。

I don't think removing the minimize button is a good thing. 我认为删除最小化按钮不是一件好事。 But may be you can use the setUndecorated() method to remove the title bar and window edges. 但是也许您可以使用setUndecorated()方法删除标题栏和窗口边缘。 And you'll have to add your own close and maximize buttons to perfom those action. 而且,您必须添加自己的关闭按钮和最大化按钮才能执行这些操作。

Here is an example : 这是一个例子:

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractAction;
import javax.swing.JButton;

public class Example {

    public Example() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setUndecorated(true);//<---- this will disable the frame decorations
        JPanel panel = new JPanel();
        panel.add(new JLabel("titlebar"));
        //Add button maximize
        JButton button_max=new JButton("Maximize");
        button_max.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                if(frame.getExtendedState() == JFrame.NORMAL) {
                    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
                } else {
                    frame.setExtendedState(JFrame.NORMAL);
                }
            }
        });
        panel.add(button_max);
        //Add button close
        JButton button_close = new JButton(new AbstractAction("Close") {
            private static final long serialVersionUID = -4901571960357967734L;
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        panel.add(button_close);
        frame.add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
         new Example();
    }
}

Edit : 编辑:

As @peeskillet states in the comment, even with this method the window still can be resized and draged by the user. 正如@peeskillet在评论中指出的那样,即使使用此方法,窗口仍可以由用户调整大小和拖动。 The ComponentResizer class allows to do that. ComponentResizer类允许这样做。

Here is a an SO post which gives a good example to use this class with Jframe . 这是一条SO 帖子 ,它提供了一个将此类与Jframe一起使用的好例子。

It's a very big hack, which works only with Synthetica L&F because it provides a painted title bar. 这是一个非常大的hack,仅与Synthetica L&F一起使用,因为它提供了一个绘制的标题栏。 Note: this L&F is not free to use. 注意:此L&F不能免费使用。 So if you use it you must by a license. 因此,如果您使用它,则必须获得许可证。

When you use this L&F you can iterate over all component starting from root pane to find an instance of SyntheticaTitlePane . 使用此L&F时,可以从根窗格开始迭代所有组件,以找到SyntheticaTitlePane的实例。 On success you can try to access the field iconifyAction using Reflection Framework and use the method Action.setEnabled(false) on it. 成功后,您可以尝试使用反射框架访问字段iconifyAction并对其使用方法Action.setEnabled(false)

I have no idea how to access the standard title bar because it's native. 我不知道如何访问标准标题栏,因为它是本地的。 Probably it's impossible. 可能是不可能的。

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

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