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


    }

}
4

2 回答 2

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

并且可以看到饼图。

于 2012-07-18T21:16:26.137 回答
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 更改为可调整大小。这是在测试期间触发重绘的一种简单方法。通过这些更改,当我单击按钮时,我什么也看不到,但如果我调整窗口大小,我会看到红色框。

于 2012-07-18T22:31:31.553 回答