2016-08-11 4 views
0

Das ist mein DialPlot Klasse:Mein JFrame zeigt nicht Ausgang des Wahl

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

Das ist mein JFrame GUI-Klasse

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

Ich habe es auch in JFrame GUI-Klasse wie folgt aus:

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

Es zeigt das gleiche Ergebnis leer JFrame. Wer weiß, was ist mein Fehler?

The image below shows what my example code currently produces

Antwort

2

Art ein Minimum, müssen Sie add() die von buildDialPlot() zum JFrame zurück ChartPanel. Ein vollständiges Beispiel wird gezeigt here.

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

Nachtrag: Mit dem Ansatz here vorgeschlagen, das folgende Beispiel fügt Ihre Demo zu einem lokal JFrame erklärt.

image

Wie getestet:

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

können Sie mir bitte führen, wie kann ich das tun? –

+0

Für genauere Anleitungen bearbeiten Sie Ihre Frage bitte so, dass sie eine [mcve] enthält, die Ihren überarbeiteten Ansatz zeigt. – trashgod

+0

es in lokalen Rahmen angezeigt, aber immer noch nach Ihrer Methode nicht in GUI-Rahmen. Ich muss einige weitere Komponenten hinzufügen, aber wenn ich apply Code auf Rahmen Dimension folgt d = Toolkit.getDefaultToolkit(). GetScreenSize(); f.setSize (d); die Größe der Wahl automatisch zu erhöhen je nach Rahmengröße, was ich nicht weiß –