如何制作像 JOptionPane.showMessageDialog(xxx,“xxx”) 这样的函数?

发布于 2024-10-15 13:12:29 字数 126 浏览 3 评论 0原文

尝试制作一个显示对话框并可以在单击确定后返回值的函数(如 JOptionPane.showMessageDialog(xxx,"xxx"))需要一个完整的周末,压力很大?谁能帮我写代码吗?

提前致谢

马卡拉

It take a full weekend with stressful in trying to make a function that show dialog and could return value after click ok (like JOptionPane.showMessageDialog(xxx,"xxx"))? could anyone help me about writing the code ?

Thanks in advance

Makara

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌逼全场 2024-10-22 13:12:29

使用 JOptionPane.showInputDialog()。

其他替代方法是将 GUI 控件交给 JOptionPane.showMessageDialog() 并在控件关闭后查询控件的状态,或者使用 JDialog。

如果您花费超过 15 分钟尝试让 JOptionPane 完全按照要求执行操作,则这是一个好兆头,表明 JOptionPane 不适合该工作。

Use a JOptionPane.showInputDialog().

Other alternatives are to hand a GUI control(s) to the JOptionPane.showMessageDialog() and query the state of the control(s) once it is closed, or use a JDialog.

If you spend more than 15 minutes trying to get a JOptionPane to do exactly as required, it is a good sign that a JOptionPane is not the class for the job.

離人涙 2024-10-22 13:12:29

这里有一个简单的方法。它是一个类而不是一个函数。如果您想仅使用两个参数来构造它,请创建一个具有所需两个参数的附加构造函数。

在这里你可以看看真正的麦考伊......

public class MyOwnJDialog extends javax.swing.JDialog {
    private String theMessage;

    public MyOwnJDialog(java.awt.Frame parent, boolean modal, String theMessage) {
        super(parent, modal);
        initComponents();
        this.theMessage = theMessage;
        jLabel1.setText(theMessage);
        setVisible(true);
    }


    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().add(jLabel1, java.awt.BorderLayout.CENTER);

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1);

        getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);
        pack();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        dispose();
    }

    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;

}

Here you have a trivial approach. It is a class and not a function. If you want to construct it with just have two parameters, make an additional constructor that has the two parameters you need.

Here you can take a look at the real McCoy...

public class MyOwnJDialog extends javax.swing.JDialog {
    private String theMessage;

    public MyOwnJDialog(java.awt.Frame parent, boolean modal, String theMessage) {
        super(parent, modal);
        initComponents();
        this.theMessage = theMessage;
        jLabel1.setText(theMessage);
        setVisible(true);
    }


    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().add(jLabel1, java.awt.BorderLayout.CENTER);

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1);

        getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);
        pack();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        dispose();
    }

    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;

}
彩虹直至黑白 2024-10-22 13:12:29
  1. 设计您需要的类以及您希望 GUI 的外观
  2. 创建类和方法的框架并开始填写详细信息。
  3. 创建一个可以将它们组合在一起并返回输出的方法。

一些有用的项目:

  1. Design what classes you will need and what you want the GUI to look like
  2. Create a skeleton of the classes and methods and start filling in the details.
  3. Create a method that could put this together and return the output.

Some useful items:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文