Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JFreeChart拨入NetBeans JButton_Java_Swing_Jbutton_Jfreechart - Fatal编程技术网

Java JFreeChart拨入NetBeans JButton

Java JFreeChart拨入NetBeans JButton,java,swing,jbutton,jfreechart,Java,Swing,Jbutton,Jfreechart,当我按下NetBeans中的JButton时,我正试图从JFreeChart中拨出一个有效的拨号键。问题是,尽管代码在JButton之外时看起来还可以,但当我把它放进去时,它会给我程序中的错误 private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here }

当我按下NetBeans中的
JButton
时,我正试图从
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));
    }
}

我想这是显而易见的,但我似乎没有发现问题;感谢您的帮助。

在您的示例中,有几个问题值得注意:

  • 用于封装功能;此示例在每次调用时递增
    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对象应仅在上构造和操作

  • 考虑这些来控制初始图表大小

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);
            }
        };
    }
}