Java 我有一个条形图,我想更新,我尝试了重新验证和重新绘制的方法,但没有成功
我有一个条形图,我想更新,我尝试了重新验证和重新绘制的方法,但没有成功。我甚至还添加了chartPanel.AddMouseListener这个。我不知道我哪里做错了,或者我应该在哪里添加一些东西。我有意将mouseListener添加到Jbutton中,因为在我的原始程序中,我使用Jbutton中的值来调用图形中的更改。您的方法在某种程度上起作用。更好的解决方案是更新图表的模型gData,并让图表自行更新。而且 不要在JButton上使用鼠标侦听器;只需处理ActionEvent 使用Java命名约定 JOptionPane可以有多个输入字段,如图所示 SSCCE:Java 我有一个条形图,我想更新,我尝试了重新验证和重新绘制的方法,但没有成功,java,swing,refresh,jfreechart,bar-chart,Java,Swing,Refresh,Jfreechart,Bar Chart,我有一个条形图,我想更新,我尝试了重新验证和重新绘制的方法,但没有成功。我甚至还添加了chartPanel.AddMouseListener这个。我不知道我哪里做错了,或者我应该在哪里添加一些东西。我有意将mouseListener添加到Jbutton中,因为在我的原始程序中,我使用Jbutton中的值来调用图形中的更改。您的方法在某种程度上起作用。更好的解决方案是更新图表的模型gData,并让图表自行更新。而且 不要在JButton上使用鼠标侦听器;只需处理ActionEvent 使用Java
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();
}
}
你的方法很有效。更好的解决方案是更新图表的模型gData,并让图表自行更新。而且 不要在JButton上使用鼠标侦听器;只需处理ActionEvent 使用Java命名约定 JOptionPane可以有多个输入字段,如图所示 SSCCE:
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();
}
}
ChartPanel的代码在哪里?您是如何重写其paintComponent方法的?作为旁注。Window本身是java.awt包中的一个类。。为这个类使用其他名称。我对jfree不太了解…还是一个初学者…我想我已经初始化了chartPanel,并且我对不导入awt包持谨慎态度,但我已经导入了awt.eventpackage@VishalK:ChartPanel是在库中定义的类;我已经修改了下面的例子+1.@trashgood:谢谢你提供了这些有价值的信息..ChartPanel的代码在哪里?您是如何重写其paintComponent方法的?作为旁注。Window本身是java.awt包中的一个类。。为这个类使用其他名称。我对jfree不太了解…还是一个初学者…我想我已经初始化了chartPanel,并且我对不导入awt包持谨慎态度,但我已经导入了awt.eventpackage@VishalK:ChartPanel是在库中定义的类;我已经修改了下面的例子+谢谢你的宝贵信息。。