Topic: 参数非法?求教高手

  Print this page

1.参数非法?求教高手 Copy to clipboard
Posted by: ice0819
Posted on: 2008-09-28 15:13


import java.awt.*;
import java.awt.event.*;

public class Debug extends Frame{
  final String names[]={"1","2","3"};
  final Button [] buttons=new Button[3];
  String [] locations={"North","Sourth","Center"};
  
  
  public Debug(String s )
  { super(s);
  final BorderLayout layout = (BorderLayout)this.getLayout();
  
     ActionListener listener=new ActionListener()
     { public void actionPerformed( ActionEvent evt)
       {
                
       ((Button)evt.getSource()).setVisible(false);
       layout.layoutContainer(Debug.this);
       }  
     
     };
    
  
     for (int i=0;i<buttons.length;i++)
     {
   buttons[i]=new Button(names[i]);
   buttons[i].addActionListener(listener);
   add(buttons[i],locations[i]);
     }


setSize(100,100);
setVisible(true);
}

  
  public static void main(String[] args) {
    new Debug("Made by ice");
  }

}


编译正确,可是运行是,提示参数非法

2.Re:参数非法?求教高手 [Re: ice0819] Copy to clipboard
Posted by: ice0819
Posted on: 2008-09-28 15:15

表情处是super(s)

3.Re:参数非法?求教高手 [Re: ice0819] Copy to clipboard
Posted by: xuxiaolei
Posted on: 2008-09-29 06:39


import java.awt.*;
import java.awt.event.*;

public class Debug extends Frame {
  final String names[] = { "1", "2", "3" };
  final Button[] buttons = new Button[3];
  String[] locations = {BorderLayout.NORTH , BorderLayout.SOUTH, BorderLayout.CENTER };

  public Debug(String s) {
    super(s);
    BorderLayout layout = (BorderLayout) this.getLayout();

    ActionListener listener = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {

        ((Button) evt.getSource()).setVisible(false);
        //layout.layoutContainer(Debug.this);
      }

    };

    for (int i = 0; i < buttons.length; i++) {
      buttons[i] = new Button(names[i]);
      buttons[i].addActionListener(listener);
      add(buttons[i], locations[i]);
    }

    setSize(100, 100);
    setVisible(true);
  }

  public static void main(String[] args) {
    new Debug("Made by ice");
  }

}

4.Re:参数非法?求教高手 [Re: ice0819] Copy to clipboard
Posted by: ice0819
Posted on: 2008-09-29 08:52

还是不行!

5.Re:参数非法?求教高手 [Re: ice0819] Copy to clipboard
Posted by: xuxiaolei
Posted on: 2008-09-29 09:02

但是我的就可以啊,我是JDK1.5的,下面是运行截图

s.JPG (36.38k)

6.Re:参数非法?求教高手 [Re: ice0819] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2008-09-29 16:24

This is the error log when I run it in jdk6


$ java Debug
Exception in thread "main" java.lang.IllegalArgumentException: cannot add to layout: unknown constraint: Sourth
at java.awt.BorderLayout.addLayoutComponent(Unknown Source)
at java.awt.BorderLayout.addLayoutComponent(Unknown Source)
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at Debug.<init>(Debug.java:29)
at Debug.main(Debug.java:39)


can you see where the problem is, it is quite obvious?

xuxiaolei's code is much better and reliable.

Regards,
Jiafan

7.Re:参数非法?求教高手 [Re: ice0819] Copy to clipboard
Posted by: ice0819
Posted on: 2008-09-29 16:35

因为add.(button,"North") 与add(button,BorderLayout.NORTH)是等效的,所以我在给数组中元素赋值的时候就简略了。可是忘了,数组中的元素取出来的时候是没有引号的。改过来了,可以了。谢谢大家

8.Re:参数非法?求教高手 [Re: ice0819] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2008-10-01 18:39

> 因为add.(button,"North") 与add(button,BorderLayout.NORTH)是等效的
You should rather use BorderLayout.NORTH then "North". They are not equal, above all one injects error while the other one does not.


   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