调整JFrame大小后更新JFreeChart / JPanel
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了调整JFrame大小后更新JFreeChart / JPanel相关的知识,希望对你有一定的参考价值。
我正在尝试使用JFileChooser来选择一个excel文件,然后由JFreeChart显示。我首先在构造函数中选择带有classLoader的默认文件,然后使用JFileChooser更新我的图形。问题是当我选择一个文件时,我显示图形的面板不会更新,除非我调整框架大小。
public class MyFrame extends JFrame
{
private static final long serialVersionUID = 1L;
MyFrame() throws BiffException, IOException
{
this.setSize(1500,1000);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
graphpanel = new Graph();
menu = new MenuBar(graphPanel);
this.add(graphPanel);
this.add(menu, BorderLayout.PAGE_START);
}
public static void main(String[] args) throws BiffException, IOException
{
MyFrame idFrame = new MyFrame();
idFrame.setVisible(true);
}
Graph graphPanel;
MenuBar menu;
}
public class Graph extends JPanel
{
Graph() throws BiffException, IOException
{
ClassLoader classLoader = getClass().getClassLoader();
file = new File(classLoader.getResource("zeszyt.xls").getFile());
FileInputStream f = new FileInputStream(file);
Workbook w = Workbook.getWorkbook(f);
Sheet s = w.getSheet("Sheet1");
XYDataset dataset = createDataset(s);
JFreeChart chart = ChartFactory.createXYLineChart("title","x", "y",dataset,PlotOrientation.VERTICAL,true, true, false);
ChartPanel panel = new ChartPanel(chart);
this.add(panel);
this.setLayout(new GridLayout(1,1));
}
private XYDataset createDataset(Sheet s)
{
XYSeriesCollection dataset = new XYSeriesCollection();
XYSeries series = new XYSeries("y = x^2");
for(int i=1; i<39; i++)
{
series.add(Double.parseDouble(s.getCell(0,i).getContents()),Double.parseDouble(s.getCell(1,i).getContents()));
}
dataset.addSeries(series);
return dataset;
}
public void setFile(File file) throws BiffException, IOException
{
this.file = file;
FileInputStream f = new FileInputStream(file);
Workbook w = Workbook.getWorkbook(f);
Sheet s = w.getSheet("Sheet2");
XYDataset dataset = createDataset(s);
JFreeChart chart = ChartFactory.createXYLineChart("title here","x", "y",dataset,PlotOrientation.VERTICAL,true, true, false);
ChartPanel panel = new ChartPanel(chart);
this.removeAll();
this.add(panel);
this.setLayout(new GridLayout(1,1));
this.setBackground(Color.RED);
}
File file;
}
public class MenuBar extends JMenuBar
{
public MenuBar(Graph graphPanel)
{
menu1 = new JMenu("Plik");
JMenuItem menu1Item = new JMenuItem("Open file...");
this.add(menu1);
menu1.add(menu1Item);
menu1Item.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
int returnValue = jfc.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION)
{
file = jfc.getSelectedFile();
try {
graphPanel.setFile(file);
} catch (BiffException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
JMenu menu1;
JMenuItem menu1Item;
JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
File file;
}
答案
除非我调整框架大小,否则显示图形的面板不会更新。
创建时组件的默认大小为(0,0),因此无需绘制任何内容。在添加所有组件之后,您需要调用面板的布局管理器)以为每个组件提供大小/位置。
因此,在向可见GUI添加组件时,基本代码为:
panel.add(...);
panel.revalidate(); // invoke the layout manager
panel.repaint(); // make sure panel is repainted.
以上是关于调整JFrame大小后更新JFreeChart / JPanel的主要内容,如果未能解决你的问题,请参考以下文章