你好,我有一个类打开一个JFrame并接受一个文本。但是,当我尝试获取文本时,它说它为空。 每次我点击按钮我想让System.out打印我在textArea中输入的文本。为什么我的输出每次都是空的?
这是我的第一类:
public class FileReader {
FileBrowser x = new FileBrowser();
private String filePath = x.filePath;
public String getFilePath(){
return this.filePath;
}
public static void main(String[] args) {
FileReader x = new FileReader();
if(x.getFilePath() == null){
System.out.println("String is null.");
}
else
{
System.out.println(x.getFilePath());
}
}
}
这是一个JFrame这需要在一个静态的字符串输入和存储。
/*
* This class is used to read the location
* of the file that the user.
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.event.*;
import javax.swing.*;
public class FileBrowser extends JFrame{
private JTextArea textArea;
private JButton button;
public static String filePath;
public FileBrowser(){
super("Enter file path to add");
setLayout(new BorderLayout());
this.textArea = new JTextArea();
this.button = new JButton("Add file");
add(this.textArea, BorderLayout.CENTER);
add(this.button, BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 100);
setVisible(true);
this.button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
filePath = new String(textArea.getText());
System.exit(0);
}
});
}
}
但每次我运行这些程序,我得到
String is null.
你期望它是什么?为什么? – 2014-10-19 19:39:12
我希望它是“测试”或任何我在textArea中输入的内容。我想使用输入的文本。 – gkmohit 2014-10-19 19:40:31
所以让它做到这一点。 – 2014-10-19 19:41:26