【问题标题】:How to activate JOptionPane from another class?如何从另一个类激活 JOptionPane?
【发布时间】:2012-03-14 10:55:50
【问题描述】:

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

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);

}

编辑:

InputNewPerson nyPerson = new InputNewPerson();
JOptionPane.showMessageDialog(null, nyPerson);
String test = nyPerson.inputName.getText();

【问题讨论】:

  • "..就像下面的代码。因为我已经打开了一个主 GUI 窗口.." 该代码无法编译。怎么可能“开放”?

标签: java swing jdialog joptionpane


【解决方案1】:

我想看看你的问题,你需要这样的东西。我制作了一个小的JDialog,您将在其中输入UserNameAnswer,然后当您按下SUBMIT JButton 时,这将被传递到原始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);
    }
}

【讨论】:

  • 欢迎您并保持微笑:-)。如果您有任何疑问,请问我:-)
【解决方案2】:

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

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

【讨论】:

  • 我正在尝试这样做,因为没有使用 multipli 文本字段的选项
  • 由于我不知道该怎么做,所以需要一些帮助?
  • 我已经取得了一些进展,但我希望获得一些帮助,如何实现 actionlistener 并获取我输入的值?
  • @3D-kreativ:我添加了更多细节并提供了一个示例。
  • @3D-kreativ:对话通常是短暂的,所以没有理由在不使用的时候有一个实例。在你需要的时候创建实例,显示它,关闭它,然后让它被垃圾回收。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-01
  • 1970-01-01
  • 2013-08-26
  • 2013-08-09
  • 2011-12-08
  • 2018-01-16
  • 1970-01-01
相关资源
最近更新 更多