简体   繁体   中英

Creating a Bar graph on JPanel

Can anyone tell me what is wrong with this? I'm pretty new to Java and I can't seem to get why this won't open the JPanel or the rectangles, or the strings. Nothing is being shown but it does compile.

Code:

import java.awt.*;
import javax.swing.*;

public class Graphing extends JPanel
{

   public static void main (String[] args)
   {
   }

   public Graphing()
   {
      JFrame frame = new JFrame ("Nested Panels");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

      frame.getContentPane();
      frame.setPreferredSize(new Dimension(350,200));
      frame.pack();
      frame.setVisible(true);
   }

   public void paintComponent (Graphics page)
   {            
      super.paintComponent (page);          
      final int HEIGHT = 10;

      page.setColor (Color.yellow);
      page.fillRect (50, 50, (7*10), HEIGHT);//1-10
      page.fillRect (50, 64, (5*10), HEIGHT);//11-20
      page.fillRect (50, 78, (0*10), HEIGHT);//21-30
      page.fillRect (50, 92, (1*10), HEIGHT);//31-40
      page.fillRect (50, 106, (9*10), HEIGHT);//41-50
      page.fillRect (50, 120, (12*10), HEIGHT);//51-60
      page.fillRect (50, 134, (4*10), HEIGHT);//61-70
      page.fillRect (50, 148, (6*10), HEIGHT);//71-80
      page.fillRect (50, 162, (2*10), HEIGHT);//81-90
      page.fillRect (50, 176, (13*10), HEIGHT);//91-100

      page.drawString("1-10",10,60);
      page.drawString("11-20",10,74);
      page.drawString("21-30",10,88);
      page.drawString("31-40",10,102);
      page.drawString("41-50",10,116);
      page.drawString("51-60",10,130);
      page.drawString("61-70",10,144);
      page.drawString("71-80",10,158);
      page.drawString("81-90",10,172);
      page.drawString("91-100",10,186);

      page.drawString("7", 30, 60);
      page.drawString("5", 30, 74);
      page.drawString("0", 30, 88);
      page.drawString("1", 30, 102);
      page.drawString("9", 30, 116);
      page.drawString("12", 30, 130);
      page.drawString("4", 30, 144);
      page.drawString("6", 30, 158);
      page.drawString("2", 30, 172);
      page.drawString("13", 30, 186);
   }

}
public Graphing()   {
  JFrame frame = new JFrame ("Nested Panels");
  frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

  frame.getContentPane().add(this);

in main

public static void main(String[] args) {
    new Graphing();
} 

To be fully Kosher, the main method should actually be:

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
           new Graphing();
        }
    });
} 

While at this point, you don't have to know the details of this second main method code, but do know that it ensures that the Swing GUI is called on the main Swing thread. Doing this can help avoid unpredictable threading problems that are not likely to occur in your simple program, but can occur if your code gets a little more complex.

Other issues -- you'll want to try to avoid using magic numbers, hard-coded numbers, and instead use variables which will allow you to more easily change the heights of your bars.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM