如何从Java文件中更新JTextfield ?



我有一个程序,它查找一个包含类似信息的记事本文件:

Cardiff : 3245658
Bristol : 4726485
Manchester : 4728945

这些位置和数字来自一个自动系统,数字每20秒左右改变一次。我需要创建一个循环(我猜),以便当数字发生变化时,我的程序将返回到记事本文件并更新JTextField

中的数字

如果不清楚,请提问。

注:我已经创建了程序,以便它查找文件,获取所需的信息,并将其放入正确的位置,使用Arraylist

所有我需要的帮助是如何使代码自动更新时,有一个变化的文件,我需要做到这一点,不使用任何类型的Jbuttons,它只需要自己做。

我的全部代码

package window;
import java.awt.Color;
import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class MapTest {
    private JFrame frame;
    private JTextField txtCardiff;
    private JTextField txtBristol;
    private JTextField txtSheffield;
    private JTextField txtDerby;
    private JTextField txtGlasgow;
    private JTextField txtFalkirk;
    private JTextField txtNewcastle;
    private JTextField txtLeeds;
    private JTextField txtPreston;
    private JTextField txtManchester;
    private JTextField txtWolverhampton;
    private JTextField txtBirmingham;
    private JTextField txtMiltonKeynes;
    private JTextField txtPeterborough;
    private JTextField txtColindale;
    private JTextField txtStepneyGreen;
    private JTextField txtSlough;
    private JTextField txtFaraday;
    private JTextField txtGuildford;
    private JTextField txtSouthbank;
    BufferedReader CSVFile = new BufferedReader(new FileReader(Filepath);
    ArrayList<String> csv = new ArrayList<String>();
    private final ScheduledExecutorService scheduler =
               Executors.newScheduledThreadPool(1);
    /** * Launch the application. */
    public static void main(String[] args) {
         try {
                UIManager.setLookAndFeel(
                            UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException e1) {
                e1.printStackTrace();
            } catch (InstantiationException e1) {
                e1.printStackTrace();
            } catch (IllegalAccessException e1) {
                e1.printStackTrace();
            } catch (UnsupportedLookAndFeelException e1) {
                e1.printStackTrace();
            }
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MapTest window = new MapTest();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**
     * Create the application.
     * 
     * @throws IOException
     */
    public MapTest() throws IOException {
        try {
            initialize();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    /**
     * Initialise the contents of the frame.
     * 
     * @param output
     * @throws IOException
     */
    private void initialize() throws IOException {

                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                System.out.println(csv.get(0));
                System.out.println(csv.get(1));
                System.out.println(csv.get(2));
                System.out.println(csv.get(3));
                System.out.println(csv.get(4));
                System.out.println(csv.get(5));
                System.out.println(csv.get(6));
                System.out.println(csv.get(7));
                System.out.println(csv.get(8));
                System.out.println(csv.get(9));
                System.out.println(csv.get(10));
                System.out.println(csv.get(11));
                System.out.println(csv.get(12));
                System.out.println(csv.get(13));
                System.out.println(csv.get(14));
                System.out.println(csv.get(15));
                System.out.println(csv.get(16));
                System.out.println(csv.get(17));
                System.out.println(csv.get(18));
                System.out.println(csv.get(19));
                // above system.out's are to check to see if it was reading the right line

        frame = new JFrame();
        frame.setBounds(0, 0, 1000, 1086);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        frame.setBackground(Color.CYAN);
        txtCardiff = new JTextField();
        txtCardiff.setText(csv.get(0));
        txtCardiff.setBackground(Color.GREEN);
        txtCardiff.setBounds(305, 699, 102, 20);
        frame.getContentPane().add(txtCardiff);
        txtCardiff.setColumns(10);
        txtBristol = new JTextField();
        txtBristol.setText(csv.get(1));
        txtBristol.setBackground(Color.GREEN);
        txtBristol.setBounds(416, 780, 102, 20);
        frame.getContentPane().add(txtBristol);
        txtBristol.setColumns(10);
        txtSheffield = new JTextField();
        txtSheffield.setText(csv.get(2));
        txtSheffield.setBackground(Color.GREEN);
        txtSheffield.setBounds(517, 356, 110, 20);
        frame.getContentPane().add(txtSheffield);
        txtSheffield.setColumns(10);
        txtDerby = new JTextField();
        txtDerby.setText(csv.get(3));
        txtDerby.setBackground(Color.GREEN);
        txtDerby.setBounds(582, 451, 90, 20);
        frame.getContentPane().add(txtDerby);
        txtDerby.setColumns(10);
        txtGlasgow = new JTextField();// makes the textfield
        txtGlasgow.setText(csv.get(4)); 
        txtGlasgow.setBackground(Color.GREEN); 
        txtGlasgow.setBounds(250, 94, 102, 20);
        frame.getContentPane().add(txtGlasgow);
        txtGlasgow.setColumns(10);
        txtFalkirk = new JTextField();
        txtFalkirk.setText(csv.get(5));
        txtFalkirk.setColumns(10);
        txtFalkirk.setBackground(Color.GREEN);
        txtFalkirk.setBounds(331, 41, 102, 20);
        frame.getContentPane().add(txtFalkirk);
        txtNewcastle = new JTextField();
        txtNewcastle.setText(csv.get(6));
        txtNewcastle.setColumns(10);
        txtNewcastle.setBackground(Color.GREEN);
        txtNewcastle.setBounds(424, 141, 111, 20);
        frame.getContentPane().add(txtNewcastle);
        txtLeeds = new JTextField();
        txtLeeds.setText(csv.get(7));
        txtLeeds.setColumns(10);
        txtLeeds.setBackground(Color.GREEN);
        txtLeeds.setBounds(481, 244, 102, 20);
        frame.getContentPane().add(txtLeeds);
        txtPreston = new JTextField();
        txtPreston.setText(csv.get(8));
        txtPreston.setColumns(10);
        txtPreston.setBackground(Color.GREEN);
        txtPreston.setBounds(356, 221, 102, 20);
        frame.getContentPane().add(txtPreston);
        txtManchester = new JTextField();
        txtManchester.setText(csv.get(9));
        txtManchester.setColumns(10);
        txtManchester.setBackground(Color.GREEN);
        txtManchester.setBounds(394, 394, 124, 20);
        frame.getContentPane().add(txtManchester);
        txtWolverhampton = new JTextField();
        txtWolverhampton.setText(csv.get(10));
        txtWolverhampton.setColumns(10);
        txtWolverhampton.setBackground(Color.GREEN);
        txtWolverhampton.setBounds(360, 486, 153, 20);
        frame.getContentPane().add(txtWolverhampton);
        txtBirmingham = new JTextField();
        txtBirmingham.setText(csv.get(11));
        txtBirmingham.setColumns(10);
        txtBirmingham.setBackground(Color.GREEN);
        txtBirmingham.setBounds(424, 553, 128, 20);
        frame.getContentPane().add(txtBirmingham);
        txtMiltonKeynes = new JTextField();
        txtMiltonKeynes.setText(csv.get(12));
        txtMiltonKeynes.setColumns(10);
        txtMiltonKeynes.setBackground(Color.GREEN);
        txtMiltonKeynes.setBounds(474, 604, 135, 20);
        frame.getContentPane().add(txtMiltonKeynes);
        txtPeterborough = new JTextField();
        txtPeterborough.setText(csv.get(13));
        txtPeterborough.setColumns(10);
        txtPeterborough.setBackground(Color.GREEN);
        txtPeterborough.setBounds(655, 576, 135, 20);
        frame.getContentPane().add(txtPeterborough);
        txtColindale = new JTextField();
        txtColindale.setText(csv.get(14));
        txtColindale.setColumns(10);
        txtColindale.setBackground(Color.GREEN);
        txtColindale.setBounds(541, 699, 107, 20);
        frame.getContentPane().add(txtColindale);
        txtStepneyGreen = new JTextField();
        txtStepneyGreen.setText(csv.get(15));
        txtStepneyGreen.setColumns(10);
        txtStepneyGreen.setBackground(Color.GREEN);
        txtStepneyGreen.setBounds(667, 699, 137, 20);
        frame.getContentPane().add(txtStepneyGreen);
        txtSlough = new JTextField();
        txtSlough.setText(csv.get(16));
        txtSlough.setColumns(10);
        txtSlough.setBackground(Color.GREEN);
        txtSlough.setBounds(525, 756, 102, 20);
        frame.getContentPane().add(txtSlough);
        txtFaraday = new JTextField();
        txtFaraday.setText(csv.get(17));
        txtFaraday.setColumns(10);
        txtFaraday.setBackground(Color.GREEN);
        txtFaraday.setBounds(650, 756, 102, 20);
        frame.getContentPane().add(txtFaraday);
        txtGuildford = new JTextField();
        txtGuildford.setText(csv.get(18));
        txtGuildford.setColumns(10);
        txtGuildford.setBackground(Color.GREEN);
        txtGuildford.setBounds(525, 822, 102, 20);
        frame.getContentPane().add(txtGuildford);
        txtSouthbank = new JTextField();
        txtSouthbank.setText(csv.get(19));
        txtSouthbank.setColumns(10);
        txtSouthbank.setBackground(Color.GREEN);
        txtSouthbank.setBounds(655, 807, 115, 20);
        frame.getContentPane().add(txtSouthbank);
        JLabel lblNewLabel = new JLabel("New label");
        lblNewLabel.setForeground(Color.BLACK);
        lblNewLabel.setBackground(Color.BLACK);
        lblNewLabel
                .setIcon(new ImageIcon(
                        "FilePath
        lblNewLabel.setBounds(33, 11, 890, 1086);
        frame.getContentPane().add(lblNewLabel);
    }
} 

这是一项简单的任务。我对WatchService一无所知,但这也可以使用Timer来完成。创建一个Timer,它将每20秒查找一次记事本。您必须通过程序检查号码是否更改过。

相关内容

  • 没有找到相关文章

最新更新