Java 我的JFrame没有显示刻度盘的输出

Java 我的JFrame没有显示刻度盘的输出,java,swing,jframe,jfreechart,Java,Swing,Jframe,Jfreechart,这是我的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);

这是我的
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类中添加了它,如下所示:

this.add(d.buildDialPlot(minimumValue,maximumValue,majorTickGap));
它显示相同的结果空
JFrame
。 有人知道我犯了什么错吗


至少,您需要将
buildDialPlot()
返回的
图表面板
添加到
JFrame
。给出了一个完整的示例

附录:使用建议的方法,下面的示例将您的
Demo
添加到本地声明的
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);
    }
}

至少,您需要将
buildDialPlot()
返回的
ChartPanel
添加到
JFrame
。给出了一个完整的示例

附录:使用建议的方法,下面的示例将您的
Demo
添加到本地声明的
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);
    }
}

你能指导我怎么做吗?要获得更具体的指导,请编辑你的问题,包括一个显示你修改过的方法。它显示在本地框架中,但仍然根据你的方法,而不是gui框架。我必须在中添加更多的组件,但当我在框架维度d=Toolkit.getDefaultToolkit().getScreenSize()上应用以下代码时;f、 设置大小(d);拨号盘的大小根据帧大小自动增加,我不知道,我怀疑GUI编辑器正在妨碍您;我建议采用更具可扩展性的方法。非常感谢,我只需设置框架或面板的布局。再次感谢您的时间。您可以指导我如何做到这一点吗?有关更具体的指导,请编辑您的问题,使其包含一个显示您修改过的方法的列表。它显示在本地框架中,但仍根据您的方法而不是gui框架中。我必须在中添加更多的组件,但当我在框架维度d=Toolkit.getDefaultToolkit().getScreenSize()上应用以下代码时;f、 设置大小(d);拨号盘的大小根据帧大小自动增加,我不知道,我怀疑GUI编辑器正在妨碍您;我建议采用更具可扩展性的方法。非常感谢,我只需设置框架或面板的布局。再次感谢您的时间