2016-08-11 47 views
0

的輸出。這是我的DialPlot類:我的JFrame沒有顯示撥號

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。 任何人都知道我的錯誤是什麼?

The image below shows what my example code currently produces

回答

2

藝術最小的,你需要通過add()返回buildDialPlot()JFrameChartPanel。一個完整的例子顯示爲here

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

附錄:使用方法建議here,下面的例子將您Demo到局部聲明JFrame

image

爲試:

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

可以請你指導我,我該怎麼辦呢? –

+0

有關更具體的指導,請編輯您的問題,以包括顯示您修改方法的[mcve]。 – trashgod

+0

它顯示在本地框架,但仍然根據你的方法不在GUI框架。 我不得不添加更多的組件,但當我在appframe框架上的代碼 Dimension d = Toolkit.getDefaultToolkit()。getScreenSize(); (d); 撥號的大小自動增加,根據框架大小,我不知道 –