Creating a pop up box in Java

天涯浪子 提交于 2020-01-05 07:50:41

问题


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.


回答1:


I think JOptionPane is what you want.

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

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

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.




回答2:


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



来源:https://stackoverflow.com/questions/4968964/creating-a-pop-up-box-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!