简体   繁体   English

如何从JFrame中删除最大化和最小化按钮?

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

I need to remove the maximize and minimize buttons from a JFrame . 我需要从JFrame删除最大化和最小化按钮。 Please suggest how to do this. 请建议如何做到这一点。

Here's a related example using setUndecorated() to disable the frame decorations. 这是一个使用setUndecorated()来禁用框架装饰的相关示例。

替代文字

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);
    }
}

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 : 目前,在保持标题栏和关闭按钮的同时删除最大化和最小化按钮的唯一方法是使用JDialog而不是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. 使用setResizable(false)将仅删除最大化按钮,但代价是无法调整窗口大小。

Lastly, as mentioned by trashgod , the setUndecorated(true) method will disable the frame decorations, removing the title bar and window edges. 最后,如trashgod所述, setUndecorated(true)方法将禁用框架装饰,删除标题栏和窗口边缘。 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. 这使得用户更难以拖动,调整大小和关闭窗口,尽管并非不可能,因为仍然可以使用快捷键执行这些操作。

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);
        }
    }
}

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);

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

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