简体   繁体   English

在JPanel上创建条形图

[英]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. 我对Java很陌生,我似乎无法JPanel为什么它无法打开JPanel或矩形或字符串。 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. 在这一点上,您不必了解第二个主要方法代码的详细信息,但可以确保它确保在Swing主线程上调用了Swing GUI。 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. 其他问题-您将要尝试避免使用幻数,硬编码数字,而应使用变量,这将使您可以更轻松地更改钢筋的高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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