如何在程序运行时显示实时更改的文本数据?

时间:2014-07-28 21:37:40

标签: java jtextarea worldwind

例如,我想找到一辆车在车辆离开无线电塔时的特定时刻接收瞬时信号强度。车辆与塔架的瞬时距离也将实时显示。

我可以使用JTextArea在程序运行时显示信号和距离的变化值吗?以下是我正在考虑做的一般概念。

 public class Stuff
 {
 public static double calculateSignal(double d, double f)
        {
            GUI.d=Mover.instantDistance(animpos); //needs to be displayed


            double atten=20*(Math.log10(d))+20*(Math.log10(f))+32.44; //would also like to display this

            return atten;
        }

    ...
    JTextArea textArea_3 = new JTextArea();
    textArea_3.setEditable(false);
    textArea_3.setText(GUI.d); //would have to be converted to String
    textArea_3.setBounds(147, 481, 215, 32);
    panel_3.add(textArea_3);


    JTextArea textArea_4 = new JTextArea();
    textArea_4.setEditable(false);
    textArea_4.setText(atten); //would have to be converted to String
    textArea_4.setBounds(147, 527, 215, 27);
    panel_3.add(textArea_4);

1 个答案:

答案 0 :(得分:1)

您可以使用发布商监听模式(code

import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class PublisherListenerSignalExample {

    private interface Listener {
        void onEventRecieved(Event e);
    }

    private static class Event {
        private double distance;
        private double atten;

        public Event(double distance, double atten) {
            this.distance = distance;
            this.atten = atten;
        }

        public double getDistance() {
            return distance;
        }

        public double getAtten() {
            return atten;
        }

    }

    private static class GUI {

        public static double d;

    }

    private static class Mover {

        public static double instantDistance(double animpos) {
            return 0;
        }

    }

    private static double distance, animpos;

    private static List<Listener> listeners = new LinkedList<>();

    private static void fire(Event event) {
        for(Listener l : listeners) {
            l.onEventRecieved(event);
        }
    }

    public static void addListener(Listener l) {
        listeners.add(l);
    }

    public static double calculateSignal(double d, double f) {

        GUI.d = Mover.instantDistance(animpos); // needs to be displayed

        double atten = 20 * (Math.log10(d)) + 20 * (Math.log10(f)) + 32.44; // would
                                                                            // also
                                                                            // like
                                                                            // to
                                                                            // display
                                                                            // this

        fire(new Event(d, atten));
        return atten;
    }

    public static void main(String[] args) {

        addListener(new Listener() {

            @Override
            public void onEventRecieved(Event e) {
                System.out.println(String.format("distance=%f atten=%f", e.getDistance(), e.getAtten()));
            }
        });

        final JTextArea textArea_3 = new JTextArea();
        textArea_3.setEditable(false);
        textArea_3.setBounds(147, 481, 215, 32);


        final JTextArea textArea_4 = new JTextArea();
        textArea_4.setEditable(false);
        textArea_4.setBounds(147, 527, 215, 27);

        addListener(new Listener() {

            @Override
            public void onEventRecieved(final Event e) {
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        textArea_3.setText(""+e.getDistance()); //would have to be converted to String
                        textArea_4.setText(""+e.getAtten()); //would have to be converted to String
                    }
                });

            }
        });

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JPanel panel = new JPanel();
                frame.setContentPane(panel);
                panel.add(textArea_3);
                panel.add(textArea_4);
                frame.pack();
                frame.setVisible(true);
            }
        });


        Thread worker = new Thread(new Runnable() {

            @Override
            public void run() {

                Random rnd = new Random();

                while(true) {

                    calculateSignal(rnd.nextDouble(), rnd.nextDouble());
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }

            }
        });

        worker.setDaemon(true);
        worker.start();

    }

}