我有以下代码通过 JCombo 框从用户获取 2 个输入,然后将获取的输入的值返回给调用函数,但问题是此代码显示框架以获取用户输入,但在用户可以之前按“确定”按钮,它会将空值返回给调用函数。我希望停止此方法中的代码,直到用户按“确定”按钮。 请提出一些建议。
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]);
最佳答案
在代码中,您可以添加一个默认值为 false 的 boolean 变量。
在actionPerformed 方法中将该值设置为true。更新您的 logInfo 方法以使线程 hibernate ,直到不执行任何操作。请看下面的代码。
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();
}
}
关于java - 防止代码在用户输入之前返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24561098/