2013-03-17 77 views
1
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.addMouseListener(this)。我不知道我該錯在哪裏,或者我應該在哪裏添加一些東西。我有意向Jbutton添加mouseListener,因爲在我的原始程序中,我使用JButton中的值來調用圖中的更改。我有一個條形圖,我想更新,我試圖重新驗證和重繪方法,但沒有成功

+0

哪裏了'ChartPanel'的代碼?你如何重寫它的'paintComponent'方法?並作爲附註。 'Window'本身就是'java.awt'包中的一個類。使用這個類的其他名稱。 – 2013-03-17 11:48:02

+0

我對jfree不太瞭解...還是個初學者...我想我已經初始化了chartPanel,並且我一直謹慎的不導入awt包但是我導入了awt.event包 – 2013-03-17 11:53:47

+0

@VishalK:'ChartPanel'是一個類在[tag:jfreechart]庫中定義;我修改了下面的例子。 +1 [sscce](http://sscce.org/)。 – trashgod 2013-03-17 12:39:35

回答

4

你的方法在時尚之後起作用。更好的解決方案是更新圖表的模型gData,並讓圖表自動更新。此外,

  • 請勿在JButton;上使用鼠標偵聽器;只需處理ActionEvent

  • 使用Java命名約定。

  • A JOptionPane可以有多個輸入字段,如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(); 
    } 
} 
相關問題