HELP! 一个关于对象数组的小问题 循环中是i<=2不是i<2 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 thx,我没注意那。:)不过不是那的错:--------------------Configuration: JDK version <Default>--------------------E:\Program\Program\Java\JCreator\MyProjects\appActionEvent.java:11: ']' expected btn[0] = new Button("Yellow"); ^E:\Program\Program\Java\JCreator\MyProjects\appActionEvent.java:11: <identifier> expected btn[0] = new Button("Yellow"); ^E:\Program\Program\Java\JCreator\MyProjects\appActionEvent.java:12: ']' expected btn[1] = new Button("Green"); ^E:\Program\Program\Java\JCreator\MyProjects\appActionEvent.java:12: <identifier> expected btn[1] = new Button("Green"); ^E:\Program\Program\Java\JCreator\MyProjects\appActionEvent.java:13: ']' expected btn[2] = new Button("Exit"); ^E:\Program\Program\Java\JCreator\MyProjects\appActionEvent.java:13: <identifier> expected btn[2] = new Button("Exit"); ^6 errorsProcess completed. import java.awt.*;import java.awt.event.*;public class appActionEvent extends Frame implements ActionListener{ static appActionEvent frm = new appActionEvent();/* static Button btn1 = new Button("Yellow"); static Button btn2 = new Button("Green"); static Button btn3 = new Button("Exit");*/ static Button btn[] = new Button[3]; public static void main(String args[]) { btn[0] = new Button("Yellow"); btn[1] = new Button("Green"); btn[2] = new Button("Exit");/* btn1.addActionListener(frm); btn2.addActionListener(frm); btn3.addActionListener(frm);*/ for(int i =0;i <=2;i++) btn[i].addActionListener(frm); frm.setLayout(new FlowLayout(FlowLayout.CENTER)); frm.setTitle("Action Event"); frm.setSize(200,150); /* frm.add(btn1); frm.add(btn2); frm.add(btn3);*/ for(int i =0;i <=2;i++) frm.add(btn[i]); frm.setVisible(true); frm.addWindowListener ( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); } public void actionPerformed(ActionEvent e) { Button bt = (Button) e.getSource(); if(bt==btn[0]) //if(btn ==btn1) frm.setBackground(Color.yellow); else if(bt == btn[1]) //else if(btn ==btn2) frm.setBackground(Color.green); else System.exit(0); }} 谢谢楼上的~!不过btn[]不是已经声明为static吗?为什么要在main()里面赋值才行? 赋值语句只能在方法中出现;不管是不是static 和 对象等 一切都是如你上面的可以这样写;声明的时候赋值;如 class A { static Button btn[] = {new Button("1"),new Button("2"),new Button("3")}; //int x;// /* x =1;*/ 不能在这赋值; int x =1 ; } log4j与换行,跨平台 求一个算法问题!,如何得到2棵树,最下的子节点,并且判断是否是同一顺序。着急! 请教jmf的设计问题 一个JTextField是否只能输入一行文字? 高分求救FileReader远程操作文件 Vector与ArrayList哪个比较好点?(易操作、效率高、方法多、优点明显==) 寻求JB8+Weblogic7的配置! 难道没有人可以告我如何在applet中使用jni方法吗? 一道难题,高手请进,紧急求救!b/s 方式文件序列化问题? 解决问题! 小问题!@!!!! 怎么让菜单项实现像单选框的功能
不过不是那的错:--------------------Configuration: JDK version <Default>--------------------
E:\Program\Program\Java\JCreator\MyProjects\appActionEvent.java:11: ']' expected
btn[0] = new Button("Yellow");
^
E:\Program\Program\Java\JCreator\MyProjects\appActionEvent.java:11: <identifier> expected
btn[0] = new Button("Yellow");
^
E:\Program\Program\Java\JCreator\MyProjects\appActionEvent.java:12: ']' expected
btn[1] = new Button("Green");
^
E:\Program\Program\Java\JCreator\MyProjects\appActionEvent.java:12: <identifier> expected
btn[1] = new Button("Green");
^
E:\Program\Program\Java\JCreator\MyProjects\appActionEvent.java:13: ']' expected
btn[2] = new Button("Exit");
^
E:\Program\Program\Java\JCreator\MyProjects\appActionEvent.java:13: <identifier> expected
btn[2] = new Button("Exit");
^
6 errorsProcess completed.
import java.awt.event.*;public class appActionEvent extends Frame implements ActionListener
{
static appActionEvent frm = new appActionEvent();
/*
static Button btn1 = new Button("Yellow");
static Button btn2 = new Button("Green");
static Button btn3 = new Button("Exit");
*/
static Button btn[] = new Button[3];
public static void main(String args[])
{
btn[0] = new Button("Yellow");
btn[1] = new Button("Green");
btn[2] = new Button("Exit");
/*
btn1.addActionListener(frm);
btn2.addActionListener(frm);
btn3.addActionListener(frm);
*/
for(int i =0;i <=2;i++)
btn[i].addActionListener(frm);
frm.setLayout(new FlowLayout(FlowLayout.CENTER));
frm.setTitle("Action Event");
frm.setSize(200,150);
/*
frm.add(btn1);
frm.add(btn2);
frm.add(btn3);
*/
for(int i =0;i <=2;i++)
frm.add(btn[i]);
frm.setVisible(true);
frm.addWindowListener
(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public void actionPerformed(ActionEvent e)
{
Button bt = (Button) e.getSource();
if(bt==btn[0]) //if(btn ==btn1)
frm.setBackground(Color.yellow);
else if(bt == btn[1]) //else if(btn ==btn2)
frm.setBackground(Color.green);
else
System.exit(0);
}
}
不过btn[]不是已经声明为static吗?为什么要在main()里面赋值才行?
不管是不是static 和 对象等 一切都是
如你上面的可以这样写;
声明的时候赋值;
如 class A {
static Button btn[] = {new Button("1"),new Button("2"),new Button("3")};
//int x;//
/* x =1;*/ 不能在这赋值;
int x =1 ;
}