如何在java中创建PopUp窗口

我目前正在开发一个java应用程序。

我想显示一个包含文本区域和按钮的新窗口。

你有什么想法?

同样的答案: JOptionpane与一个例子:)

 package experiments; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; public class CreateDialogFromOptionPane { public static void main(final String[] args) { final JFrame parent = new JFrame(); JButton button = new JButton(); button.setText("Click me to show dialog!"); parent.add(button); parent.pack(); parent.setVisible(true); button.addActionListener(new java.awt.event.ActionListener() { @Override public void actionPerformed(java.awt.event.ActionEvent evt) { String name = JOptionPane.showInputDialog(parent, "What is your name?", null); } }); } } 

在此处输入图像描述

嗯它已经有一段时间但是从我记忆中…
如果你想要一个自定义窗口,你可以创建一个新的框架,使其显示就像你使用主窗口一样。 Java还有一个很棒的对话库,你可以在这里查看:

如何制作对话

这可能会为您提供您正在寻找的function,而且工作量更少。

 Object[] possibilities = {"ham", "spam", "yam"}; String s = (String)JOptionPane.showInputDialog( frame, "Complete the sentence:\n" + "\"Green eggs and...\"", "Customized Dialog", JOptionPane.PLAIN_MESSAGE, icon, possibilities, "ham"); //If a string was returned, say so. if ((s != null) && (s.length() > 0)) { setLabel("Green eggs and... " + s + "!"); return; } //If you're here, the return value was null/empty. setLabel("Come on, finish the sentence!"); 

如果您不想限制用户的选择,可以使用showInputDialog方法的forms,该方法接受较少的参数或为对象数组指定null。 在Java外观中,将null替换为可能性会导致具有文本字段的对话框如下所示:

JOptionPane是你的朋友: http : //www.javalobby.org/java/forums/t19012.html

查看Swing Dialogs (主要关注JOptionPane ,如@mcfinnigan所述)。

 public class JSONPage { Logger log = Logger.getLogger("com.prodapt.autotest.gui.design.EditTestData"); public static final JFrame JSONFrame = new JFrame(); public final JPanel jPanel = new JPanel(); JLabel IdLabel = new JLabel("JSON ID*"); JLabel DataLabel = new JLabel("JSON Data*"); JFormattedTextField JId = new JFormattedTextField("Auto Generated"); JTextArea JData = new JTextArea(); JButton Cancel = new JButton("Cancel"); JButton Add = new JButton("Add"); public void JsonPage() { JSONFrame.getContentPane().add(jPanel); JSONFrame.add(jPanel); JSONFrame.setSize(400, 250); JSONFrame.setResizable(false); JSONFrame.setVisible(false); JSONFrame.setTitle("Add JSON Data"); JSONFrame.setLocationRelativeTo(null); jPanel.setLayout(null); JData.setWrapStyleWord(true); JId.setEditable(false); IdLabel.setBounds(20, 30, 120, 25); JId.setBounds(100, 30, 120, 25); DataLabel.setBounds(20, 60, 120, 25); JData.setBounds(100, 60, 250, 75); Cancel.setBounds(80, 170, 80, 30); Add.setBounds(280, 170, 50, 30); jPanel.add(IdLabel); jPanel.add(JId); jPanel.add(DataLabel); jPanel.add(JData); jPanel.add(Cancel); jPanel.add(Add); SwingUtilities.updateComponentTreeUI(JSONFrame); Cancel.addActionListener(new ActionListener() { @SuppressWarnings("deprecation") @Override public void actionPerformed(ActionEvent e) { JData.setText(""); JSONFrame.hide(); TestCasePage.testCaseFrame.show(); } }); Add.addActionListener(new ActionListener() { @SuppressWarnings("deprecation") @Override public void actionPerformed(ActionEvent e) { try { PreparedStatement pStatement = DAOHelper.getInstance() .createJSON( ConnectionClass.getInstance() .getConnection()); pStatement.setString(1, null); if (JData.getText().toString().isEmpty()) { JOptionPane.showMessageDialog(JSONFrame, "Must Enter JSON Path"); } else { // System.out.println(eleSelectBy); pStatement.setString(2, JData.getText()); pStatement.executeUpdate(); JOptionPane.showMessageDialog(JSONFrame, "!! Added !!"); log.info("JSON Path Added"+JData); JData.setText(""); JSONFrame.hide(); } } catch (SQLException e1) { JData.setText(""); log.info("Error in Adding JSON Path"); e1.printStackTrace(); } } }); } } 

尝试使用JOptionPane或Swt Shell