输入文字,按下按钮,为什么饼图不显示?
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);
}
}