2014-03-19 68 views
0

我知道此问题早前已被询问(here,herehere),但所提供的解决方案不起作用。因此,请不要将其标记为重复。我正在使用JFreeChart绘制折线图。然后我把这个图表放入一个JPanel中,然后放入tabbedPane。 我知道直接把JPanel放在JFrame中可以调整大小,但是我怎么能这样做呢?JPanel中可调整大小的JFreeChart

public class DynamicLineAndTimeSeriesChart extends JPanel implements ActionListener { 
    private ArrayList<TimeSeries> Series = new ArrayList<TimeSeries>(); 
    private ArrayList<Double> Last = new ArrayList<Double>(); 
    private String Id1; 
    private Timer timer = new Timer(250, this); 
    public DynamicLineAndTimeSeriesChart(String id) { 

     Runtime runtime = Runtime.getRuntime(); 
     int NoOfProc = runtime.availableProcessors(); 
     for (int i = 0; i < NoOfProc; i++) { 
      Last.add(new Double(100.0)); 
      Series.add(new TimeSeries("Core" + i, Millisecond.class)); 
     } 
     Id1 = id; 
     final TimeSeriesCollection dataset = new TimeSeriesCollection(); 
     for (int i = 0; i < NoOfProc; i++) { 
      dataset.addSeries(this.Series.get(i)); 
     } 
     final JFreeChart chart = createChart(dataset); 
     timer.setInitialDelay(1000); 
     chart.setBackgroundPaint(Color.LIGHT_GRAY); 

     //Created JPanel to show graph on screen 
     final JPanel content = new JPanel(new GridLayout()); 
     final ChartPanel chartPanel = new ChartPanel(chart); 
     content.add(chartPanel, BorderLayout.CENTER); 
     content.revalidate(); 
     this.add(content); 

     timer.start(); 
    } 

createChart是一种JFreeChart类型的方法。 这个类被称为另一类

JPanel main = new JPanel(new BorderLayout()); 
main.add(demo); 
jTabbedPane1.add("Core", main); 
demo.setVisible(true); 

使用NetBeans完成框架设计。 我已经尝试了所有的解决方案,如将BorderLayout的布局更改为GridBagLayout,甚至提到了here

+0

也许content.pack()? – djb

+0

*“我知道这个问题早已被问到,但提供的解决方案并不奏效。”*在哪里?请链接.. –

+0

究竟是什么问题?调整内容大小时,是否希望chartPanel自动调整大小? –

回答

1

BorderLayout.CENTER应该让ChartPanel随着帧的大小调整而增长。我猜你有一个JPanelFlowLayout,它继续使用图表面板的首选大小。由于FlowLayout是默认值,因此您可能需要查找它。

编辑:有一个完整的示例here,它将折线图添加到选项卡式窗格中。您的DynamicLineAndTimeSeriesChart有默认FlowLayout;我想你想要一个GridLayout

public DynamicLineAndTimeSeriesChart(String id) { 
    this.setLayout(new GridLayout()); 
    … 
} 
+0

你能指定吗?我已经使用了两个JPanel,并且他们的布局已经更改为borderlayout。 – sol

+0

我不知道为什么这是行不通的;你能编辑你的问题来展示一个完整的例子吗? –

+0

已经完成了编辑,但发布完整的代码是不可能的,所以我发布了相关部分。感谢您的帮助。 – sol

0

没有看到你所有的代码,它只能被推测。但是,作为一种解决方法,你可以让你的JPanel侦听调整大小事件具有的ComponentListener,是这样的:

content.addComponentListener(new ComponentAdapter(){ 
     public void componentResized(ActionEvent e){ 
      chartPanel.setSize(content.getSize()); 
     } 
    }); 
+0

仍然没有改变,图形没有调整大小。 – sol

+0

你有没有尝试过监控veriables? chartPanel.setSize(content.getSize())之后的chartpanel的大小和它的容器是什么?我不能想到为什么这不应该起作用的原因。 –

0

我有同样的问题,我设法解决这个问题是这样的: (1)创建该扩展JPanel的自定义类 (2)得到的大小在某种程度上,你想传递给你的图表 (3)创建它返回一个像这样的“ChartPanel”对象的方法:

ChartPanel chart() { 
    //... custom code here 
    JFreeChart chart = ChartFactory.createPieChart(title, pieDataset, false, false, false);`enter code here` 
    // Now: this is the trick to manage setting the size of a chart into a panel!: 
    return new ChartPanel(chart) { 
     public Dimension getPreferredSize() { 
      return new Dimension(width, height); 
     } 
    }; 
} 

我准备了SSCCE让你知道它是如何工作的:

import java.awt.Dimension; 
import java.util.ArrayList; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import org.jfree.chart.ChartFactory; 
import org.jfree.chart.ChartPanel; 
import org.jfree.chart.JFreeChart; 
import org.jfree.data.general.DefaultPieDataset; 

public class MyPieChart extends JPanel { 

    public static void main(String[] args) { 
     example1(); 
     example2(); 
     example3(); 
    } 

    public static void example1() { 
     JPanel panel = new JPanel(); 
     panel.setBounds(50, 80, 100, 100); 
     MyPieChart piePanel = new MyPieChart("Example 1", dataset(), panel); 
     panel.add(piePanel); 
     JFrame frame = new JFrame(); 
     frame.setLayout(null); 
     frame.setBounds(10, 10, 200, 300); 
     frame.add(panel); 
     frame.setVisible(true); 
    } 

    public static void example2() { 
     MyPieChart piePanel = new MyPieChart("Example 2", dataset(), 30, 50, 100, 100); 
     JFrame frame = new JFrame(); 
     frame.setLayout(null); 
     frame.setBounds(210, 10, 200, 300); 
     frame.add(piePanel); 
     frame.setVisible(true); 
    } 

    public static void example3() { 
     MyPieChart piePanel = new MyPieChart("Example 3", dataset(), 100, 100); 
     piePanel.setLocation(0,0); 
     JFrame frame = new JFrame(); 
     frame.setLayout(null); 
     frame.setBounds(410, 10, 200, 300); 
     frame.add(piePanel); 
     frame.setVisible(true); 
    } 

    static ArrayList<ArrayList<String>> dataset() { 
     ArrayList<ArrayList<String>> dataset = new ArrayList<ArrayList<String>>(); 
     dataset.add(row("Tom", "LoggedIn", "Spain")); 
     dataset.add(row("Jerry", "LoggedOut", "England")); 
     dataset.add(row("Gooffy", "LoggedOut", "France")); 
     return dataset; 
    } 

    static ArrayList<String> row(String name, String actualState, String country) { 
     ArrayList<String> row = new ArrayList<String>(); 
     row.add(name); row.add(actualState); row.add(country); 
     return row; 
    } 

    ArrayList<ArrayList<String>> dataset; 
    DefaultPieDataset pieDataset = new DefaultPieDataset(); 
    int width, height, posX, posY; 
    int colState = 1; 
    String title; 
    String LoggedIn = "LoggedIn"; 
    String LoggedOut = "LoggedOut"; 

    public MyPieChart(String title, ArrayList<ArrayList<String>> dataset, int...args) { 

     if(args.length==2) { 
      this.width = args[0]; 
      this.height = args[1]; 
      this.setSize(width, height); 
     } 
     else if(args.length==4) { 
      this.posX = args[0]; 
      this.posY = args[1]; 
      this.width = args[2]; 
      this.height = args[3]; 
      this.setBounds(posX, posY, width, height); 
     } 
     else { 
      System.err.println("Error: wrong number of size/position arguments"); 
      return; 
     } 

     this.title = title; 
     this.dataset = dataset; 
     this.add(chart()); 
    } 

    public MyPieChart(String title, ArrayList<ArrayList<String>> dataset, JPanel panel) { 
     this.title = title; 
     this.dataset = dataset; 
     this.width = panel.getWidth(); 
     this.height = panel.getHeight(); 
     this.setBounds(panel.getBounds()); 
     this.add(chart()); 
    } 

    ChartPanel chart() { 

     int totalLoggedIn = 0; 
     int totalLoggedOut = 0; 

     for(ArrayList<String> user : dataset) { 
      if(user.get(colState).equals(LoggedIn)) totalLoggedIn++; 
      else totalLoggedOut++; 
     } 
     pieDataset.clear(); 
     pieDataset.setValue(LoggedIn +": "+ totalLoggedIn, totalLoggedIn); 
     pieDataset.setValue(LoggedOut +": "+ totalLoggedOut, totalLoggedOut); 

     JFreeChart chart = ChartFactory.createPieChart(title, pieDataset, false, false, false); 

     return new ChartPanel(chart) { // this is the trick to manage setting the size of a chart into a panel! 
      public Dimension getPreferredSize() { 
       return new Dimension(width, height); 
      } 
     }; 
    } 
} 

我真的希望它有帮助!