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);
}
}
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.
发布评论
评论(4)
这是使用
setUndecorated()
禁用框架装饰。Here's a related example using
setUndecorated()
to disable the frame decorations.注意:我最初编辑了 stacker 的答案,但建议我创建一个新答案。< /em>
有几种方法可以自定义用户可用的窗口控件。
目前,删除最大化和最小化按钮,同时保留标题栏和关闭按钮的唯一方法是使用
JDialog
而不是JFrame
:对话框解决方案使用户无法最小化和最大化窗口,包括通过使用快捷方式,但是它不会删除调整窗口大小的功能。
使用
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 aJFrame
: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.你可以试试这个:
You can try this: