java - NetBeans JButton 中的 JFreeChart 拨号

标签 java swing jbutton jfreechart

我试图在按下 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));
    }
}

我想这是显而易见的事情,但我似乎没有发现问题;如有任何帮助,我们将不胜感激。

最佳答案

您的示例中有几个问题值得关注:

  • 使用Action封装功能;此示例会在每次调用时递增dataset 值。

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

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

关于java - NetBeans JButton 中的 JFreeChart 拨号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30678969/

相关文章:

java - JFrame 中仅显示一个组件

java - 仅在选择时将 Jtable 单元格设置为可编辑

java - 当图片较大时,带有 JPanel 的 JScrollPane 不显示滚动条

java - JButton 不会显示另一个 jframe

java - 当另一个 JButton 被按下时添加一个 JButton

java - 将数组对象中的信息打印到单独的字段中

java - 如何使用 jcreator 制作复制粘贴脚本?

java - 在java中处理这个问题的首选方法是什么

java - 基本代码错误

java - 无法反序列化 Wiremock Json 文件配置中收到的 START_ARRAY token 异常中的 `java.lang.String` 实例