不断更新GUI JLabel文本

问题描述 投票:0回答:1

我目前有一个程序正在从不断更新的文本文件中读取实时信息,该程序位于其自己的单独类“Reciever”中。为此,为了简单起见,我刚刚创建了一个随着时间的推移而增加的 int 。所有相关数字都添加到 int x 中。为了简单起见,在我提供的示例中,TimerTask 每秒都会更新 x 的值。现在我有一个 GUI 类,当我单击 GUI 中的按钮时,它会运行 Reciever 类。我希望 GUI 类中的 JLabel 不断打印来自 Reciever 类的更新值 x,但我不知道该怎么做。

这是我的接收器类,运行时每秒更新一个值。

package DataParsing;

import java.util.Timer;
import java.util.TimerTask;

public class Reciever {
    private static int x;
    private static Timer timer = new Timer();
    public static void main(String[] args) {
        x = 1;
        
        timer.schedule(new RecallLoop22(), 1, 1000);

    }
    public int getX() {
        return this.x;
    }
    
    public void setX(int b) {
        this.x = b;
    }
}

class RecallLoop22 extends TimerTask {
    private Reciever recieve = new Reciever();
    private static int i = 0;
    public void run() {
        recieve.setX(i);
        i++;
        System.out.println(recieve.getX() + " Recieve X");
    }
}


这是我的 GUI 类,当我按下按钮时,我希望它将不断更新的值打印到 JLabel 中。

package DataParsing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;

public class NGUI implements ActionListener {
    private Reciever recieve = new Reciever();
    private JLabel label = new JLabel("");
    private DatabaseSorter sort = new DatabaseSorter();
    private JButton button1 = new JButton("Foo");

    public NGUI() {

        label.setText("B: " + recieve.getX());
        JFrame frame = new JFrame();

        JPanel panel = new JPanel();

        panel.add(label);

        panel.add(button1);
        button1.addActionListener(this);

        panel.setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50));
        panel.setLayout(new FlowLayout());

        frame.add(panel, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        String[] arr = new String[0];
        new NGUI();

    }

    public void actionPerformed(ActionEvent e) {
        String[] arr = new String[0];
        if (e.getActionCommand().equals("Foo")) {
            recieve.main(arr);
            System.out.println("Recieve X value " + recieve.getX());
            label.setText("B: " + recieve.getX());
            button1.setBackground(Color.GREEN);

        }
    }
}

老实说,我不知道从这里该做什么。我发现的所有与此相关的帖子都不太符合我正在寻找的内容,也不足以让我从中获得。

任何帮助将不胜感激!

java user-interface jlabel timertask
1个回答
0
投票

这就是(拿你的代码)Hovercraft Full Of Eels 的意思:

import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Container;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.event.*;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

import java.util.List;

import javax.swing.*;

public class F extends JFrame {
    JLabel progress;

    class Receiver extends SwingWorker<Void, String> {
        private static Timer timer = new Timer();
        private static DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM);

        @Override
        protected Void doInBackground() {
            timer.schedule(new RecallLoop22(), 1, 1000);
            return null; 
        }

        protected void process(List<String> chunks) {
            progress.setText(chunks.get(0));
        }


        private class RecallLoop22 extends TimerTask {

            public void run() {
                publish(dtf.format(LocalTime.now()));
            }
        }
    }

    private void setGui() {
        try {
            setLocation(0, 100);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Container cp = getContentPane();
            cp.setLayout(new FlowLayout());
            progress = new JLabel("0");
            cp.add(progress);
            new Receiver().execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            EventQueue.invokeAndWait(() -> {
                F f = new F();
                f.setGui();
                f.setSize(200, 200);
                f.setVisible(true);
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.