禁用JFrame最小化按钮

Pan*_*apa 7 java swing jframe jdialog jwindow

我正在为我的笔记本电脑开发一个工具.我想在JFrame中禁用最小化按钮.我已经禁用了最大化和关闭按钮.

以下是禁用最大化和关闭按钮的代码:

JFrame frame = new JFrame();  
frame.setResizable(false); //Disable the Resize Button  
// Disable the Close button
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 
Run Code Online (Sandbox Code Playgroud)

请告诉我如何禁用最小化按钮.

Mad*_*mer 9

一般来说,你不能,你可以做的是用一个JDialog而不是JFrame


Dav*_*amp 8

正如@MadProgrammer所说(给他+1),这绝对不是你想要的好主意

  • 使用a JDialog并调用setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);以确保无法关闭.

  • 您也可以使用JWindow(+1到@MM)或调用setUndecorated(true);您的JFrame实例.

或者,您可能希望通过在方法中覆盖和调用来添加自己WindowAdapater的JFrame非最小化等:windowIconified(..)setState(JFrame.NORMAL);

//necessary imports
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Test {

    /**
     * Default constructor for Test.class
     */
    public Test() {
        initComponents();
    }

    public static void main(String[] args) {

        /**
         * Create GUI and components on Event-Dispatch-Thread
         */
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Test test = new Test();
            }
        });
    }
    private final JFrame frame = new JFrame();

    /**
     * Initialize GUI and components (including ActionListeners etc)
     */
    private void initComponents() {
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frame.setResizable(false);
        frame.addWindowListener(getWindowAdapter());

        //pack frame (size JFrame to match preferred sizes of added components and set visible
        frame.pack();
        frame.setVisible(true);
    }

    private WindowAdapter getWindowAdapter() {
        return new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent we) {//overrode to show message
                super.windowClosing(we);

                JOptionPane.showMessageDialog(frame, "Cant Exit");
            }

            @Override
            public void windowIconified(WindowEvent we) {
                frame.setState(JFrame.NORMAL);
                JOptionPane.showMessageDialog(frame, "Cant Minimize");
            }
        };
    }
}
Run Code Online (Sandbox Code Playgroud)


Mor*_*hai 7

如果您不想允许任何用户操作使用JWindow.


小智 6

您可以尝试将 JFrame 类型更改为 UTILITY。那么您将不会在程序中同时看到最小化 btn 和最大化 btn。