java 将 JPanel 变成 JOptionPane.OK_OPTION

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16267562/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:28:02  来源:igfitidea点击:

making a JPanel into a JOptionPane.OK_OPTION

javaswingevent-handlingjpaneljoptionpane

提问by Jordan Atkinson

currently i have a class which extends JPanel and basically shows some information about an object passed into its constructor. There are various labels and Image icons on the screen and has a BorderLayout set.

目前我有一个扩展 JPanel 的类,基本上显示了有关传递给其构造函数的对象的一些信息。屏幕上有各种标签和图像图标,并设置了 BorderLayout。

This panel is triggered when the user left clicks on an ImageIcon from the main GUI and shows up on the screen.

当用户左键单击主 GUI 中的 ImageIcon 并显示在屏幕上时,将触发此面板。

i was wondering, how (if there is a way) i could implement the JOptionPane.OK_OPTION onto the whole Panel so that i dont have to close the panel using Event handling, as the screen is simply to show information and when the user has finished, they press okay and the panel should close off.

我想知道,如何(如果有办法)我可以在整个面板上实现 JOptionPane.OK_OPTION,这样我就不必使用事件处理关闭面板,因为屏幕只是为了显示信息以及用户何时完成,他们按 OK,面板应该关闭。

thanks Jordan

谢谢乔丹

回答by Vishal K

You can simply pass the object of that JPanelwithin the JOptionPane. For example:

您可以简单地JPanelJOptionPane. 例如:

 JPanel panel = new JPanel();
 panel.add(new JButton("Click"));
 panel.add(new JTextField(20));
 panel.add(new JLabel("Label"));
 JOptionPane.showMessageDialog(null,panel,"Information",JOptionPane.INFORMATION_MESSAGE);

Above code will put the JPanelon JOptionPanemessage dialog. When you click OKthe panel closes Off.

上面的代码将JPanel打开JOptionPane消息对话框。当您单击OK面板时关闭关闭。