Java 难以通过JButton激活BufferedReader

Java 难以通过JButton激活BufferedReader,java,eclipse,jbutton,bufferedreader,final,Java,Eclipse,Jbutton,Bufferedreader,Final,我正在寻找一个快速解决此问题的方法:以下是我的代码: 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 j

我正在寻找一个快速解决此问题的方法:以下是我的代码:

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变量声明为实例变量。但是,在static 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);
      }
    }
    
    公共类目录{
    公共静态void main(字符串args[])引发IOException{
    JFrame=newjframe(“目录”);
    frame.setPreferredSize(新尺寸(300300));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JProgressBar searchprogress=新JProgressBar();
    JPanel面板=新的JPanel();
    最终JButton searchbutton=新JButton(“搜索”);
    最终JTextField searchfield=新JTextField();
    setPreferredSize(新维度(100,30));
    searchprogress.setPreferredSize(新维度(200,30));
    搜索按钮。设置位置(100100);
    /*启动缓冲读取器*/
    最终列表housetypes=新的ArrayList();
    字符串行=”;
    BufferedReader br=新的BufferedReader(新文件读取器(“test1.txt”);
    while(行!=null){
    line=br.readLine();
    房屋类型。添加(行);
    字符串housenumber=br.readLine();
    字符串housestreet=br.readLine();
    字符串housepostal=br.readLine();
    字符串houseplace=br.readLine();
    字符串分隔=br.readLine();
    }
    /*完成缓冲读卡器*/
    /*开始内容代码*/
    JButton done=新JButton(“done”);
    完成。设置可见(假);
    JLabel housetype_label=新JLabel();
    JLabel housenumber_label=新JLabel();
    JLabel housestreet_标签=新JLabel();
    JLabel housepostal_标签=新JLabel();
    JLabel houseplace_标签=新JLabel();
    /*完成内容代码*/
    /*启动按钮代码*/
    searchbutton.addActionListener(新ActionListener(){
    已执行的公共无效行动(行动事件ae){
    String searchquery=searchfield.getText();
    searchprogress.setValue(100);
    searchfield.setEnabled(false);
    用于(字符串房屋类型:房屋类型){
    if(searchquery.equals(housetype)){
    System.out.println(“我们找到了一条记录!!”;
    }
    }
    }
    });
    /*完成按钮代码*/
    /*试验场*/
    /*终端测试场*/
    panel.add(搜索字段);
    面板。添加(完成);
    面板。添加(搜索按钮);
    添加(搜索进度);
    框架。添加(面板);
    frame.pack();
    frame.setVisible(true);
    }
    }
    
  • 有更多的选择,但这些是最快的


这里有几个选项:

  • 最快的方法是按照Eclipse告诉您的去做,实际上是Java告诉您的。为了能够在方法的内部类中使用方法局部变量,变量必须是final
  • 另一个选项是在类定义之后立即将housetype变量声明为实例变量。但是,在static 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);
      }
    }
    
    公共类目录{
    公共静态void main(字符串args[])引发IOException{
    JFrame=newjframe(“目录”);
    frame.setPreferredSize(新尺寸(300300));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JProgressBar searchprogress=新JProgressBar();
    JPanel面板=新的JPanel();
    最终JButton searchbutton=新JButton(“搜索”);
    最终JTextField searchfield=新JTextField();
    setPreferredSize(新维度(100,30));
    searchprogress.setPreferredSize(新维度(200,30));
    搜索按钮。设置位置(100100);
    /*启动缓冲读取器*/
    最终列表housetypes=新的ArrayList();
    字符串行=”;
    BufferedReader br=新的BufferedReader(新文件读取器(“test1.txt”);
    while(行!=null){
    line=br.readLine();
    房屋类型。添加(行);
    字符串housenumber=br.readLine();
    字符串housestreet=br.readLine();
    字符串housepostal=br.readLine();
    字符串houseplace=br.readLine();
    字符串分隔=br.readLine();
    }
    /*完成缓冲读卡器*/
    /*开始内容代码*/
    JButton done=新JButton(“done”);
    完成。设置可见(假);
    JLabel housetype_label=新JLabel();
    JLabel housenumber_label=新JLabel();
    JLabel housestreet_标签=新JLabel();
    JLabel housepostal_标签=新JLabel();
    JLabel houseplace_标签=新JLabel();
    /*完成内容代码*/
    /*启动按钮代码*/
    searchbutton.addActionListener(新ActionListener(){
    已执行的公共无效行动(行动事件ae){
    String searchquery=searchfield.getText();
    searchprogress.setValue(100);
    searchfield.setEnabled(false);
    用于(字符串房屋类型:房屋类型){
    if(searchquery.equals(housetype)){
    
    public void actionPerformed(ActionEvent ae)
    {
        searchButtonAction();
    });
    
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    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 {
    
        private final JTextField searchfield = new JTextField();
        private final JProgressBar searchprogress = new JProgressBar();
    
        private String housetype;
    
        public Directory() throws IOException {
    
            JFrame frame = new JFrame("Directory");
            frame.setPreferredSize(new Dimension(300, 300));
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JPanel panel = new JPanel();
            final JButton searchbutton = new JButton("Search");
            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"));
            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) {
                    searchButtonAction();
                }
            });
    
            /* 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);
    
        }
    
        private void searchButtonAction() {
            String searchquery = searchfield.getText();
            searchprogress.setValue(100);
            searchfield.setEnabled(false);
            if (searchquery.equals(housetype)) {
                System.out.println("We Have Found  A Record!!");
            }
        }
    
        public static void main(String args[]) throws IOException {
    
            new Directory();
    
        }
        }