Java 如何从另一个类激活JOptionPane?

Java 如何从另一个类激活JOptionPane?,java,swing,jdialog,joptionpane,Java,Swing,Jdialog,Joptionpane,我有一个带有主GUI的main类,我想从中激活它,并从一个带有JOptionPane的新类中获取值,如下面的代码所示。因为我已经打开了一个主GUI窗口,我应该如何以及在哪里激活/调用下面的类,最后,我应该如何从JOptionPane获取值?帮助是宝贵的!谢谢 import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class OptionPaneTest {

我有一个带有主GUI的main类,我想从中激活它,并从一个带有JOptionPane的新类中获取值,如下面的代码所示。因为我已经打开了一个主GUI窗口,我应该如何以及在哪里激活/调用下面的类,最后,我应该如何从JOptionPane获取值?帮助是宝贵的!谢谢

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class OptionPaneTest {

    JPanel myPanel = new JPanel();
    JTextField field1 = new JTextField(10);
    JTextField field2 = new JTextField(10);
    myPanel.add(field1);
    myPanel.add(field2);
    JOptionPane.showMessageDialog(null, myPanel);

}
编辑:


JOPtionPane
提供了许多可以使用的方法。然而,当您试图做一些不适合其中一种类型的模板的事情时,最好通过创建一个子类来创建您自己的对话框。这样做将使您能够完全控制控件的布局,并能够根据需要响应按钮单击。您需要为OK按钮添加一个
ActionListener
。然后,在该回调中,可以从文本字段中提取值


创建自定义对话框的过程应该与为GUI创建主窗口的过程非常相似。除了扩展
JFrame
,您应该扩展
JDialog
。这是一个非常基本的问题。在本例中,
ActionListener
仅关闭对话框。您需要添加更多代码,从文本字段中提取值,并将它们提供给代码其余部分所需的位置。

JOPtionPane
提供了许多可以使用的代码。然而,当您试图做一些不适合其中一种类型的模板的事情时,最好通过创建一个子类来创建您自己的对话框。这样做将使您能够完全控制控件的布局,并能够根据需要响应按钮单击。您需要为OK按钮添加一个
ActionListener
。然后,在该回调中,可以从文本字段中提取值


创建自定义对话框的过程应该与为GUI创建主窗口的过程非常相似。除了扩展
JFrame
,您应该扩展
JDialog
。这是一个非常基本的问题。在本例中,
ActionListener
仅关闭对话框。您需要添加更多代码,从文本字段中提取值,并将它们提供给代码其余部分所需的位置。

我想看看您的问题,您需要这样的内容。我制作了一个小的
JDialog
,您将在其中输入
用户名
答案
,然后当您按下
提交jb按钮时,它将被传递到原始GUI,显示在相应的字段中

尝试使用此代码并询问可能出现的任何问题:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/*
 * This is the actual GUI class, which will get
 * values from the JDIalog class.
 */
public class GetDialogValues extends JFrame
{
    private JTextField userField;
    private JTextField questionField;

    public GetDialogValues()
    {
        super("JFRAME");
    }

    private void createAndDisplayGUI(GetDialogValues gdv)
    {       
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationByPlatform(true);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new GridLayout(0, 2));

        JLabel userName = new JLabel("USERNAME : ");
        userField = new JTextField();
        JLabel questionLabel = new JLabel("Are you feeling GOOD ?");
        questionField = new JTextField();

        contentPane.add(userName);
        contentPane.add(userField);
        contentPane.add(questionLabel);
        contentPane.add(questionField);

        getContentPane().add(contentPane);
        pack();
        setVisible(true);

        InputDialog id = new InputDialog(gdv, "Get INPUT : ", true);
    }

    public void setValues(final String username, final String answer)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                userField.setText(username);
                questionField.setText(answer);
            }
        });
    }

    public static void main(String... args)
    {
        Runnable runnable = new Runnable()
        {
            public void run()
            {
                GetDialogValues gdv = new GetDialogValues();
                gdv.createAndDisplayGUI(gdv);
            }
        };
        SwingUtilities.invokeLater(runnable);
    }
}

class InputDialog extends JDialog
{
    private GetDialogValues gdv;
    private JTextField usernameField;
    private JTextField questionField;
    private JButton submitButton;
    private ActionListener actionButton = new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            if (usernameField.getDocument().getLength() > 0
                && questionField.getDocument().getLength() > 0)
            {
                gdv.setValues(usernameField.getText().trim()
                    , questionField.getText().trim());
                dispose();
            }
            else if (usernameField.getDocument().getLength() == 0)
            {
                JOptionPane.showMessageDialog(null, "Please Enter USERNAME."
                    , "Invalid USERNAME : ", JOptionPane.ERROR_MESSAGE);
            }
            else if (questionField.getDocument().getLength() == 0)
            {
                JOptionPane.showMessageDialog(null, "Please Answer the question"
                    , "Invalid ANSWER : ", JOptionPane.ERROR_MESSAGE);
            }
        }
    };

    public InputDialog(GetDialogValues gdv, String title, boolean isModal)
    {
        this.gdv = gdv;
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        setLayout(new BorderLayout());
        setModal(isModal);
        setTitle(title);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(0, 2));
        JLabel usernameLabel = new JLabel("Enter USERNAME : ");
        usernameField = new JTextField();
        JLabel questionLabel = new JLabel("How are you feeling ?");
        questionField = new JTextField();

        panel.add(usernameLabel);
        panel.add(usernameField);
        panel.add(questionLabel);
        panel.add(questionField);

        submitButton = new JButton("SUBMIT");
        submitButton.addActionListener(actionButton);

        add(panel, BorderLayout.CENTER);
        add(submitButton, BorderLayout.PAGE_END);

        pack();
        setVisible(true);
    }
}

我想看看你的问题,你需要这样的东西。我制作了一个小的
JDialog
,您将在其中输入
用户名
答案
,然后当您按下
提交jb按钮时,它将被传递到原始GUI,显示在相应的字段中

尝试使用此代码并询问可能出现的任何问题:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/*
 * This is the actual GUI class, which will get
 * values from the JDIalog class.
 */
public class GetDialogValues extends JFrame
{
    private JTextField userField;
    private JTextField questionField;

    public GetDialogValues()
    {
        super("JFRAME");
    }

    private void createAndDisplayGUI(GetDialogValues gdv)
    {       
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationByPlatform(true);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new GridLayout(0, 2));

        JLabel userName = new JLabel("USERNAME : ");
        userField = new JTextField();
        JLabel questionLabel = new JLabel("Are you feeling GOOD ?");
        questionField = new JTextField();

        contentPane.add(userName);
        contentPane.add(userField);
        contentPane.add(questionLabel);
        contentPane.add(questionField);

        getContentPane().add(contentPane);
        pack();
        setVisible(true);

        InputDialog id = new InputDialog(gdv, "Get INPUT : ", true);
    }

    public void setValues(final String username, final String answer)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                userField.setText(username);
                questionField.setText(answer);
            }
        });
    }

    public static void main(String... args)
    {
        Runnable runnable = new Runnable()
        {
            public void run()
            {
                GetDialogValues gdv = new GetDialogValues();
                gdv.createAndDisplayGUI(gdv);
            }
        };
        SwingUtilities.invokeLater(runnable);
    }
}

class InputDialog extends JDialog
{
    private GetDialogValues gdv;
    private JTextField usernameField;
    private JTextField questionField;
    private JButton submitButton;
    private ActionListener actionButton = new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            if (usernameField.getDocument().getLength() > 0
                && questionField.getDocument().getLength() > 0)
            {
                gdv.setValues(usernameField.getText().trim()
                    , questionField.getText().trim());
                dispose();
            }
            else if (usernameField.getDocument().getLength() == 0)
            {
                JOptionPane.showMessageDialog(null, "Please Enter USERNAME."
                    , "Invalid USERNAME : ", JOptionPane.ERROR_MESSAGE);
            }
            else if (questionField.getDocument().getLength() == 0)
            {
                JOptionPane.showMessageDialog(null, "Please Answer the question"
                    , "Invalid ANSWER : ", JOptionPane.ERROR_MESSAGE);
            }
        }
    };

    public InputDialog(GetDialogValues gdv, String title, boolean isModal)
    {
        this.gdv = gdv;
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        setLayout(new BorderLayout());
        setModal(isModal);
        setTitle(title);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(0, 2));
        JLabel usernameLabel = new JLabel("Enter USERNAME : ");
        usernameField = new JTextField();
        JLabel questionLabel = new JLabel("How are you feeling ?");
        questionField = new JTextField();

        panel.add(usernameLabel);
        panel.add(usernameField);
        panel.add(questionLabel);
        panel.add(questionField);

        submitButton = new JButton("SUBMIT");
        submitButton.addActionListener(actionButton);

        add(panel, BorderLayout.CENTER);
        add(submitButton, BorderLayout.PAGE_END);

        pack();
        setVisible(true);
    }
}

我尝试这么做是因为没有多个文本字段的选项有些帮助很宝贵,因为我不知道怎么做?我已经取得了一些进展,但是我想提供一些帮助,如何实现actionlistener并获取我输入的值?@3D Krativ:我添加了一些详细信息,并提供了一个示例。@3D Krativ:对话框通常是短暂的,因此没有理由在不使用它的情况下有一个实例。在需要时创建实例,显示它,关闭它,然后让它被垃圾收集。我尝试这样做,因为没有多个文本字段选项。有些帮助很宝贵,因为我不知道如何做?我已经取得了一些进展,但是我想提供一些帮助,如何实现actionlistener并获取我输入的值?@3D Krativ:我添加了一些详细信息,并提供了一个示例。@3D Krativ:对话框通常是短暂的,因此没有理由在不使用它的情况下有一个实例。在需要的时候创建实例,显示它,关闭它,然后让它被垃圾收集。”…就像下面的代码一样。因为我已经打开了一个主GUI窗口…”该代码没有编译。它怎么可能是“打开的”?“。就像下面的代码。因为我已经打开了一个主GUI窗口…”该代码没有编译。它怎么可能是“开放的”?欢迎您并保持微笑:-)。如果你有任何疑问,一定要问我:-)欢迎你并保持微笑:-)。如果你有任何疑问,一定要问我:-)