为什么自定义饼图不显示?

时间:2012-07-18 20:02:02

标签: java swing

输入文字,按钮,为什么饼图不显示?

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);


    }

}

2 个答案:

答案 0 :(得分: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();
}

可以看到Piechart。

答案 1 :(得分: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更改为可调整大小。在测试期间,这是一种简单的方法来触发重绘。通过这些更改,当我点击按钮时,我什么都没看到,但如果我调整窗口大小,我会看到红色框。