How do I remove the maximize and minimize buttons from a JFrame?

懵懂的女人 提交于 2019-12-28 05:58:41

问题


I need to remove the maximize and minimize buttons from a JFrame. Please suggest how to do this.


回答1:


import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Dlg extends JDialog {
    public Dlg(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 {
            Dlg frame = new Dlg(new JFrame(), "No min max buttons");
            JPanel panel = new JPanel();
            panel.setSize(200, 200);
            JLabel lbl = new JLabel("blah blah");
            panel.add(lbl);
            frame.add(panel);
            frame.setSize(400, 400);
            frame.setVisible(true);
        } catch (IllegalArgumentException e) {
            System.exit(0);
        }
    }
}



回答2:


Here's a related example using setUndecorated() to disable the frame decorations.

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class FrameTest implements Runnable {

    public static void main(String[] args) {
        EventQueue.invokeLater(new FrameTest());
    }

    @Override
    public void run() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setUndecorated(true);
        JPanel panel = new JPanel();
        panel.add(new JLabel("Stackoverflow!"));
        panel.add(new JButton(new AbstractAction("Close") {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        }));
        f.add(panel);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}



回答3:


Note: I initially edited stacker's answer, but it was suggested that I create a new answer instead.

There are a few ways to customize the window controls available to your users.

Currently the only way to remove the maximize and minimize buttons, while keeping the title bar and close button, is to use a JDialog instead of a JFrame:

import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class DialogDemo {

    public static void main(String[] args) {
        JDialog dialog = new JDialog(new JFrame(), "No min max buttons");
        // necessary as setDefaultCloseOperation(EXIT_ON_CLOSE) is 
        // not available for JDialogs.
        dialog.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                System.exit(0);
            }
        });

        JLabel label = new JLabel("blah blah");
        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(400, 400));
        panel.add(label);

        dialog.add(panel);
        dialog.pack();
        dialog.setVisible(true);
    }
}

The dialog solution makes it impossible for users to minimize and maximise the window, including through the use of shortcuts, however it does not remove the ability to resize the window.

Using setResizable(false) will remove the maximize button only, at the cost of not being able to resize the window.

Lastly, as mentioned by trashgod, the setUndecorated(true) method will disable the frame decorations, removing the title bar and window edges. This makes it harder for users to drag, resize, and close the window, although not impossible, as these actions can still be performed using shortcut keys.




回答4:


You can try this:

JFrame loadingDialog = new JFrame();

JLabel label = new JLabel("blah blah");
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(400, 400));
panel.add(label);

loadingDialog.add(panel);
loadingDialog.setUndecorated(true);

loadingDialog.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
                    loadingDialog.pack();

loadingDialog.setVisible(true);


来源:https://stackoverflow.com/questions/2665355/how-do-i-remove-the-maximize-and-minimize-buttons-from-a-jframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!