Java中出现以下错误,何解?
两段出错代码如下:packagebegin;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;imp...
两段出错代码如下:
package begin;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import resource.getresource;
import begin.enter;
public class start extends JFrame {
protected static JFrame jf = new JFrame("开始游戏");
public static void setButton(final int k,String s,final JFrame jf,Container cont) {
getresource get = new getresource();
JButton jb = new JButton(s,get.geticon("button/button1.bmp"));
cont.add(jb);
jb.setBounds(100,50*k+5,200,40);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jf.dispose();
enter en = new enter();
en.entermain(k);
}
});
jb.setSize(200,40);
}
public static void main(String[] args) {
String str[] = {"","简单","普通","困难","疯狂","超神" };
Container cont = jf.getContentPane();
cont.setBackground(Color.orange);
cont.setLayout(null);
jf.setVisible(true);
jf.setBounds(400,200,400,330);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JLabel jl = new JLabel("请选择难度:");
cont.add(jl);
jl.setBounds(100,5,200,40);
for (int i=1; i<6; i++) {
setButton(i,str[i],jf,cont);
}
}
}
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2
at begin.enter.entermain(enter.java:9)
at begin.start$1.actionPerformed(start.java:22)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
太多写不下了 展开
package begin;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import resource.getresource;
import begin.enter;
public class start extends JFrame {
protected static JFrame jf = new JFrame("开始游戏");
public static void setButton(final int k,String s,final JFrame jf,Container cont) {
getresource get = new getresource();
JButton jb = new JButton(s,get.geticon("button/button1.bmp"));
cont.add(jb);
jb.setBounds(100,50*k+5,200,40);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jf.dispose();
enter en = new enter();
en.entermain(k);
}
});
jb.setSize(200,40);
}
public static void main(String[] args) {
String str[] = {"","简单","普通","困难","疯狂","超神" };
Container cont = jf.getContentPane();
cont.setBackground(Color.orange);
cont.setLayout(null);
jf.setVisible(true);
jf.setBounds(400,200,400,330);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JLabel jl = new JLabel("请选择难度:");
cont.add(jl);
jl.setBounds(100,5,200,40);
for (int i=1; i<6; i++) {
setButton(i,str[i],jf,cont);
}
}
}
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2
at begin.enter.entermain(enter.java:9)
at begin.start$1.actionPerformed(start.java:22)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
太多写不下了 展开
3个回答
展开全部
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2
这个错误是数组越界的意思
at begin.enter.entermain(enter.java:9)
at begin.start$1.actionPerformed(start.java:22)这应该是错误行数
自己检查一下取数组值的时候有没有取错,这个是经常会失误的地方。
这个错误是数组越界的意思
at begin.enter.entermain(enter.java:9)
at begin.start$1.actionPerformed(start.java:22)这应该是错误行数
自己检查一下取数组值的时候有没有取错,这个是经常会失误的地方。
更多追问追答
追问
第22行是调用另一个对象的方法
追答
那就是那个对象里面的问题了,那个对象里面代码是啥?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询