【问题标题】:How to wait for a button press in Java如何在 Java 中等待按钮按下
【发布时间】:2012-06-22 04:06:14
【问题描述】:

我有一个带有一些 JTextField 和一个 JButton 的 JFrame。我希望它表现得像 JOptionPane.showInputDialog()。基本上,我想构造它,然后调用 .start() 或使其可见的东西,然后等待按下按钮,然后返回 JTextField 的内容。我听说 wait()/notify() 可能会这样做,但我不知道这是否合适,如果合适,我能看一个如何使用它的简短示例吗?

【问题讨论】:

  • 为什么不用JDialog 代替JOptionPane
  • 这听起来可能会奏效。 JDialog 是否可以包含比 JOptonPane 更多的信息(因此它可以替换 JFrame?)
  • 实际上它们都具有相同的功能,看起来你真的不知道JOptionPane 能做什么,实际上,除了向用户显示默认对话框窗口。你可以操纵JOptionPane 来显示你需要它拥有的东西。这是一个关于JOptionPane usage 的例子,也可以看看这个:-)
  • 请看JButton(名称为“HIGHLIGHT TEXT”)的actionPerformed(...) 方法,看这一行的第二个参数int selection = JOptionPane.showConfirmDialog(frame, getOptionPanel(), "Highlight Colour : ",JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);,这可以给你一个想法,这意味着你可以添加您自己的JPanel 并将您想要放入的任何内容放入JPanel 并使用相同的JOptionPane 来满足您的特定需求:-)

标签: java swing jbutton wait notify


【解决方案1】:

在这里尝试使用JDialog 的代码示例:

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

public class DialogExample extends JFrame
{
    private JLabel nameLabel;

    public DialogExample()
    {
        super("Dialog Example");
    }

    private void createAndDisplayGUI()
    {       
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        nameLabel = new JLabel();
        contentPane.add(nameLabel);

        setContentPane(contentPane);
        setSize(200, 100);
        setLocationByPlatform(true);
        setVisible(true);

        MyDialog dialog = new MyDialog(this, "Credentials : ", true);
        dialog.createAndDisplayGUI();
    }

    public void setName(String name)
    {
        if (name.length() > 0)
            nameLabel.setText(name);
        else
            nameLabel.setText("Empty string received.");
    }


    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new DialogExample().createAndDisplayGUI();
            }
        });
    }
}

class MyDialog extends JDialog
{
    private JTextField nameField;
    private JFrame frame;

    public MyDialog(JFrame f
            , String title, boolean isModal)
    {
        super(f, title, isModal);
        frame = f;
    }

    public void createAndDisplayGUI()
    {
        JPanel contentPane = new JPanel();
        contentPane.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
        JLabel nameLabel = new JLabel("Please Enter your Name : ");
        nameField = new JTextField(10);
        JButton submitButton = new JButton("SUBMIT");
        submitButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                if (nameField.getDocument().getLength() > 0)
                    frame.setName(nameField.getText());
                else
                    frame.setName("");
                MyDialog.this.dispose();    
            }
        });

        contentPane.add(nameLabel);
        contentPane.add(nameField);
        contentPane.add(submitButton);

        add(contentPane);
        pack();
        setVisible(true);
    }
}

【讨论】:

  • 这几乎正是我正在研究的解决方案。你可能只是救了我半个小时。谢谢
  • 您很受欢迎,请保持微笑:-)。很高兴知道我的回答确实对您有所帮助:-)
  • 有一件事 - 我可以让它像 JOptionPane.showInputDialog 一样返回它的值,它不需要在框架上回呼吗?
  • 是的,是的,只需更改Submit JButtonactionPerformed(...)方法内部的逻辑,否则您可以采用我评论中显示的方法,关于使用JOptionPaneJDialog 相同的方式(在某种意义上):-)
【解决方案2】:

JDialog 也是您自定义输入对话框的解决方案,有一个库可以帮助您加快开发速度。它被称为任务日志。

更多信息http://code.google.com/p/oxbow/wiki/TaskDialogIntroduction?tm=6

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 2013-03-31
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    • 2017-01-13
    相关资源
    最近更新 更多