Java JPanel中可调整大小的JFreeChart
我知道之前有人问过这个问题(,和),但提供的解决方案不起作用。因此,请不要将其标记为重复。我正在使用JFreeChart绘制折线图。然后我把这个图表放在一个JPanel中,然后放在一个选项卡窗格中。 我知道直接将JPanel放在JFrame中可以使其调整大小,但我如何才能这样做呢Java JPanel中可调整大小的JFreeChart,java,swing,netbeans,jfreechart,Java,Swing,Netbeans,Jfreechart,我知道之前有人问过这个问题(,和),但提供的解决方案不起作用。因此,请不要将其标记为重复。我正在使用JFreeChart绘制折线图。然后我把这个图表放在一个JPanel中,然后放在一个选项卡窗格中。 我知道直接将JPanel放在JFrame中可以使其调整大小,但我如何才能这样做呢 public class DynamicLineAndTimeSeriesChart extends JPanel implements ActionListener { private ArrayList&l
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();
}
框架设计是使用NetBeans完成的。
我已经尝试了所有的解决方案,比如将布局从BorderLayout更改为GridBagLayout,甚至是前面提到的那种。
BorderLayout.CENTER
应该让图表面板随着框架的大小而增长。我猜您有一个JPanel
和FlowLayout
,它继续使用图表面板的首选大小。由于FlowLayout
是默认设置,因此您可能需要查找它
编辑:有一个完整的示例,它将折线图添加到选项卡式窗格中
编辑:您的DynamicLineAndTimeSeriesChart
具有默认的FlowLayout
;我想你需要一个网格布局
public DynamicLineAndTimeSeriesChart(String id) {
this.setLayout(new GridLayout());
…
}
没有看到所有的代码,这只能是猜测。但是,作为一种解决方法,您可以使用ComponentListener让您的JPanel侦听调整大小事件,如下所示:
content.addComponentListener(new ComponentAdapter(){
public void componentResized(ActionEvent e){
chartPanel.setSize(content.getSize());
}
});
我也遇到了同样的问题,我成功地解决了这个问题:
(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);
}
};
}
}
导入java.awt.Dimension;
导入java.util.ArrayList;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入org.jfree.chart.ChartFactory;
导入org.jfree.chart.ChartPanel;
导入org.jfree.chart.JFreeChart;
导入org.jfree.data.general.DefaultPieDataset;
公共类MyPieChart扩展了JPanel{
公共静态void main(字符串[]args){
例1();
例2();
例3();
}
公共静态无效示例1(){
JPanel面板=新的JPanel();
面板.立根(50,80,100,100);
MyPieChart piePanel=新的MyPieChart(“示例1”,dataset(),panel);
面板。添加(面板);
JFrame=新JFrame();
frame.setLayout(空);
框架.立根(10,10,200,300);
框架。添加(面板);
frame.setVisible(true);
}
公共静态无效示例2(){
MyPieChart Piecanel=新的MyPieChart(“示例2”,dataset(),30,50,100,100);
JFrame=新JFrame();
frame.setLayout(空);
机架立根(210、10、200、300);
框架。添加(面板);
frame.setVisible(true);
}
公共静态无效示例3(){
MyPieChart Piecanel=新的MyPieChart(“示例3”,dataset(),100100);
面板设置位置(0,0);
JFrame=新JFrame();
frame.setLayout(空);
框架.立根(410,10,200,300);
框架。添加(面板);
frame.setVisible(true);
}
静态ArrayList数据集(){
ArrayList数据集=新的ArrayList();
添加(行(“Tom”、“LoggedIn”、“西班牙”);
添加(行(“杰里”、“LoggedOut”、“英格兰”);
添加(行(“Gooffy”、“LoggedOut”、“France”);
返回数据集;
}
静态ArrayList行(字符串名称、字符串实际状态、字符串国家){
ArrayList行=新的ArrayList();
行添加(名称);行添加(实际状态);行添加(国家);
返回行;
}
ArrayList数据集;
DefaultPieDataset=新的DefaultPieDataset();
int宽度、高度、posX、posY;
int colState=1;
字符串标题;
字符串LoggedIn=“LoggedIn”;
字符串LoggedOut=“LoggedOut”;
公共MyPieChart(字符串标题、ArrayList数据集、int…args){
如果(参数长度==2){
this.width=args[0];
this.height=args[1];
此.setSize(宽度、高度);
}
else if(args.length==4){
this.posX=args[0];
this.posY=args[1];
this.width=args[2];
this.height=args[3];
此.setBounds(posX、posY、宽度、高度);
}
否则{
System.err.println(“错误:大小/位置参数数量错误”);
返回;
}
this.title=标题;
this.dataset=数据集;
添加(chart());
}
公共MyPieChart(字符串标题、ArrayList数据集、JPanel面板){
this.title=标题;
this.dataset=数据集;
this.width=panel.getWidth();
this.height=panel.getHeight();
this.setBounds(panel.getBounds());
添加(chart());
}
图表面板图表(){
int totaloggedin=0;
int totaloggedout=0;
for(ArrayList用户:数据集){
if(user.get(colState).equals(LoggedIn))totaloggedin++;
else totaloggedout++;
}
pieDataset.clear();
设置值(LoggedIn+:“+totaloggedin,totaloggedin);
设置值(LoggedOut+“:”+totaloggedout,totaloggedout);
JFreeChart chart=ChartFactory.createPieChart(标题、pieDataset、false、false、false);
returnnewchartpanel(chart){//这是管理将图表大小设置为面板的技巧!
公共维度getPreferredSize(){
返回新尺寸(宽度、高度);
}
};
}
}
我真的希望它能有帮助 可能是content.pack()?“我知道这个问题早些时候被问过,但提供的解决方案不起作用。”在哪里?链接请…到底是什么问题?调整内容大小时是否希望chartPanel自动调整大小?@andreThompson已完成编辑。是否可以指定?我使用了两个JPanel,它们的布局都改成了borderlayout。我不知道为什么这样不行;你能编辑你的问题来显示你的答案吗
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);
}
};
}
}