Topic: 以下代码为什么不能运行 (面板数组) |
Print this page |
1.以下代码为什么不能运行 (面板数组) | Copy to clipboard |
Posted by: flywithmoon Posted on: 2004-08-24 22:56 可以调试通过,但是运行时看不到任何结果。 import java.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; class test extends JFrame { test(){ this.getContentPane().add(new myp()); } public static void main(String args []){ test f=new test(); f.setSize(300,300); f.show(); } } class myp extends JPanel { JPanel p[]; myp() { setLayout(new GridLayout(2,2)); for(int i=0;i<4;i++) {p[i]=new JPanel(); p[i].setBackground(new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255))); add(p[i]);} } } |
2.Re:以下代码为什么不能运行 [Re: flywithmoon] | Copy to clipboard |
Posted by: zero_alf Posted on: 2004-08-25 12:36 你改成 class myp extends JPanel { myp() { setLayout(new GridLayout(2,2)); for(int i=0;i<4;i++) { JPanel p=new JPanel(); p.setBackground(new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255))); add;} } } 我编译通过运行成功了 |
3.Re:以下代码为什么不能运行 [Re: flywithmoon] | Copy to clipboard |
Posted by: flywithmoon Posted on: 2004-08-25 15:00 不能够使用面板数组吗? |
4.Re:以下代码为什么不能运行 [Re: flywithmoon] | Copy to clipboard |
Posted by: yangmin9702 Posted on: 2004-08-26 15:22 可以使用面板数组,但你必须先指定几个元素,所以你应将JPanel p[];改为 JPanel p[]=new JPanel[4];就可以了 |
5.Re:以下代码为什么不能运行 [Re: flywithmoon] | Copy to clipboard |
Posted by: flywithmoon Posted on: 2004-08-27 13:04 谢谢了,但是别的数组好像是可以先声明,不用定义界限的。 |
6.Re:以下代码为什么不能运行 [Re: flywithmoon] | Copy to clipboard |
Posted by: sankxuan Posted on: 2004-08-31 13:48 不可能的,你的p[]还没有new ,是null, 在你p[i]的时候肯定报错了 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |