gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 05:14:14 25 4
gpt4 key购买 nike

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

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


}

}

最佳答案

以下是需要考虑的一些事项:

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

并且可以看到饼图。

关于java - 为什么自定义饼图不显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11549315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com