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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2665355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:32:17  来源:igfitidea点击:

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

javaswingjframejdialog

提问by silverkid

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

我需要从 .xml 文件中删除最大化和最小化按钮JFrame。请建议如何做到这一点。

采纳答案by stacker

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

回答by trashgod

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

这是一个setUndecorated()用于禁用框架装饰的相关示例。

alt text

替代文字

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

回答by Daniel

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

注意:我最初编辑了stacker的答案,但有人建议我创建一个新答案。

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 JDialoginstead of a JFrame:

目前,在保留标题栏和关闭按钮的同时删除最大化和最小化按钮的唯一方法是使用 aJDialog而不是 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.

使用setResizable(false)将仅删除最大化按钮,代价是无法调整窗口大小。

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.

最后,正如trashgod所提到的,该setUndecorated(true)方法将禁用框架装饰,移除标题栏和窗口边缘。这使得用户更难拖动、调整大小和关闭窗口,尽管并非不可能,因为这些操作仍然可以使用快捷键执行。

回答by Eason Xiao

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