禁用JFrame最小化按钮

时间:2012-10-28 07:45:39

标签: 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); 

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

5 个答案:

答案 0 :(得分:9)

一般情况下,您不能,您可以使用JDialog代替JFrame

答案 1 :(得分:8)

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

  • 使用JDialog并致电setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);以确保无法关闭。

  • 您还可以在JWindow个实例上使用setUndecorated(true);(+1到@M.M.)或致电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");
            }
        };
    }
}

答案 2 :(得分:7)

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

答案 3 :(得分:1)

您可以尝试将JFrame类型更改为UTILITY。然后你不会在你的程序中看到最小化btn和最大化btn。

答案 4 :(得分:0)

我建议您使用jframe.setUndecorated(true),因为您没有使用任何窗口事件,也不希望调整应用程序的大小。如果您想移动面板,请使用我制作的MotionPanel

相关问题