我的JFrame没有显示拨盘的输出 [英] My JFrame is not showing output of the dial

查看:40
本文介绍了我的JFrame没有显示拨盘的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的DialPlot课:

public class Demo {

    private  DefaultValueDataset dataset = new DefaultValueDataset(0);

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

        DialPlot plot = new DialPlot(dataset);
        plot.setDialFrame(new StandardDialFrame());
        plot.addLayer(new DialValueIndicator(0));
        plot.addLayer(new DialPointer.Pointer());
        int redLine =maximumValue / 5 + 10;
        plot.addLayer(new StandardDialRange(maximumValue, redLine,            Color.blue));
        plot.addLayer(new StandardDialRange(redLine, minimumValue, Color.red));

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

        return new ChartPanel(new JFreeChart(plot)) {

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

这是我的JFrame GUI类

public class NewJFrame extends javax.swing.JFrame {
    Demo d=new Demo();
    int minimumValue=0;
    int maximumValue=100;
    int majorTickGap=1;

    public NewJFrame() {
    initComponents();
    d.buildDialPlot(minimumValue,maximumValue,majorTickGap);
    this.pack();
    this.setLocationRelativeTo(null);
    }

我还像这样在JFrame GUI类中添加了它:

I also added it in JFrame GUI class like this:

this.add(d.buildDialPlot(minimumValue,maximumValue,majorTickGap));

它显示相同的结果为空JFrame. 有人知道我怎么了吗?

It shows the same result empty JFrame. Anyone knows what's my mistake?

推荐答案

最低要求,您需要add()buildDialPlot()返回的ChartPanelJFrame.完整的示例显示在此处.

Art a minimum, you need to add() the ChartPanel returned by buildDialPlot() to the JFrame. A complete example is shown here.

public NewJFrame() {
    initComponents();
    ChartPanel cp = d.buildDialPlot(minimumValue, maximumValue, majorTickGap);
    this.add(cp);
    this.pack();
    this.setLocationRelativeTo(null);
}

附录:使用此处建议的方法,下面的示例将Demo添加到本地声明的JFrame

Addendum: Using the approach suggested here, the example below adds your Demo to a locally declared JFrame.

经测试:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
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.StandardDialRange;
import org.jfree.chart.plot.dial.StandardDialScale;
import org.jfree.data.general.DefaultValueDataset;

/**
 * @see https://stackoverflow.com/a/38906326/230513
 */
public class Test {

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Demo d = new Demo();
        ChartPanel cp = d.buildDialPlot(0, 100, 10);
        f.add(cp);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private static class Demo {

        private DefaultValueDataset dataset = new DefaultValueDataset(0);

        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());
            int redLine = maximumValue / 5 + 10;
            plot.addLayer(new StandardDialRange(maximumValue, redLine, Color.blue));
            plot.addLayer(new StandardDialRange(redLine, minimumValue, Color.red));

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

            return new ChartPanel(new JFreeChart(plot)) {

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

    public static void main(String[] args) {
        EventQueue.invokeLater(new Test()::display);
    }
}

这篇关于我的JFrame没有显示拨盘的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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