2015-06-06 30 views
2

我試圖從JFreeChart撥打電話,而我按NetBeans中的JButton。問題是,雖然代碼在JButton之外似乎很好,但是當我將它放入程序時,它會給我帶來錯誤。NetBeans中的JFreeChart撥號JButton

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {           
    // TODO add your handling code here 
} 

public class DemoChartProblem { 

    private final DefaultValueDataset dataset = new DefaultValueDataset(50); 
    private final JFrame frame = new JFrame(); 

    public void main(String[] args) throws Exception { 
     new DemoChartProblem(); 
    } 

    public DemoChartProblem() { 
     frame.setPreferredSize(new Dimension(300, 300)); 
     frame.add(buildDialPlot(0, 30, 5)); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       frame.setVisible(true); 
      } 
     }); 
    } 

    private ChartPanel buildDialPlot(int minimumValue, int maximumValue, 
     int majorTickGap) { 

     DialPlot plot = new DialPlot(dataset); 
     plot.setDialFrame(new StandardDialFrame()); 
     plot.addLayer(new DialValueIndicator(0)); 
     plot.addLayer(new DialPointer.Pointer()); 

     StandardDialScale scale = new StandardDialScale(minimumValue, 
      maximumValue, -120, -300, majorTickGap, majorTickGap - 1); 
     scale.setTickRadius(0.88); 
     scale.setTickLabelOffset(0.20); 
     plot.addScale(0, scale); 

     return new ChartPanel(new JFreeChart(plot)); 
    } 
} 

我想這是明顯的,但我似乎沒有找到問題;任何幫助表示讚賞。

回答

2

幾個問題值得在你的榜樣注意:

  • 使用Action封裝的功能;本示例在每次調用時遞增dataset值。

    frame.add(new JButton(new AbstractAction("Update") { 
    
        @Override 
        public void actionPerformed(ActionEvent e) { 
         dataset.setValue(dataset.getValue().intValue() + 1); 
        } 
    }), BorderLayout.SOUTH); 
    
  • main()方法必須是static

  • Swing GUI對象應該在event dispatch thread上構建和操縱只有

  • 考慮這些alternate ways來控制初始圖表大小。

image

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.event.ActionEvent; 
import javax.swing.AbstractAction; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import org.jfree.chart.ChartPanel; 
import org.jfree.chart.JFreeChart; 
import org.jfree.chart.plot.dial.DialPlot; 
import org.jfree.chart.plot.dial.DialPointer; 
import org.jfree.chart.plot.dial.DialValueIndicator; 
import org.jfree.chart.plot.dial.StandardDialFrame; 
import org.jfree.chart.plot.dial.StandardDialScale; 
import org.jfree.data.general.DefaultValueDataset; 

public class DemoChartProblem { 

    private final DefaultValueDataset dataset = new DefaultValueDataset(41); 
    private final JFrame frame = new JFrame(); 

    public static void main(String[] args) throws Exception { 
     EventQueue.invokeLater(() -> { 
      new DemoChartProblem(); 
     }); 
    } 

    public DemoChartProblem() { 
     frame.add(buildDialPlot(0, 30, 5)); 
     frame.add(new JButton(new AbstractAction("Update") { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       dataset.setValue(dataset.getValue().intValue() + 1); 
      } 
     }), BorderLayout.SOUTH); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.setVisible(true); 
    } 

    private ChartPanel buildDialPlot(int minimumValue, int maximumValue, 
     int majorTickGap) { 

     DialPlot plot = new DialPlot(dataset); 
     plot.setDialFrame(new StandardDialFrame()); 
     plot.addLayer(new DialValueIndicator(0)); 
     plot.addLayer(new DialPointer.Pointer()); 

     StandardDialScale scale = new StandardDialScale(minimumValue, 
      maximumValue, -120, -300, majorTickGap, majorTickGap - 1); 
     scale.setTickRadius(0.88); 
     scale.setTickLabelOffset(0.20); 
     plot.addScale(0, scale); 

     return new ChartPanel(new JFreeChart(plot)) { 

      @Override 
      public Dimension getPreferredSize() { 
       return new Dimension(300, 300); 
      } 
     }; 
    } 
}