【问题标题】:My JFrame is not showing output of the dial我的 JFrame 没有显示表盘的输出
【发布时间】:2016-08-11 21:13:38
【问题描述】:

这是我的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。 有谁知道我的错误是什么?

【问题讨论】:

    标签: java swing jframe jfreechart


    【解决方案1】:

    艺术至少,你需要add()ChartPanel返回buildDialPlot()JFrame。一个完整的例子显示在here

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

    附录:使用建议的方法here,下面的示例将您的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);
        }
    }
    

    【讨论】:

    • 你能指导我怎么做吗?
    • 如需更具体的指导,请编辑您的问题以包含minimal reproducible example,以显示您修改后的方法。
    • 它显示在本地框架中,但仍然根据您的方法而不是在 gui 框架中。我必须添加更多组件但是当我在框架 Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); 上应用以下代码时f.setSize(d);表盘大小会根据我不知道的帧大小自动增加
    • 我怀疑 GUI 编辑器妨碍了您;我会推荐 cited 的方法,因为它更具可扩展性。
    • 非常感谢我只需要设置框架或面板的布局再次感谢您的时间
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多