简体   繁体   English

Java Swing非活动按钮可关闭窗口

[英]Java Swing inactive button for window close

I am trying to create a gui using swing. 我正在尝试使用swing创建gui。 I have a main frame and I want it to only show the x button at the top right corner. 我有一个主框架,我只希望它在右上角显示x按钮。 I would also like to make this button impossible to press if certain conditions are met. 如果满足某些条件,我还要使此按钮无法按下。 I did some search on google and on java's website but I couldn't find any functions related to these two tasks. 我在Google和Java网站上进行了一些搜索,但找不到与这两个任务相关的任何功能。 Any small pieces of code or links to tutorials or apis are welcome. 欢迎使用任何小段代码或教程或api的链接。

I want the button to be there. 我希望按钮在那里。 I don't want the window to close (which I can do with the setDefaultCloseOperation) and also I'm trying to find a way to be able to make the button look greyed out and not allow it to execute the animation of getting pressed in when I click on it. 我不希望窗口关闭(我可以使用setDefaultCloseOperation来完成),而且我也在试图找到一种方法来使按钮显示为灰色,并且不允许其执行按下动画当我点击它。 I want it to be there but grey and totally non responsive. 我希望它在那里,但灰色且完全不响应。

maybe myFrame.setResizable(false); 也许myFrame.setResizable(false); and all of Events for this Button you can hande this way 以及该按钮的所有事件,您可以通过这种方式进行处理

EDIT: 编辑:

window's decorations coming from Native OS, and then you have this four options as follows 窗口的装饰来自Native OS,然后您具有以下四个选项

  • full decorated (JFrame) minimize/maximize/close Button 全装饰(JFrame)最小化/最大化/关闭按钮

  • only close Button JDialog and JFrame#setResiziable(false) 仅关闭Button JDialog和JFrame#setResiziable(false)

  • undecorated, then there missed minimize/maximize/close Button(a) and toolBar (that came from Native OS) too 未经装饰,则也错过了最小化/最大化/关闭Button(a)和toolBar(来自Native OS)

  • undecorated Top-level Container with JPanel (with GradientPaint simulated real ToolBar) contains JButton with Char X (could be enabled once time or another time disabled as you want) 带有JPanel的未经装饰的顶级容器 (带有GradientPaint 模拟的真实工具栏)包含带有Char X的JButton (可以根据需要启用一次或禁用一次)

You can use a JDialog it just have got the X button 您可以使用带有X按钮的JDialog

的JDialog

And with dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); 并带有dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
you can disable the close button. 您可以禁用关闭按钮。

With dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 使用dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
you can enable the close button 您可以启用关闭按钮

You can specify what you want to happen when close button is pressed on your Frame. 您可以指定在框架上按下关闭按钮时想要发生的情况。 If you want it to do nothing, then: 如果您不希望其执行任何操作,则:

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

You didn't search very far. 您没有搜索很远。 See http://download.oracle.com/javase/6/docs/api/javax/swing/JFrame.html#setDefaultCloseOperation%28int%29 . 请参阅http://download.oracle.com/javase/6/docs/api/javax/swing/JFrame.html#setDefaultCloseOperation%28int%29 Passing DO_NOTHING_ON_CLOSE to this method will forbid the frame to be closed via the default close button. DO_NOTHING_ON_CLOSE传递给此方法将禁止通过默认的关闭按钮关闭框架。

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

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