2012-07-18 65 views
1

輸入文本,按鈕,爲什麼餅圖不顯示?爲什麼自定義餅圖不顯示?

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.ArrayList; 
import javax.swing.*; 
import javax.swing.border.TitledBorder; 

public class PieChart extends JFrame { 

    private JTextField jtfParticipation = new JTextField(); 
    private JTextField jtfProjects = new JTextField(); 
    private JTextField jtfQuizzes = new JTextField(); 
    private JTextField jtfFinalExam = new JTextField(); 
    private JButton jbtCreatePieChart = new JButton("Create Pie Chart"); 

    public PieChart() { 

     // Text panel 

     JPanel panel1 = new JPanel(new GridLayout(8, 0)); 

     panel1.setBorder(new TitledBorder("Input percentages:")); 



     // A font to change from the default Plain font to Arial font 

     Font arialFont = new Font("Arial", Font.PLAIN, 12); 

     JLabel jlblParticipation = new JLabel("Participation %"); 

     JLabel jlblProjects = new JLabel("Projects %"); 

     JLabel jlblQuizzes = new JLabel("Quizzes %"); 

     JLabel jlblFinalExam = new JLabel("Final Exam %"); 



     // The labels use the new font 

     jlblParticipation.setFont(arialFont); 

     jlblProjects.setFont(arialFont); 

     jlblQuizzes.setFont(arialFont); 

     jlblFinalExam.setFont(arialFont); 



     // Adds the objects to the panel 

     panel1.add(jlblParticipation); 

     panel1.add(jtfParticipation); 

     panel1.add(jlblProjects); 

     panel1.add(jtfProjects); 

     panel1.add(jlblQuizzes); 

     panel1.add(jtfQuizzes); 

     panel1.add(jlblFinalExam); 

     panel1.add(jtfFinalExam); 



     // Assigns the text panel and the button to one panel 

     JPanel panel2 = new JPanel(new BorderLayout()); 

     panel2.add(panel1, BorderLayout.CENTER); 

     panel2.add(jbtCreatePieChart, BorderLayout.SOUTH); 


     add(panel2, BorderLayout.WEST); 



     jbtCreatePieChart.addActionListener(new ButtonListener()); 

    } 



    class ButtonListener implements ActionListener { 



     @Override 

     public void actionPerformed(ActionEvent e) { 



      // Set the size and trigger a repaint 

      final PieChartGraphic pie = new PieChartGraphic(); 

      add(pie, BorderLayout.CENTER); 

      pie.setPreferredSize(new Dimension()); 

      pie.repaint(); 

     } 

    } 



    class PieChartGraphic extends JPanel { 



     @Override 
     protected void paintComponent(Graphics slice) { 



      super.paintComponent(slice); 



      int xCenter = getWidth()/2; 

      int yCenter = getHeight()/2; 

      int radius = (int) (Math.min(getWidth(), getHeight()) * 0.4); 

      int x = xCenter - radius; 
      int y = yCenter - radius; 



      double inputIsDouble; 

      int inputIsInt; 

      int availablePercentage = 100; 

      int currentAngle = 0; 



      ArrayList<JTextField> jtfs = new ArrayList<>(); 

      jtfs.add(jtfProjects); 

      jtfs.add(jtfParticipation); 

      jtfs.add(jtfQuizzes); 

      jtfs.add(jtfFinalExam); 



      ArrayList<Color> color = new ArrayList<>(); 

      color.add(Color.RED); 

      color.add(Color.GREEN); 

      color.add(Color.BLUE); 

      color.add(Color.WHITE); 



      for (int i = 0; i < jtfs.size(); i++) { 

       inputIsDouble = userInput(jtfs.get(i).getText(), availablePercentage); 

       inputIsInt = (int) (inputIsDouble * 3.6); 

       // Sets the color of the filled 

       slice.setColor(color.get(i)); 

       // Sets the start point and size of the angle 

       slice.fillArc(x, y, 2 * radius, 2 * radius, currentAngle, inputIsInt); 

       currentAngle += inputIsInt; 

       availablePercentage -= inputIsDouble; 

      } 



      // Places the text strings 

      slice.setColor(Color.BLACK); 

      slice.drawString("Participation - " + 

       "\jtfParticipation.getText() + "%", 1/4 * x, 1/4 * y); 

      slice.drawString("Projects - " + jtfProjects.getText() + "%", 3/4 * x, 1/4 * y); 

      slice.drawString("Quizzes -- " + jtfQuizzes.getText() + "%", 1/4 * x, 3/4 * y); 

      slice.drawString("Final - " + jtfFinalExam.getText() + "%", 3/4 * x, 3/4 * y); 

     } 

    } 



    public static double userInput(String inputIsString, int availablePercentage) { 

     return new Double(inputIsString).doubleValue(); 

    } 



    public static void main(String[] args) { 

     PieChart frame = new PieChart(); 

     frame.setTitle("CMIS Pie Chart"); 

     frame.setSize(334, 215); 

     frame.setLocationRelativeTo(null); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     frame.setResizable(false); 

     frame.setVisible(true); 


    } 

} 
+0

良好的SSCCE爲您的第一篇文章。 – wolfcastle 2012-07-18 22:32:05

回答

0

這裏有一些事情要考慮:

創造·Panel3中後,該組件的大小不會被置:

panel3.setSize(200, 200); 
PieChartGraphic pieChartGraphic = new PieChartGraphic(); 
pieChartGraphic.setSize(200, 200); 
panel3.add(pieChartGraphic); 

加入jpanel3後,你應該叫

jpanel3.repaint(); 

繪製添加組件的圖形。

用戶輸入和驗證發生在paintComponent調用的中途。這項工作應你做任何噴漆前立即

我用:

public static double userInput(String inputIsString, int availablePercentage) { 
    return new Double(inputIsString).doubleValue(); 
} 

,可以看到餅圖。

+0

所以你說你改變了以前的方法到上面? 另外我不確定我是否理解你是如何說明它需要實施的。目前,我已經將textfield中的文本改爲double,然後是一個整數,並拋出異常,以防止用戶輸入除double之外的任何內容。至少這是這個想法。 感謝您的建議到目前爲止! – 2012-07-18 22:05:01

+0

是的,我改變它到上面只是爲了讓它工作。 – Reimeus 2012-07-18 22:14:33

+0

儘管如此,它仍然不顯示。它只是顯示一個空白框,並沒有實際顯示餅圖。 – 2012-07-18 22:17:38

0

我想你在這裏有幾個問題。

首先是尺寸/重繪問題@Reimeus指出。您需要設置餅圖的大小,然後重新繪製它。

接下來,用每個按鈕點擊添加一個新的PieChartPanel似乎是一種奇怪的做事方式。每次用戶按下按鈕時,您最終都會創建剛疊加在一起的新面板。爲什麼不只是將餅圖面板永久添加,並且根據狀態更新其上顯示​​的內容?

我改變了下面幾段代碼。首先,我更改了動作偵聽器以刪除額外面板並在面板上設置大小。我還添加了重繪,儘管這似乎並不重要:

class ButtonListener implements ActionListener { 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     // Set the size and trigger a repaint 
     final PieChartGraphic pie = new PieChartGraphic(); 
     add(pie, BorderLayout.EAST); 
     pie.setPreferredSize(new Dimension(300, 300)); 
     pie.repaint(); 
    } 
} 

我再簡化的paintComponent方法來刪除所有代碼:

protected void paintComponent(Graphics slice) { 
     super.paintComponent(slice); 

     slice.setColor(Color.RED); 
     slice.fillRect(0, 0, getWidth(), getHeight()); 
    } 

最後,我改變了JFrame的是調整大小。在測試過程中,這是一種簡單的方法來觸發重繪。隨着這些變化,當我點擊按鈕時,我什麼都看不到,但如果我調整窗口大小,我會看到紅色框。

+0

我的意圖是添加在用戶輸入每個文本字段的值並使用按鈕後創建的餅圖。 我將編輯我之前的代碼,並執行我的更改。 – 2012-07-18 22:36:25

+0

謝謝。我按照你的建議做了,但沒有改變原來的PieChartGraphic類。它確實創建了具有指定大小的餅圖,但是如您所寫,它需要調整大小。任何想法如何解決這個問題,讓它顯示而不會強制用戶調整窗口的大小? – 2012-07-18 22:58:23

+0

I雙重檢查fillarc方法有關弧度如你所說,它爲int角度: 公共抽象無效fillArc(INT的x, INT Y, INT寬度, INT高度, INT由startAngle, INT arcAngle) – 2012-07-18 23:15:29