【问题标题】:How to make PopUp window in java如何在java中制作弹出窗口
【发布时间】:2012-02-09 18:34:24
【问题描述】:

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

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

你有什么想法吗?

【问题讨论】:

  • 可能是JOptionPane.showInputDialog(null, "This is the message", "This is the default text")?
  • 请标记答案以便解决此问题。
  • @NoName 我猜不是。他仍然很活跃……最后一次出现是在 3 月 6 日。已经有一段时间了,但仍然如此。

标签: java swing popup window


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    嗯,已经有一段时间了,但据我所知......
    如果您想要一个自定义窗口,您可以制作一个新框架并使其显示出来,就像您在主窗口中一样。 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 方法形式,或者为对象数组指定 null。在 Java 外观中,将 null 替换为可能性会产生一个具有文本字段的对话框,如下所示:

    【讨论】:

    • 我想我想做新的框架你能告诉我怎么做吗thx btw ?
    • 我在上面的例子是尽可能清晰和简洁地向你展示如何做到这一点。如果您想在示例之外对其进行自定义,请查看 Java 文档。
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:
        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();
                        }
                    }
                });
            }
        
        }
        

        【讨论】:

        • hmm ...有点接近回答问题的边界,不是吗 ;-) 无论如何,a) 请学习 java 命名约定并遵守它们 b) 永远不要进行任何手动调整大小/定位组件,这是合适的 LayoutManager 的专属任务 c) 不需要 updateComponentTree
        【解决方案6】:

        尝试使用 JOptionPane 或 Swt Shell

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多