NetBeans JButton中的JFreeChart拨号 [英] JFreeChart dial in NetBeans JButton

查看:77
本文介绍了NetBeans JButton中的JFreeChart拨号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我按NetBeans中的JButton时,我试图通过JFreeChart进行拨号.问题在于,尽管在JButton之外时该代码看起来还可以,但是当我将其放入其中时,却给我程序带来错误.

I'm trying to make a dial from JFreeChart work while I press a JButton made in NetBeans. The problem is that although the code seems ok while outside the JButton, it gives me errors in the program when I put it inside.

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

我想这很明显,但是我似乎没有发现问题.任何帮助表示赞赏.

I guess it's something obvious, but I dont seem to find the problem; any help is appreciated.

推荐答案

在您的示例中应注意几个问题:

Several issues merit attention in your example:

  • 使用 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.

  • The main() method must be static.

    仅在考虑以下替代方法以控制初始图表大小.

    Consider these alternate ways to control the initial chart size.

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

    这篇关于NetBeans JButton中的JFreeChart拨号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆