无法获取NetBeans的访问" FigurePanel"在Java中 [英] Can't get NetBeans to access "FigurePanel" in Java

查看:328
本文介绍了无法获取NetBeans的访问" FigurePanel"在Java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者,当谈到Java的,我下面从一本书中的说明。我要创建具有以下code一FigurePanel:

I'm a beginner when it comes to Java and I'm following the instructions from a book. I am going to create a FigurePanel with the following code:

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

 public class TestFigurePanel extends JFrame{
    public TestFigurePanel() {
         setLayout(new GridLayout(2, 3, 5, 5));
         add(new FigurePanel(FigurePanel.LINE));
    }
}

但在增加(新FigurePanel(FigurePanel.LINE));我收到以下错误:

But at the "add(new FigurePanel(FigurePanel.LINE));" I get the following error:

找不到符号:

符号:类FigurePanel

Symbol: class FigurePanel

类:TestFigurePanel

class: TestFigurePanel"

我将非常高兴,如果有人好告诉我是什么原因造成这个问题。

I would be very happy if someone good inform me of what is causing this problem.

推荐答案

添加这个类在你的包:

import java.awt.*;

import javax.swing.JPanel;



public class FigurePanel extends JPanel {

// Define constants
public static final int LINE = 1;

public static final int RECTANGLE = 2;

public static final int ROUND_RECTANGLE = 3;

public static final int OVAL = 4;

private int type = 1;

private boolean filled;

/** Construct a default FigurePanel */

public FigurePanel() {

}

/** Construct a FigurePanel with the specified type */

public FigurePanel(int type) {

this.type = type;

}



/** Construct a FigurePanel with the specified type and filled */

public FigurePanel(int type, boolean filled) {

this.type = type;

this.filled = filled;

}



/** Draw a figure on the panel */

public void paintComponent(Graphics g) {

super.paintComponent(g);



// Get the appropriate size for the figure
int width = getSize().width;

int height = getSize().height;



switch (type) {

  case LINE: // Display two cross lines
    g.setColor(Color.BLACK);

    g.drawLine(10, 10, width - 10, height - 10);

    g.drawLine(width - 10, 10, 10, height - 10);

    break;

  case RECTANGLE: // Display a rectangle
    g.setColor(Color.BLUE);

    if (filled)

      g.fillRect((int)(0.1 * width), (int)(0.1 * height),

        (int)(0.8 * width), (int)(0.8 * height));

    else

      g.drawRect((int)(0.1 * width), (int)(0.1 * height),

        (int)(0.8 * width), (int)(0.8 * height));

    break;

  case ROUND_RECTANGLE: // Display a round-cornered rectangle
    g.setColor(Color.RED);

    if (filled)

      g.fillRoundRect((int)(0.1 * width), (int)(0.1 * height),

        (int)(0.8 * width), (int)(0.8 * height), 20, 20);

    else

      g.drawRoundRect((int)(0.1 * width), (int)(0.1 * height),

        (int)(0.8 * width), (int)(0.8 * height), 20, 20);

    break;

  case OVAL: // Display an oval
    g.setColor(Color.BLACK);

    if (filled)

      g.fillOval((int)(0.1 * width), (int)(0.1 * height),

        (int)(0.8 * width), (int)(0.8 * height));

    else

      g.drawOval((int)(0.1 * width), (int)(0.1 * height),

        (int)(0.8 * width), (int)(0.8 * height));

}

}



  /** Set a new figure type */

  public void setType(int type) {

  this.type = type;

  repaint();

}



/** Return figure type */

public int getType() {

return type;

}



/** Set a new filled property */

public void setFilled(boolean filled) {

  this.filled = filled;

  repaint();

}



/** Check if the figure is filled */

public boolean isFilled() {

return filled;

}



/** Specify preferred size */

public Dimension getPreferredSize() {

return new Dimension(80, 80);

}


}

这篇关于无法获取NetBeans的访问" FigurePanel"在Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆