gpt4 book ai didi

java - 为什么条形图不执行?

转载 作者:太空宇宙 更新时间:2023-11-04 06:24:39 25 4
gpt4 key购买 nike

我不太确定我哪里出了问题。我正在创建一个程序,获取 4 个人当月的销售数据并将其放在条形图上。我在 public void windowClosing 的 maint 字符串中收到一个 @override 请求。当我编译时它说

Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
at java.awt.Container.checkNotAWindow(Container.java:488)
at java.awt.Container.addImpl(Container.java:1089)
at java.awt.Container.add(Container.java:415)
at BarGraph.main(BarGraph.java:115)
Java Result: 1

感谢任何帮助。

import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class BarGraph extends JFrame

{
private final double[] sales;
private final String[] salesAssociate;
private final String title;

public BarGraph(double[] sale, String[] saleAssoc, String t)
{
sales = sale;
salesAssociate = saleAssoc;
title = t;
}

public void paintComponent(Graphics graphics)
{
super.paintComponents(graphics);
if (sales == null || sales.length == 0)
return;
double minSales = 0;
double maxSales = 0;
for (int i = 0; i < sales.length; i++)
{
if (minSales > sales[i])
minSales = sales[i];
if (maxSales < sales[i])
maxSales = sales[i];
}
Dimension dim = getSize();
int clientWidth = dim.width;
int clientHeight = dim.height;
int barWidth = clientWidth / sales.length;
Font titleFont = new Font("Book Antiqua", Font.BOLD, 15);
FontMetrics titleFontMetrics = graphics.getFontMetrics(titleFont);
Font labelFont = new Font("Book Antiqua", Font.PLAIN, 10);
FontMetrics labelFontMetrics = graphics.getFontMetrics(labelFont);
int titleWidth = titleFontMetrics.stringWidth(title);
int q = titleFontMetrics.getAscent();
int p = (clientWidth - titleWidth) / 2;
graphics.setFont(titleFont);
graphics.drawString(title, p, q);
int top = titleFontMetrics.getHeight();
int bottom = labelFontMetrics.getHeight();
if (maxSales == minSales)
return;
double scale = (clientHeight - top - bottom) / (maxSales - minSales);
q = clientHeight - labelFontMetrics.getDescent();
graphics.setFont(labelFont);
for (int j = 0; j < sales.length; j++)
{
int salesP = j * barWidth + 1;
int salesQ = top;
int height = (int) ((sales[j]) * scale);
if (sales[j] >= 0)
salesQ += (int) (sales[j] * scale);
else
{
salesQ += (int) (maxSales * scale);
height = -height;
}


graphics.setColor(Color.BLUE);
graphics.fill3DRect(salesP, salesQ, barWidth - 2, height, true);
graphics.setColor(Color.black);
graphics.draw3DRect(salesP, salesQ, barWidth - 2, height, true);
int labelWidth = labelFontMetrics.stringWidth(salesAssociate[j]);
p = j * barWidth + (barWidth - labelWidth) / 2;
graphics.drawString(salesAssociate[j], p, q);

}
}

public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(350, 300);
double[] sales = new double[4];
String[] salesAssociate = new String[4];
double carsSold1;
double carsSold2;
double carsSold3;
double carsSold4;



Scanner input = new Scanner(System.in);
System.out.println("How many cars did Pam sell for the month?");
carsSold1 = input.nextInt();
System.out.println("How many cars did Leo sell for the month?");
carsSold2 = input.nextInt();
System.out.println("How many cars did Kim sell for the month?");
carsSold3 = input.nextInt();
System.out.println("How many cars did Bob sell for the month?");
carsSold4 = input.nextInt();

sales[0] = carsSold1;
salesAssociate[0] = "Pam";

sales[1] = carsSold2;
salesAssociate[1] = "Leo";

sales[2] = carsSold3;
salesAssociate[2] = "Kim";

sales[3] = carsSold4;
salesAssociate[3] = "Bob";

frame.getContentPane().add(new BarGraph(sales, salesAssociate, "Friendly Hal's Auto"));
WindowListener winListener = new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
System.exit(0);
}
};
frame.addWindowListener(winListener);
frame.setVisible(true);
}

}

最佳答案

改变

public class BarGraph extends JFrame

public class BarGraph extends JPanel

按照您的方式,BarGraph 是一个 JFrame,并且您无法将 JFrame 添加到 JFrame

关于java - 为什么条形图不执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26961617/

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