Java JFreeChart/JPanel在调整JFrame大小后更新
我试图使用JFileChooser选择一个excel文件,然后由JFreeChart显示。我首先使用构造函数中的classLoader选择默认文件,然后使用JFileChooser更新我的图形。问题是,当我选择一个文件时,除非调整帧的大小,否则显示图形的面板不会更新Java JFreeChart/JPanel在调整JFrame大小后更新,java,swing,awt,jfreechart,jfilechooser,Java,Swing,Awt,Jfreechart,Jfilechooser,我试图使用JFileChooser选择一个excel文件,然后由JFreeChart显示。我首先使用构造函数中的classLoader选择默认文件,然后使用JFileChooser更新我的图形。问题是,当我选择一个文件时,除非调整帧的大小,否则显示图形的面板不会更新 public class MyFrame extends JFrame { private static final long serialVersionUID = 1L; MyFrame() throws B
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 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;
}
组件在创建时的默认大小为(0,0),因此无需绘制任何内容。您需要在添加所有组件后调用面板的布局管理器)为每个组件指定尺寸/位置
因此,将组件添加到可见GUI时,基本代码是:
panel.add(...);
panel.revalidate(); // invoke the layout manager
panel.repaint(); // make sure panel is repainted.
什么意思?我从不向我的面板添加任何内容。在程序开始时加载的defualt图工作正常。@NotStudent,我从不向面板添加任何内容。你认为
this.removeAll()
和this.add(…)
语句在做什么?您正在向面板中添加一个新创建的图表面板。哦,是的,我完全忘记了这一点。它起作用了。非常感谢你!还考虑在现场更新绘图,如在这些建议;重写getPreferredSize()
以建立图表的首选大小。
panel.add(...);
panel.revalidate(); // invoke the layout manager
panel.repaint(); // make sure panel is repainted.