简体   繁体   中英

Java Swing inactive button for window close

I am trying to create a gui using swing. I have a main frame and I want it to only show the x button at the top right corner. 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. Any small pieces of code or links to tutorials or apis are welcome.

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. I want it to be there but grey and totally non responsive.

maybe 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

  • full decorated (JFrame) minimize/maximize/close Button

  • only close Button JDialog and JFrame#setResiziable(false)

  • undecorated, then there missed minimize/maximize/close Button(a) and toolBar (that came from Native OS) too

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

You can use a JDialog it just have got the X button

的JDialog

And with dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
you can disable the close button.

With 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 . Passing DO_NOTHING_ON_CLOSE to this method will forbid the frame to be closed via the default close button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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