JFreeChart不会显示Eclipse的任何内容

时间:2015-01-09 08:21:17

标签: java eclipse swing jfreechart

我是JFreeCharts的血腥初学者,然而,这是我的第一次到期,但我的程序没有显示任何内容,eclipse说它正在运行,但没有窗口或任何显示的内容。当然,我确保尝试适当的范围,例如.validate或.setVisible true ...

package at.htlklu.chart;

import java.awt.BorderLayout;

public class ChartsExperiments extends JFrame {

    private JPanel contentPane;
    private JTextField txt_1;
    private JTextField txt_2;
    private JTextField txt_3;
    private JTextField txt_4;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ChartsExperiments frame = new ChartsExperiments();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public ChartsExperiments() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 852, 719);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        final JPanel panel = new JPanel();
        panel.setBackground(Color.LIGHT_GRAY);
        panel.setBounds(6, 6, 840, 447);
        contentPane.add(panel);
        panel.setLayout(new BorderLayout(0, 0));

        JLabel lblStat1 = new JLabel("statistic_1");
        lblStat1.setBounds(6, 465, 75, 16);
        contentPane.add(lblStat1);

        JLabel lblStat2 = new JLabel("statistic_2");
        lblStat2.setBounds(93, 465, 75, 16);
        contentPane.add(lblStat2);

        JLabel lblStat3 = new JLabel("statistic_3");
        lblStat3.setBounds(180, 465, 75, 16);
        contentPane.add(lblStat3);

        JLabel lblStat4 = new JLabel("statistic_4");
        lblStat4.setBounds(267, 465, 75, 16);
        contentPane.add(lblStat4);

        JButton btnView = new JButton("View");
        btnView.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int s1 = Integer.parseInt(txt_1.getText());
                int s2 = Integer.parseInt(txt_2.getText());
                int s3 = Integer.parseInt(txt_3.getText());
                int s4 = Integer.parseInt(txt_4.getText());

                DefaultCategoryDataset dataset = new DefaultCategoryDataset();

                dataset.setValue(s1, "", "Statistic_1");
                dataset.setValue(s2, "", "Statistic_2");
                dataset.setValue(s3, "", "Statistic_3");
                dataset.setValue(s4, "", "Statistic_4");

                JFreeChart chart = ChartFactory.createBarChart("Statistics", "", "", dataset, PlotOrientation.HORIZONTAL, false, false, false);
                CategoryPlot catPlot = chart.getCategoryPlot();
                catPlot.setRangeGridlinePaint(Color.BLACK);

                ChartPanel chartPanel = new ChartPanel(chart);
                chartPanel.setVisible(true);
                panel.removeAll();
                panel.setVisible(true);
                panel.add(chartPanel, BorderLayout.CENTER);
                panel.validate();


            }
        });
        btnView.setBounds(389, 489, 117, 29);
        contentPane.add(btnView);

        txt_1 = new JTextField();
        txt_1.setBounds(6, 488, 75, 28);
        contentPane.add(txt_1);
        txt_1.setColumns(10);

        txt_2 = new JTextField();
        txt_2.setColumns(10);
        txt_2.setBounds(93, 488, 75, 28);
        contentPane.add(txt_2);

        txt_3 = new JTextField();
        txt_3.setColumns(10);
        txt_3.setBounds(180, 488, 75, 28);
        contentPane.add(txt_3);

        txt_4 = new JTextField();
        txt_4.setColumns(10);
        txt_4.setBounds(267, 488, 75, 28);
        contentPane.add(txt_4);
    }
}

1 个答案:

答案 0 :(得分:1)

你确定吗?我复制并尝试了你的程序,它的执行情况非常好。

enter image description here