Java 从另一个类的文本字段中读取内容
我有两个类,一个类是GUI,它要求用户选择2个文件目录,并在两个文本字段“输入行”和“输入行1”文本字段中分别显示目录。 第二个类应该读取这两个目录并执行索引操作。 我的问题是我不知道如何让第二个类将文本字段中的目录(路径)从第一个类读入“索引路径”和“文档路径” 我已尝试导入第一个类,但仍然不起作用 它给我一条n错误消息,说“输入行无法解析为变量。以下是类及其代码: 第一类代码段:Java 从另一个类的文本字段中读取内容,java,class,lucene,textfield,Java,Class,Lucene,Textfield,我有两个类,一个类是GUI,它要求用户选择2个文件目录,并在两个文本字段“输入行”和“输入行1”文本字段中分别显示目录。 第二个类应该读取这两个目录并执行索引操作。 我的问题是我不知道如何让第二个类将文本字段中的目录(路径)从第一个类读入“索引路径”和“文档路径” 我已尝试导入第一个类,但仍然不起作用 它给我一条n错误消息,说“输入行无法解析为变量。以下是类及其代码: 第一类代码段: openButton.addActionListener(new ActionListener() {
openButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int option = chooser.showOpenDialog(uploaad.this);
if (option == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
inputLine.setText(file.getAbsolutePath());
}
else {
statusbar.setText("You canceled.");
}
}
});
indexButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ap) {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int option = chooser.showOpenDialog(uploaad.this);
if (option == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
inputLine1.setText(file.getAbsolutePath());
}
else {
statusbar.setText("You canceled.");
}
}
});
String indexPath = inputLine.getText;
String docsPath = inputLine1;
boolean create = true;
for(int i=0;i<args.length;i++) {
if ("-index".equals(args[i])) {
indexPath = args[i+1];
i++;
} else if ("-docs".equals(args[i])) {
docsPath = args[i+1];
i++;
} else if ("-update".equals(args[i])) {
create = false;
}
}
if (docsPath == null) {
System.err.println("Usage: " + usage);
System.exit(1);
}
final Path docDir = Paths.get(docsPath);
if (!Files.isReadable(docDir)) {
System.out.println("Document directory '" +docDir.toAbsolutePath()+ "'
does not exist or is not readable, please check the path");
System.exit(1);
}
第二类代码段:
openButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int option = chooser.showOpenDialog(uploaad.this);
if (option == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
inputLine.setText(file.getAbsolutePath());
}
else {
statusbar.setText("You canceled.");
}
}
});
indexButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ap) {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int option = chooser.showOpenDialog(uploaad.this);
if (option == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
inputLine1.setText(file.getAbsolutePath());
}
else {
statusbar.setText("You canceled.");
}
}
});
String indexPath = inputLine.getText;
String docsPath = inputLine1;
boolean create = true;
for(int i=0;i<args.length;i++) {
if ("-index".equals(args[i])) {
indexPath = args[i+1];
i++;
} else if ("-docs".equals(args[i])) {
docsPath = args[i+1];
i++;
} else if ("-update".equals(args[i])) {
create = false;
}
}
if (docsPath == null) {
System.err.println("Usage: " + usage);
System.exit(1);
}
final Path docDir = Paths.get(docsPath);
if (!Files.isReadable(docDir)) {
System.out.println("Document directory '" +docDir.toAbsolutePath()+ "'
does not exist or is not readable, please check the path");
System.exit(1);
}
String indexath=inputLine.getText;
字符串docsPath=inputLine1;
布尔创建=真;
对于(inti=0;i,如果有单独的类,那么必须有这些类的对象
如果类上有一个字段,则必须通过对象访问该字段。如果要访问类的字段,请将该字段定义为静态字段
public class MyClass {
int a;
static int b;
}
然后,要访问:
MyClass first = new MyClass();
first.a ///
要访问b:
you just need to use MyClass.b
在我看来,文本框变量是私有的。如果是这样的话,那么你就不会对另一个类走得太远了