jfree图表未加载

时间:2014-03-09 14:11:05

标签: java swing user-interface jfreechart

大家好

我有一个作业到期,我必须制作一个条形图。我在youtube上关注了一个教程。一切似乎都在编译,但我不知道为什么图形不会与我的GUI一起出现。这是代码:

public class NewjFrame extends javax.swing.JFrame {private JButton jButton1; 
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            NewJFrame inst = new NewJFrame();
            inst.setLocationRelativeTo(null);
            inst.setVisible(true);
        }
    });
}

public NewJFrame() {
    super();
    initGUI();
}

private void initGUI() {
    try {
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(null);
        {
            jButton1 = new JButton();
            getContentPane().add(jButton1);
            jButton1.setText("jButton1");
            jButton1.setBounds(90, 19, 151, 28);
        }
        pack();
        setSize(400, 300);
    } catch (Exception e) {
        //add your error handling code here
        e.printStackTrace();
    }
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
    // create a dataset...
    // Create a simple Bar chart
     DefaultCategoryDataset dataset = new DefaultCategoryDataset();
     dataset.setValue(6, "Profit", "Jane");
     dataset.setValue(7, "Profit", "Tom");
     dataset.setValue(8, "Profit", "Jill");
     dataset.setValue(5, "Profit", "John");
     dataset.setValue(12, "Profit", "Fred");
     JFreeChart chart = ChartFactory.createBarChart("Comparison between Salesman", 
     "Salesman", "Profit", dataset, PlotOrientation.VERTICAL, false, true, false);
     CategoryPlot p = chart.getCategoryPlot();
     p.setRangeGridlinePaint(Color.red);
     ChartFrame frame = new ChartFrame("Bar Chart", chart);
     frame.setVisible(true);
     frame.setSize(400,350);
}
}

PS我是java的新手,所以请放轻松我

1 个答案:

答案 0 :(得分:1)

您需要将ActionListener添加到jButton1

jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

您可以在实例化initGui()之后在jButton1内添加。