我有一个要更新的条形图,我尝试了revalidate和repaint方法,但没有成功 [英] I have a bar chart which I want to update and I tried the revalidate and repaint methods but with no success

查看:78
本文介绍了我有一个要更新的条形图,我尝试了revalidate和repaint方法,但没有成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class GraphGenerator1 extends JPanel {
    ChartPanel chartPanel, sbc;

    void generator(int t, int Value1, int Value2) {
        if (t == 1) {
            DefaultCategoryDataset gData = new DefaultCategoryDataset();
            gData.setValue(Value1, "What you saved", "");
            gData.setValue(Value2, "What you paid", "");

            JFreeChart chart = ChartFactory.createBarChart("", "", "", gData,
                    PlotOrientation.VERTICAL, false, false, false);
            chart.setBackgroundPaint(Color.WHITE);
            BarRenderer br = (BarRenderer) chart.getCategoryPlot()
                    .getRenderer();
            br.setBarPainter(new StandardBarPainter());
            br.setSeriesPaint(0, Color.decode("#97d95c"));
            br.setSeriesPaint(1, Color.decode("#437346"));
            chart.getCategoryPlot().setBackgroundPaint(new Color(0, 0, 0, 0));
            br.setMaximumBarWidth(0.25);
            chart.getCategoryPlot().setDomainGridlinesVisible(false);
            chart.getCategoryPlot().setRangeGridlinesVisible(false);
            chart.getCategoryPlot().getDomainAxis().setTickLabelsVisible(false);
            // chart.getCategoryPlot().clearDomainMarkers();
            chart.getCategoryPlot().getRangeAxis().setAxisLineVisible(false);
            chart.getCategoryPlot().getRangeAxis().setTickMarksVisible(false);
            chartPanel = new ChartPanel(chart);
            this.setLayout(new BorderLayout());
            this.add(chartPanel, BorderLayout.CENTER);
            this.setOpaque(true);
        }
    }
}

class Window extends JFrame implements MouseListener {
    GraphGenerator1 x;
    JButton j;

    Window() {
        x = new GraphGenerator1();
        x.generator(1, 56, 20);
        j = new JButton("CLICK ME");

        this.setLayout(new BorderLayout());
        this.add(x, BorderLayout.CENTER);
        this.add(j, BorderLayout.SOUTH);

        j.addMouseListener(this);

        this.setVisible(true);
        this.setExtendedState(MAXIMIZED_BOTH);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
        String a = (String) JOptionPane.showInputDialog(rootPane,
                "ENTER FIRST VALUE", "", JOptionPane.PLAIN_MESSAGE);
        String b = (String) JOptionPane.showInputDialog(rootPane,
                "ENTER SECOND VALUE", "", JOptionPane.PLAIN_MESSAGE);

        int aa = Integer.parseInt(a);
        int bb = Integer.parseInt(b);

        x.generator(1, aa, bb);

        x.chartPanel.revalidate();
        x.chartPanel.repaint();

        // I DONT KNOW IT DOESNT UPDATE//

    }

    public static void main(String args[]) {
        new Window();
    }
}

我有一个要更新的条形图,我尝试了revalidate和repaint方法,但没有成功.我什至还添加了chartPanel.addMouseListener(this).我不知道我要去哪里错了,或者我应该在哪里添加一些东西.我故意将mouseListener添加到Jbutton,因为在我的原始程序中,我使用JButton中的值来调用图形中的更改.

I have a bar chart which I want to update and I tried the revalidate and repaint methods but with no success. I have even added chartPanel.addMouseListener(this). I dont know where am I going wrong or where I should be adding something. I have intentionally added mouseListener to Jbutton because in my original program I am using values in a JButton to invoke change in graphs.

推荐答案

您的方法遵循某种风格.更好的解决方案是更新图表的模型gData,并让图表自行更新.还有,

Your approach works after a fashion. A better solution is to update the chart's model, gData, and let the chart update itself. Also,

  • 不要在JButton上使用鼠标侦听器;只需处理ActionEvent.

  • Don't use a mouse listener on a JButton; just handle the ActionEvent.

使用Java命名约定.

Use Java naming conventions.

A JOptionPane可以具有多个输入字段,如此处所示.

A JOptionPane can have multiple inputs fields, as shown here.

SSCCE:

public class GraphFrame extends JFrame {

    GraphFrame() {
        final GraphPanel gPanel = new GraphPanel();
        gPanel.create();
        JButton button = new JButton(new AbstractAction("Update") {
            @Override
            public void actionPerformed(ActionEvent e) {
                String a = JOptionPane.showInputDialog(rootPane,
                    "ENTER FIRST VALUE", "", JOptionPane.PLAIN_MESSAGE);
                String b = JOptionPane.showInputDialog(rootPane,
                    "ENTER SECOND VALUE", "", JOptionPane.PLAIN_MESSAGE);
                int aa = Integer.parseInt(a);
                int bb = Integer.parseInt(b);
                gPanel.update(aa, bb);
            }
        });
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.add(gPanel, BorderLayout.CENTER);
        this.add(button, BorderLayout.SOUTH);
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    private static class GraphPanel extends JPanel {

        private DefaultCategoryDataset gData = new DefaultCategoryDataset();

        void create() {
            update(56, 20);
            JFreeChart chart = ChartFactory.createBarChart("", "", "", gData,
                PlotOrientation.VERTICAL, false, false, false);
            ChartPanel chartPanel = new ChartPanel(chart);
            this.add(chartPanel);
        }

        private void update(int value1, int value2) {
            gData.clear();
            gData.setValue(value1, "What you saved", "");
            gData.setValue(value2, "What you paid", "");
        }
    }

    public static void main(String args[]) {
        new GraphFrame();
    }
}

这篇关于我有一个要更新的条形图,我尝试了revalidate和repaint方法,但没有成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆