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

发布于 2024-08-29 01:05:05 字数 155 浏览 6 评论 0原文

我需要从 JFrame。请建议如何执行此操作。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

指尖上得阳光 2024-09-05 01:05:05

这是使用 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);
    }
}

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

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);
    }
}
时光磨忆 2024-09-05 01:05:05

注意:我最初编辑了 stacker 的答案,但建议我创建一个新答案。< /em>

有几种方法可以自定义用户可用的窗口控件。

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

对话框解决方案使用户无法最小化和最大化窗口,包括通过使用快捷方式,但是它不会删除调整窗口大小的功能。

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

最后,正如 trashgod 提到的,setUndecorated(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:

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.

感情废物 2024-09-05 01:05:05
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);
        }
    }
}
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);
        }
    }
}
辞别 2024-09-05 01:05:05

你可以试试这个:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文