在用户输入之前阻止代码返回值

时间:2014-07-03 18:40:38

标签: java swing jframe user-input

我有以下代码从用户通过JCombo框获得2输入,然后我将获得的输入的值s返回给调用函数,但问题是此代码显示框架以获取用户输入但在用户可以之前按“确定”按钮,它将空值返回给调用函数。我希望暂停此方法中的代码,直到用户按下“确定”按钮。 请提出建议。

package io;

import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener;
import java.awt.Insets; 
import java.io.FileNotFoundException;

public class SrcAndTargLangInput implements ActionListener {
    public static JFrame frame;
    public static JComboBox sourcLang;
    public static JComboBox targLang;
    public static JLabel setSrcLang;
    public static JLabel setTargLang;
    public static JButton ok;
    static String[] lang=new String[2];

    public SrcAndTargLangInput(){
        ok = new JButton("Ok");
        ok.setBounds(150,150,100,50);


        frame = new JFrame();
        frame.getContentPane().setLayout(null);

        frame.getContentPane().add(ok);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        Insets ins = frame.getInsets();
        frame.setSize(400+ins.left+ins.right, 200+ins.bottom+ins.top);
        setSrcLang=new JLabel("Source Language");
        frame.getContentPane().add(setSrcLang);
        setSrcLang.setBounds(50, 50, 100, 40);
        setTargLang=new JLabel("Target Language");
        frame.getContentPane().add(setTargLang);
        setTargLang.setBounds(50, 100, 100, 40);
        String[] srcLangList={"English","Spanish","French"};
        sourcLang = new JComboBox(srcLangList);
        frame.getContentPane().add(sourcLang);
        sourcLang.setBounds(250,50,100,40);
        String[] targLangList={"English","Spanish","French"};
        targLang = new JComboBox(targLangList);
        frame.getContentPane().add(targLang);
        targLang.setBounds(250,100,100,40);
        frame.setVisible(true);
        ok.addActionListener(this);

    }
    public static String[] langInfo(){
        SrcAndTargLangInput ob = new SrcAndTargLangInput();
        return lang;
    }

    public void actionPerformed(ActionEvent e){
        lang[0]=(sourcLang.getSelectedItem().toString());
        lang[1]=(targLang.getSelectedItem().toString());
        frame.setVisible(false);
    }
}

调用函数是:

String[] lg = new String[2];
lg = io.SrcAndTargLangInput.langInfo();
System.out.println(lg[0]);
System.out.println(lg[1]);

2 个答案:

答案 0 :(得分:0)

在您的代码中,您可以添加一个默认值为false的布尔变量。

在actionPerformed方法中将该值设置为true。更新logInfo方法以使线程休眠直到不执行任何操作。请看下面的代码。

import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Insets; 
import java.io.FileNotFoundException;

public class SrcAndTargLangInput implements ActionListener {
    public static JFrame frame;
    public static JComboBox sourcLang;
    public static JComboBox targLang;
    public static JLabel setSrcLang;
    public static JLabel setTargLang;
    public static JButton ok;
    static String[] lang=new String[2];
    boolean actionPerformed = false;

    public SrcAndTargLangInput(){
        ok = new JButton("Ok");
        ok.setBounds(150,150,100,50);


        frame = new JFrame();
        frame.getContentPane().setLayout(null);

        frame.getContentPane().add(ok);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        Insets ins = frame.getInsets();
        frame.setSize(400+ins.left+ins.right, 200+ins.bottom+ins.top);
        setSrcLang=new JLabel("Source Language");
        frame.getContentPane().add(setSrcLang);
        setSrcLang.setBounds(50, 50, 100, 40);
        setTargLang=new JLabel("Target Language");
        frame.getContentPane().add(setTargLang);
        setTargLang.setBounds(50, 100, 100, 40);
        String[] srcLangList={"English","Spanish","French"};
        sourcLang = new JComboBox(srcLangList);
        frame.getContentPane().add(sourcLang);
        sourcLang.setBounds(250,50,100,40);
        String[] targLangList={"English","Spanish","French"};
        targLang = new JComboBox(targLangList);
        frame.getContentPane().add(targLang);
        targLang.setBounds(250,100,100,40);
        frame.setVisible(true);
        ok.addActionListener(this);

    }
    public static String[] langInfo() throws InterruptedException{
        SrcAndTargLangInput ob = new SrcAndTargLangInput();
        while(!ob.actionPerformed) {
            Thread.sleep(1000);
        }
        return lang;
    }

    public void actionPerformed(ActionEvent e){
        lang[0]=(sourcLang.getSelectedItem().toString());
        lang[1]=(targLang.getSelectedItem().toString());
        actionPerformed = true;
        frame.setVisible(false);
        Thread.currentThread().interrupt();
    }
}

答案 1 :(得分:0)

基于以下几点:

String[] lg = new String[2];
lg = io.SrcAndTargLangInput.langInfo();

问题是您的SrcAndTargLangInput班级使用JFrame向用户询问输入。话虽如此,JFrame不是模态的,因此它不会阻止当前线程等待输入。您必须使用模态JDialogJOptionPane(显示模式对话框),以便等到用户确认其输入。

看看这些主题:

题外话

正如许多开发人员已经说过的,Swing旨在与Layout Managers一起使用,您应该避免使用setBounds()setLocation()setSize()等方法。您可能也想看看这些主题:

示例(使用JOptionPane)

请注意,在您确认输入之前,文本字段的文本不会更改。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Demo {

    private void createAndShowGui() {

        final JTextField textField = new JTextField(10);
        textField.setEditable(false);

        JButton button = new JButton("Show dialog");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JComboBox languagesComboBox = new JComboBox(new Object[]{"English","Spanish","French"});
                JPanel dialogPanel = new JPanel();
                dialogPanel.add(new JLabel("Please select an option:"));
                dialogPanel.add(languagesComboBox);

                int option = JOptionPane.showConfirmDialog(null, dialogPanel, "Select an input"
                                                        , JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);

                if(option == JOptionPane.OK_OPTION) {
                    String selectedInput = (String)languagesComboBox.getSelectedItem();
                    textField.setText(selectedInput);
                }
            }
        });


        JPanel content = new JPanel();
        content.add(new JLabel("User input:"));
        content.add(textField);
        content.add(button);

        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(content);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }


    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Demo().createAndShowGui();
            }
        }); 
    }

}