简体   繁体   中英

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.

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. 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. The ComponentResizer class allows to do that.

Here is a an SO post which gives a good example to use this class with Jframe .

It's a very big hack, which works only with Synthetica L&F because it provides a painted title bar. Note: this L&F is not free to use. 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 . On success you can try to access the field iconifyAction using Reflection Framework and use the method Action.setEnabled(false) on it.

I have no idea how to access the standard title bar because it's native. Probably it's impossible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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