- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个程序,请求用户一个月的销售额,并将销售额执行为条形图。程序运行并正确生成条形图;但是文本没有正确执行。我尝试将标题“每月销售额”作为图表标题,用销售助理名称标记条形,并在图表左侧标记条形的值。任何建议都会有帮助。它在整个图表中重复生成“每月销售额”。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class BarGraph extends JPanel
{
//set variables for graph
JLabel jLabel1, jLabel2, jLabel3, jLabel4, jLabel5;
private Map<Color, Integer> bars =
new LinkedHashMap <>();
//execute bars and color
public void addBar(Color color, int value)
{
bars.put(color, value);
repaint();
}
//create paint components of chaart
public void paintComponent(Graphics g)
{
Dimension d = getSize();
int clientWidth = d.width;
int clientHeight = d.height;
int max = Integer.MIN_VALUE;
for (Integer value : bars.values())
{
max = Math.max(max, value);
}
jLabel1 = new JLabel("Sales For Month", JLabel.CENTER);
// We can position of the text, relative to the icon:
jLabel1.setVerticalTextPosition(JLabel.BOTTOM);
jLabel2 = new JLabel("PAM");
jLabel3 = new JLabel("Leo");
jLabel4 = new JLabel("Kim");
jLabel5 = new JLabel("BOB"); // Label of Icon Only
// Add labels to the Panel
add(jLabel1);
//paint bar
int width = (getWidth() / bars.size()) - 2;
int x = 1;
for (Color color : bars.keySet())
{
int value = bars.get(color);
int height = (int)
((getHeight() -5) * ((double)value /max));
g.setColor(color);
g.fillRect(x, getHeight() - height, width, height);
g.setColor(Color.black);
g.drawRect(x, getHeight() - height, width, height);
x += (width + 2);
}
}
//set bar size
public Dimension getPreferredSize()
{
return new Dimension(bars.size() * 10 + 2, 50);
}
//create main to generate charte
public static void main(String[] args)
{
JFrame frame = new JFrame("Friendly Hal's Auto");
BarGraph graph = new BarGraph();
//set frame size
frame.setSize(350, 300);
//create variable for user input
int carsSold1;
int carsSold2;
int carsSold3;
int carsSold4;
//request user input
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();
//color bar to user choice
graph.addBar (Color.red, carsSold1);
graph.addBar (Color.green, carsSold2);
graph.addBar(Color.blue, carsSold3);
graph.addBar(Color.yellow, carsSold4);
frame.getContentPane().add(graph);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
最佳答案
你的问题是,你破坏了油漆链......
public void paintComponent(Graphics g)
{
// Your problem is right here...
Dimension d = getSize();
paintComponent 的工作之一是清除 Graphics
上下文(带有组件背景色),为绘画做好准备...
在进行任何自定义绘制之前,添加 super.paintComponent(g);
作为对 paintComponent
方法的第一次调用
Graphics
上下文是共享资源,在给定绘制周期内绘制的所有组件都将共享相同的 Graphics
上下文,并且在某些系统上,它对于所有绘画周期,因此您需要确保在使用之前将其清除...
参见Painting in AWT and Swing和 Performing Custom Painting了解更多详情。
已更新
正如 @HovercraftFullOfEels 所指出的,您正在 paintComponent
中创建 UI 元素,绘画应该绘制 UI 的当前状态,并且不应对其进行任何修改。当重绘管理器认为应该完成时,绘画就会完成,因此您的 paintComponent
可能会被调用多次,具体取决于您在做什么。
从paintComponent
内部修改UI的状态可以设置重绘请求的无限循环,这最终将消耗您的CPU周期并使您的PC无法使用...
可能还想读一下Initial Threads并确保您的 UI 仅在事件调度线程的上下文中创建和修改。
您也不应该混合基于 UI 和控制台的方法,它们不能很好地混合在一起......
关于java - 尝试在图表中标记条形,但多次生成文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27053353/
有人知道如何在数组字段中插入以竖线/竖线分隔的数组吗? 逗号可以正常工作,但是当我将其更改为 bar 时,它会产生错误“格式错误的数组文字” 示例(有效): insert into table (ar
我尽力阅读与我的问题相关的问题。最相关的是:question .但是我无法弄清楚如何解决我的问题。我有一个像这样的数据集 structure(list(COMPANY = structure(1
在使用 matplotlib 挣扎了比我想承认的时间更长的时间之后,我试图在我使用过的几乎任何其他绘图库中做一些轻而易举的事情,我决定向 Stackiverse 寻求一些见解。简而言之,我需要的是创建
如何实现带有条形样式的 Activity 指示器。根据以下文档http://developer.android.com/design/building-blocks/progress.html “Ac
我正在尝试使用数组中的数据制作水平条形图 $values = implode(',', array_values($type)); $labels = implode('|', array_keys(
这个问题在这里已经有了答案: Two-tone background split by diagonal line using css (7 个答案) Create a slanted edge
我正在处理一个范围非常大的图表,我发现条形图偏离了轴。我怎样才能阻止这个?这是我在教程中编写的一个示例,因为我的数据是通过读取大量文件获取的。这说明了我遇到的问题。我给出了标准差,意味着一个很大的范围
我在 Swift 2.2 中创建了一个 UIProgressView(条形),现在想改变它的高度。其他一些帖子建议使用 CGAffineTransformScale 来更改高度。但是,出于某种原因,这
我想在条形图上绘制两组具有不同比例的值。例如,对于一周中的每一天,我都有一个比例(比如那天同事迟到的比例)和一个值(他们平均迟到多长时间)。 这两个值可以绘制在两个单独的图表上,但将它们放在同一个图表
我想制作一个具有隐藏/显示功能的组合(线/条)图。我遇到的问题是我不知道如何指定索引更改(对于 javascript 来说是新的)这是我的示例(取消选中前两个复选框之一并重新选中它以查看问题): ht
我安装了 YCM(你完成了我),当我编码时,我发现命令 $ 不工作。它不会跳到行尾,而是跳到最后一行。例如,a.cpp 中的一行: #include| 现在光标在 include 之后,然后我键
我想在条形图顶部显示自定义标签。 下面是我的 stacklabel 代码,这里的问题是我想在 stacklabel 的格式化程序中使用堆栈的类别名称,我该如何访问它。 stackLabels: {
我是一名优秀的程序员,十分优秀!