1

我编辑了一个 JFreeChart 实时图表,现在当我尝试运行它时出现此错误。当我运行它时,GUI 会弹出它的只是图形没有随机数据流。任何想法都会有很大的帮助。

public class Therm extends ApplicationFrame implements ActionListener{

private static TimeSeries ts;
JTextArea text = new JTextArea("25");
JTextArea degreeC = new JTextArea("degrees C");
JTextArea degreeF = new JTextArea("degrees F");


public Therm(final String title){
super(title);


ts = new TimeSeries("Data", Millisecond.class);
TimeSeriesCollection data = new TimeSeriesCollection(ts);

JFreeChart chart = create(data);
JFrame frame = new JFrame("GraphTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ChartPanel label = new ChartPanel(chart);
frame.getContentPane().add(label);  


ChartPanel chartPanel = new ChartPanel(chart);
JButton buttonC = new JButton("Celsius");
JButton buttonF = new JButton("Farenheit");
JButton button1 = new JButton("Toggle Extension (60 and 300 seconds)");
JButton buttonOff = new JButton("Turn Box");
buttonC.setActionCommand("C");
buttonC.addActionListener(this);
buttonF.setActionCommand("F");
buttonF.addActionListener(this);
button1.setActionCommand("ADD");
button1.addActionListener(this);
buttonOff.setActionCommand("off");
buttonOff.addActionListener(this);



Font font = new Font ("Verdana", Font.BOLD, 26);
text.setFont(font);
degreeC.setFont(font);
degreeF.setFont(font);


JPanel graph = new JPanel(new BorderLayout());
graph.add(chartPanel);
chartPanel.setPreferredSize(new java.awt.Dimension(700,300));
setContentPane(graph);


JPanel content = new JPanel(new GridLayout(3,2));
content.setLayout(new GridLayout(3,2));
content.add(text);
content.add(degreeC);
content.add(buttonC);
content.add(buttonF);
content.add(button1);
content.add(buttonOff);
add(content,BorderLayout.SOUTH);
chartPanel.setPreferredSize(new java.awt.Dimension(700,300));

} 

private JFreeChart create(final XYDataset data){
    JFreeChart chart = ChartFactory.createTimeSeriesChart(
        "Thermometer Reading",
        "Time",
        "Degrees",
        data,
        true,
        true,
        false
    );

    final XYPlot plot = chart.getXYPlot();
    ValueAxis axisy = plot.getDomainAxis();
    axisy.setFixedAutoRange(60000.0);
    axisy = plot.getRangeAxis();
    axisy.setRange(10.0,50.0);
    return chart;
    }



public void actionPerformed(final ActionEvent e) {
    if (e.getActionCommand().equals("C")) {  
        degreeC.setText("degrees C");
    }
    if (e.getActionCommand().equals("F")){
        degreeC.setText("degrees F");
    }
    if (e.getActionCommand().equals("off")){

    }
}


public static void main(String[] args) throws IOException{
    gen myGen = new gen();
    new Thread(myGen).start();



    final Therm prog = new Therm("Thermometer Graph");
    prog.pack();
    prog.setVisible(true);
}



static class gen implements Runnable{
    public void run() {
        while(true) {
            int num = 25 + (int)(Math.random() * ((40 - 30) + 1));
            System.out.println(num);
            ts.addOrUpdate(new Millisecond(), num);
            System.out.println("HI");
            try {
                Thread.sleep(20);
            }
            catch (InterruptedException ex) {
                System.out.println(ex);
            }

        }
    }
}
}

编辑:这是接收错误“”语法错误,插入“类体”以完成类声明“”在“gen”的新代码

static class gen{
    while(true) {
        // don't forget to make num final!
        final int num = 25 + (int)(Math.random() * ((40 - 30) + 1));
        System.out.println(num);
        SwingUtilities.invokeLater(new Runnable(){
          public void run() {
            ts.addOrUpdate(new Millisecond(), num);                
          }
        });
        // ts.addOrUpdate(new Millisecond(), num);
        System.out.println("HI");
        try {
            Thread.sleep(20);
        }
        catch (InterruptedException ex) {
            System.out.println(ex);
        }
    }
}
4

1 回答 1

1

TimeSeries 是一个 Swing GUI 模型,可能只应在 Swing 事件线程上更新。

如果您像这样将更新添加到事件队列会发生什么:

    while(true) {
        // don't forget to make num final!
        final int num = 25 + (int)(Math.random() * ((40 - 30) + 1));
        System.out.println(num);
        SwingUtilities.invokeLater(new Runnable(){
          public void run() {
            ts.addOrUpdate(new Millisecond(), num);                
          }
        });
        // ts.addOrUpdate(new Millisecond(), num);
        System.out.println("HI");
        try {
            Thread.sleep(20);
        }
        catch (InterruptedException ex) {
            System.out.println(ex);
        }
    }

如果这没有帮助,请打印异常的整个堆栈跟踪,并指出堆栈跟踪引用的类中的任何行。

于 2012-10-09T01:19:07.140 回答