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
How do I remove the maximize and minimize buttons from a JFrame?
提问by silverkid
采纳答案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()
用于禁用框架装饰的相关示例。
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 JDialog
instead 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);