我有以下代码,我从一些不同的代码调用,但接收到的值为null,而如果我在此处打印,则显示读取的值。 似乎调用方法继续运行而不返回返回值。 请提出建议
这是我打电话的代码:
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(){
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]);
但总是打印“null”。 请提出建议。
答案 0 :(得分:0)
您必须保存实例并从新创建的实例
返回参数langpublic static String[] langInfo()
{
SrcAndTargLangInput result = new SrcAndTargLangInput();
// where is result.lang being set?
return result.lang;
}
答案 1 :(得分:0)
使用wait / notify或semaphore。 例如,对于信号量,它可能如下:
...
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Semaphore;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
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];
private static final Semaphore SEMAPHORE = new Semaphore(1);
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() {
try {
SEMAPHORE.acquire();
new SrcAndTargLangInput();
// Wait until Semaphore is released
SEMAPHORE.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
SEMAPHORE.release();
return lang;
}
}
public void actionPerformed(ActionEvent e) {
lang[0] = (sourcLang.getSelectedItem().toString());
lang[1] = (targLang.getSelectedItem().toString());
frame.setVisible(false);
SEMAPHORE.release();
}
public static void main(String[] args) {
String[] lg = new String[2];
lg = SrcAndTargLangInput.langInfo();
System.out.println(lg[0]);
System.out.println(lg[1]);
}
}