【问题标题】:non-static variable this cannot be referenced from a static context in main method非静态变量 this 不能从 main 方法中的静态上下文中引用
【发布时间】:2013-04-05 15:24:29
【问题描述】:

我在此语句的主要方法中显示错误: // 非静态变量 this 不能被静态上下文引用

frame.getContentPane().add(new PieChart()); 

我认为这就像加载内容窗格并向其中添加 PieChart 类一样简单。我今天花了几个小时,希望能在这个问题上得到帮助。我有 10 周的 Java 经验,直到现在还没有超出我的深度。任何意见是极大的赞赏。

Here is my PieChart program:


package iapiechart;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JComponent;
import javax.swing.JFrame;

class IAPieChart{

    double arcValue;        // passes a value for the calculation of the arc.
    Color marker;            // holds value for color (expressed as an integer 

    public IAPieChart(double value, Color color){

        this.arcValue = value;
        this.marker = color;
    }


    public class PieChart extends JComponent { 

        IAPieChart[] pieValue = {new IAPieChart(5, Color.green),
                                new IAPieChart(33, Color.orange),
                                new IAPieChart(20, Color.blue),
                                new IAPieChart(15, Color.red)

        };

        public void paint(Graphics g) {

            drawPie((Graphics2D) g, getBounds(),  pieValue);

        }

        void drawPie(Graphics2D g, Rectangle area, IAPieChart[] pieValue){

            double sum = 0.0D;
            for (int i = 0; i < pieValue.length; i++) {

                sum += pieValue[i].arcValue;
            }

            double endPoint =  0.0D;
            int arcStart = 0; 
            for (int i = 0; i < pieValue.length; i++){

                endPoint = (int) (endPoint * 360 / sum);
                int radius = (int) (pieValue[i].arcValue * 360/ sum);
                g.setColor(pieValue[i].marker);
                g.fillArc(area.x, area.y, area.width, area.height, arcStart, radius);
                radius += pieValue[i].arcValue;
            }

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

        JFrame frame = new JFrame();
        frame.getContentPane().add(new PieChart()); // This is where the error occurs. 
        frame.setSize(500, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        }
 }

【问题讨论】:

  • 致那些试图将这个问题作为重复来结束的人:这个问题是关于使内部类静态,而不是方法静态,所以这不是重复。

标签: java swing main


【解决方案1】:

您正试图从静态方法main 实例化一个非静态的内部类PieChart -- 将其声明为静态。

public static class PieChart extends JComponent { 

如果您要保持PieChart 非静态,那么您将需要IAPieChart 的实例来创建PieChart 的实例,并且您在main 中没有IAPieChart 的实例.

【讨论】:

  • rgettman:我欠你一箱你最喜欢的饮料。我在这一问题上花了 9 个小时。仅仅感谢你是不够的。我会和其他人分享这个,我的朋友。
  • 再次感谢 Gettman。上周你们真的帮了大忙。这并不完整,但它确实满足了一些要求。感谢您的帮助,我对 JApplet 有了一定的基础和很好的理解。 apexsouthwest.com/PieChart
【解决方案2】:

由于您在 PieChart 类中创建了 IaPieChart,因此您的代码应该类似于:

public class PieChart extend JComponent
{

    static class IaPieChart(..)
    {
    }
}

那是您的 IaPieChart 类实际上是 PieChart 类的辅助类,因此它应该在该类中定义,而不是相反。

此外,自定义绘画是通过覆盖 paintComponent() 方法而不是 paint() 方法来完成的。

【讨论】:

    【解决方案3】:

    正如几个小时前讨论的那样,您无法从静态成员访问非静态内容。-

    "non static method cannot be referenced from a static context" JPA Java

    【讨论】:

      猜你喜欢
      • 2013-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-17
      • 2011-11-30
      相关资源
      最近更新 更多