java 用Java创建一个弹出框

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

Creating a pop up box in Java

javapopup

提问by Mr Teeth

I want to create a pop up box that is activated when I click a "Check" button. What it does is ask the user if they are sure about what they requested. If yes, it does the request. If no, it goes back to it's normal state.

我想创建一个弹出框,当我单击“检查”按钮时会激活该框。它的作用是询问用户是否确定他们所请求的内容。如果是,它会执行请求。如果没有,它会恢复到正常状态。

I know what I said is a bit ambiguous but I want to create various types of pop up boxes.

我知道我说的有点模棱两可,但我想创建各种类型的弹出框。

So I was wondering is there a website that has generic pop up boxes with the code given?

所以我想知道是否有一个网站有带有给定代码的通用弹出框?

I just need a simple code which I can expand on.

我只需要一个可以扩展的简单代码。

回答by Zach L

I think JOptionPaneis what you want.

我认为JOptionPane就是你想要的。

  • showConfirmDialog: Asks a confirming question, like yes/no/cancel.
  • showInputDialog: Prompt for some input.
  • showMessageDialog: Tell the user about something that has happened.
  • showOptionDialog: The Grand Unification of the above three.
  • showConfirmDialog:询问确认问题,例如是/否/取消。
  • showInputDialog: 提示输入。
  • showMessageDialog:告诉用户发生的事情。
  • showOptionDialog: 以上三者的大统一。

A small example to get a Yes-No popup like you asked for would be:

获得您要求的 Yes-No 弹出窗口的一个小例子是:

if (JOptionPane.showConfirmDialog(null, "Are you sure about your request?", "Request", 
    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)
    == JOptionPane.YES_OPTION)
{
 //Do the request
}
else
{
 //Go back to normal
}

This solution, however, only works if you are using Swing.

但是,此解决方案仅在您使用Swing时才有效。

回答by frm

You shoudl check JDialogto create your custom message dialogs or you can use standard message dialogs in JOptionPane.

您应该检查JDialog以创建自定义消息对话框,或者您可以在JOptionPane 中使用标准消息对话框。