【问题标题】:JFreeChart does not display anything with EclipseJFreeChart 在 Eclipse 中不显示任何内容
【发布时间】:2015-03-07 12:39:47
【问题描述】:

我是 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);
    }
}

【问题讨论】:

  • 避免使用null 布局,像素完美的布局是现代用户界面设计中的一种错觉。影响组件单个尺寸的因素太多,您无法控制。 Swing 旨在与核心布局管理器一起工作,丢弃这些将导致无穷无尽的问题和问题,您将花费越来越多的时间来尝试纠正

标签: java eclipse swing jfreechart


【解决方案1】:

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

【讨论】:

  • 我只是没有弹出窗口,所有其他程序也在我的身上执行,不知何故这没有......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-05
  • 1970-01-01
  • 2014-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多