简体   繁体   English

调整 JFrame 大小后更新 JFreeChart/JPanel

[英]JFreeChart/JPanel updating after resizing the JFrame

I'm trying to use JFileChooser to choose an excel file which is then displayed by JFreeChart.我正在尝试使用 JFileChooser 选择一个 Excel 文件,然后由 JFreeChart 显示该文件。 I start by choosing the default file with classLoader in constructor and then using JFileChooser to update my graph.我首先在构造函数中使用 classLoader 选择默认文件,然后使用 JFileChooser 更新我的图形。 The problem is that when I choose a file, the panel in which I display the graph doesn't update unless I resize my frame.问题是,当我选择一个文件时,除非我调整框架的大小,否则显示图形的面板不会更新。


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;

}


the panel in which I display the graph doesn't update unless I resize my frame.除非我调整框架的大小,否则我显示图形的面板不会更新。

Components have a default size of (0, 0) when created so there is nothing to paint.组件在创建时的默认大小为 (0, 0),因此无需绘制任何内容。 You need to invoke the layout manager of the panel )after all components have been added) to give each component a size/location.您需要在添加所有组件后调用面板的布局管理器)来为每个组件指定大小/位置。

So, when adding components to a visible GUI the basic code is:因此,当向可见 GUI 添加组件时,基本代码是:

panel.add(...);
panel.revalidate(); // invoke the layout manager
panel.repaint(); // make sure panel is repainted.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM