我正在寻找解决此问题的方法:这是我的代码:
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
public class Directory{
public static void main(String args[]) throws IOException{
JFrame frame = new JFrame("Directory");
frame.setPreferredSize(new Dimension(300,300));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JProgressBar searchprogress = new JProgressBar();
JPanel panel = new JPanel();
final JButton searchbutton = new JButton("Search");
final JTextField searchfield = new JTextField();
searchfield.setPreferredSize(new Dimension(100,30));
searchprogress.setPreferredSize(new Dimension(200, 30));
searchbutton.setLocation(100, 100);
/* Start Buffered Reader */
BufferedReader br = new BufferedReader(new FileReader("test1.txt"));
String housetype = br.readLine();
String housenumber = br.readLine();
String housestreet = br.readLine();
String housepostal = br.readLine();
String houseplace = br.readLine();
String seperation = br.readLine();
/* Finish Buffered Reader */
/* Start Content Code */
JButton done = new JButton("Done");
done.setVisible(false);
JLabel housetype_label = new JLabel();
JLabel housenumber_label = new JLabel();
JLabel housestreet_label = new JLabel();
JLabel housepostal_label = new JLabel();
JLabel houseplace_label = new JLabel();
/* Finish Content Code */
/* Start Button Code */
searchbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae)
{
String searchquery = searchfield.getText();
searchprogress.setValue(100);
searchfield.setEnabled(false);
if(searchquery.equals(housetype)){
System.out.println("We Have Found A Record!!");
}}
});
/* Finish Button Code */
/* Test Field */
/* End Test Field */
panel.add(searchfield);
panel.add(done);
panel.add(searchbutton);
panel.add(searchprogress);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
基本上,在我写完这段代码后,Eclipse告诉我必须将housetype的修饰符更改为final。这确实不行,因为如果要经历不同的记录,我需要成为一个变化的值。 请帮我! D:
最佳答案
这里有多种选择:
- 最快的方法是按照 Eclipse 告诉你的去做,实际上是 Java 告诉你的。为了能够在方法内部的内部类中使用方法局部变量,变量必须是final的。
- 另一种选择是在类定义之后立即将 housetype 变量声明为实例变量。但是,在静态 main 方法中使用它意味着该变量也需要是静态的,这使其成为类变量。
另一种方法是保留代码,但声明一个额外的变量,如下所示,然后在内部类中使用 house 变量而不是 housetype。请参阅下面的完整代码:
public class Directory { public static void main(String args[]) throws IOException { JFrame frame = new JFrame("Directory"); frame.setPreferredSize(new Dimension(300, 300)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JProgressBar searchprogress = new JProgressBar(); JPanel panel = new JPanel(); final JButton searchbutton = new JButton("Search"); final JTextField searchfield = new JTextField(); searchfield.setPreferredSize(new Dimension(100, 30)); searchprogress.setPreferredSize(new Dimension(200, 30)); searchbutton.setLocation(100, 100); /* Start Buffered Reader */ final List<String> housetypes = new ArrayList<String>(); String line = ""; BufferedReader br = new BufferedReader(new FileReader("test1.txt")); while (line != null) { line = br.readLine(); housetypes.add(line); String housenumber = br.readLine(); String housestreet = br.readLine(); String housepostal = br.readLine(); String houseplace = br.readLine(); String seperation = br.readLine(); } /* Finish Buffered Reader */ /* Start Content Code */ JButton done = new JButton("Done"); done.setVisible(false); JLabel housetype_label = new JLabel(); JLabel housenumber_label = new JLabel(); JLabel housestreet_label = new JLabel(); JLabel housepostal_label = new JLabel(); JLabel houseplace_label = new JLabel(); /* Finish Content Code */ /* Start Button Code */ searchbutton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { String searchquery = searchfield.getText(); searchprogress.setValue(100); searchfield.setEnabled(false); for (String housetype : housetypes) { if (searchquery.equals(housetype)) { System.out.println("We Have Found A Record!!"); } } } }); /* Finish Button Code */ /* Test Field */ /* End Test Field */ panel.add(searchfield); panel.add(done); panel.add(searchbutton); panel.add(searchprogress); frame.add(panel); frame.pack(); frame.setVisible(true); } }
还有更多选项,但这些是最快的。
关于java - 难以通过 JButton 激活 BufferedReader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12069075/