2012-01-13 100 views
21

我目前正在开发一个java应用程序。如何在java中制作PopUp窗口

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

你有什么想法吗?

+4

也许'JOptionPane.showInputDialog(null,“这是消息”,“这是默认文本”)'? – fireshadow52 2012-01-13 15:22:03

+0

请标记答案,以便解决此问题。 – Ungeheuer 2016-05-24 17:38:34

回答

12

嗯,它一直是一小会儿,但是从我记得...
如果你想有一个自定义窗口,你可以只是做一个新的框架,并使其显示出来,就像你会与主窗口。 Java也有,你可以看看这里有很大的对话框库:

How to Make Dialogs

这或许可以给你你正在寻找少了一大堆的努力的功能。

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方法,它采用较少的参数或对象的数组指定空的形式。在Java的外观和感觉,用空的可能性,结果,有一个文本框,看起来像这样的对话:

+0

我想我想做新的框架可以ü演示如何做到这一点thx btw? – Carlo 2012-01-13 21:12:29

+0

我上面提到的例子大致上是清晰和简洁的,我可以告诉你如何去做。如果您想在示例之外对其进行定制,请查看Java文档。 – Prediluted 2012-01-23 14:17:24

27

了相同的答案: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); 
      } 
     }); 
    } 
} 

enter image description here

0
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(); 
       } 
      } 
     }); 
    } 

} 
+7

嗯...有点在回答问题的边界上,是不是;-)无论如何,a)请学习java命名约定并坚持它们b)从不做任何手动调整/定位组件,这是独占的合适的LayoutManager的任务c)不需要updateComponentTree – kleopatra 2013-10-04 08:00:49