2014-11-21 24 views
0

我正在创建一个程序,要求用户在一个月内完成销售,并将销售执行到条形图。程序运行并正确生成条形图;但是文本没有正确执行。我试图将标题“月销售额”作为图表标题,用销售助理名称标记栏,并在图表左侧标注栏的值。任何建议都会有所帮助。它在整个图表上反复产生“月度销售”。试图在图表中标注条形,但多次生成文本

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


    } 
+2

你不希望创建的方法的paintComponent内部组件。这是有点疯狂的一面,因为你不控制什么时候或者如果这个方法被调用,你正在放慢一个方法,必须是快速的。只要在那儿画画,然后**没什么别的。 – 2014-11-21 02:53:57

回答

1

你的问题是,你已经打破油漆链...

public void paintComponent(Graphics g) 
{ 
    // Your problem is right here... 

    Dimension d = getSize(); 

一个paintComponent的工作是清除Graphics上下文(与组件背景色)准备好画...

作为第一个呼叫添加super.paintComponent(g);paintComponent方法,你做的任何自定义涂装前

A Graphics上下文是共享资源,在指定的绘制周期内绘制的所有组件都将共享相同的上下文,并且在某些系统上,对于所有的绘制周期它们都是相同的,因此您需要确保在绘制之前将其清除使用它...

有关更多详细信息,请参见Painting in AWT and SwingPerforming Custom Painting

更新

正如指出的@HovercraftFullOfEels,你是一个paintComponent中创建UI元素,绘画应该绘制UI的当前状态,并且不应该做任何修改。绘画是在重绘经理认为应该完成时完成的,因此根据你在做什么,你的paintComponent可能会被调用多次。

paintComponent内修改UI的状态可以设置重绘请求将最终消耗你的CPU周期,使您的PC无法使用的无限循环......

可能还需要采取通过阅读Initial Threads并确保您的UI仅在事件分派线程的上下文中创建和修改。

你也应该不会被混合UI和基于控制台的方式,他们不一起拌匀...

+0

他也在创建和放置JLabels。 – 2014-11-21 02:54:57

+0

@HovercraftFullOfEels 没有看到:P – MadProgrammer 2014-11-21 02:55:51

+0

我是否仅为标签创建新方法 – Soccer82 2014-11-21 03:02:17