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 ; } JAVA程序小问题。 maven -o intall 报错 包不存在 java连接池连接sql数据库 线程问题 请教一个加辅助线的问题 编译时提示出错,请指教 100分提议大家把所知道的好的java方面的电子书下载地址帖出来....... 那位兄弟给段代码:连接Oracle数据库的。 在jbuilder6中如何实现鼠标伦的滚动? 关于打开的游标超出最大数问题 小问题!@!!!! 怎么让菜单项实现像单选框的功能
不过不是那的错:--------------------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 ;
}