【发布时间】:2014-02-26 07:32:15
【问题描述】:
我试图在用户输入的任何位置画鱼,但它会说
drawFish.java:38: error: cannot find symbol
outer.add(sPanel1);
或者
Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
at java.awt.Container.checkNotAWindow(Container.java:483)
at java.awt.Container.addImpl(Container.java:1084)
at java.awt.Container.add(Container.java:410)
at drawFish.main(drawFish.java:38)
我在想我需要为每条鱼制作一个新面板,但是如何制作一个循环来创建多个面板?如果这甚至是问题?另外,我应该使用一种采用 x 和 y 坐标的方法,以便用户可以更改鱼的位置,并在不同的位置绘制许多鱼。但这不是我正在做的。我试图制作一个包含 x 和 y 问题的方法,但是它说变量不是公共的,因此不能在paint方法中使用。我会感谢对一切的解释,因为我想理解我正在做的一切。
public class drawFish extends JPanel {
int x = Integer.parseInt(JOptionPane.showInputDialog(null, "What is the x location of the fish? "));
int y = Integer.parseInt(JOptionPane.showInputDialog(null, "What is the y location of the fish? "));
int w = 200;
int h = 100;
int a = x + 20;
int b = y + 30;
int d = 50;
public drawFish() {
setPreferredSize(
new Dimension(400,400));
}
public void paint(Graphics g) {
g.setColor(Color.GREEN);
g.fillOval(x, y, w, h);
g.fillOval((w-5), y, d, h);
g.setColor(Color.BLACK);
g.fillOval(a, b, 25, 25);
}
public static void main(String[] args) {
MyFrame frame1 = new MyFrame("Drawing Fish");
JPanel outer = new JPanel();
int fn = Integer.parseInt(JOptionPane.showInputDialog(null, "How many fish would you like to draw? "));
for(int i=0; i<fn; i++){
drawFish sPanel1 = new drawFish();
}
outer.add(sPanel1);
frame1.add(outer);
frame1.pack();
frame1.setVisible(true);
}
}
【问题讨论】:
标签: java user-interface methods panel