代码返回空值



我有以下代码,我从一些不同的代码调用,但收到的值是空的,而如果我在这里打印它的值读取显示。似乎调用方法在不接受返回值的情况下继续前进。请提出建议

这是我正在调用的代码:

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"。

您必须保存实例并从新创建的实例返回参数lang

public static String[] langInfo()
{
        SrcAndTargLangInput result = new  SrcAndTargLangInput();
        // where is result.lang being set?
        return result.lang;
}

使用wait/notify或信号量。例如,对于信号量,它可能如下所示:

...
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]);
    }
}

相关内容

  • 没有找到相关文章

最新更新