Java:IndexOutOfBounds异常和eventDispatch

时间:2015-04-09 22:22:37

标签: java indexoutofboundsexception jcomponent

如果标题不能描述我遇到的问题,我道歉。 我正在为一个简单的GUI计算器开发一个测试GUI。测试GUI将允许用户点击计算器GUI的组件以便记录它们。然后开始在这些组件中放置值来测试计算器的功能。

基本上,我设置了三个类MyListener,ThreadRunner和TestGUI。 TestGUI有两个按钮,一个用户可以点击计算器JTextfields并使用ThreadRunner中的方法记录它们,ThreadRunner中有一个ArrayList来存储JTextFields和其他变量来存储其他组件。另一个按钮是通过TestRunner中的方法将值设置到存储在ArrayList中的JTextField。我遇到的问题是,当我点击第二个按钮设置新值时,我得到了异常IndexOutOfBounds以及其他的。在过去的几天里,我一直在撞墙试图解决这个问题,所以任何帮助都会非常感激。谢谢!

public class ThreadRunner extends Thread {
//ChooseInput ci = new ChooseInput();
//List<String> inputVal = ci.getInput();

List<JTextField> listOfTextFields = new ArrayList<>();
JTextField output;
JButton goButton;

public void getInputField(JTextField a) {
    listOfTextFields.add(a);
    System.out.println("getinput func. in thread runner works");
    //System.out.println(a);
    System.out.print(listOfTextFields.get(0));
}

public void getOutputField(JTextField a) {
    output = a;
}

public void getButton(JButton a) {
    goButton = a;
}

public void go() {
    for (int i = 0; i < 2; i++) { // to test if go() can access listOfTextFields
        System.out.print(listOfTextFields.get(i));
    }
    listOfTextFields.get(0).setText("10");
    listOfTextFields.get(1).setText("20");
    System.out.println("func go() works");
}
}


public class MyListener implements AWTEventListener {
ThreadRunner t = new ThreadRunner();
public MyListener(ThreadRunner t) {
    t = t;
}

@Override
public void eventDispatched(AWTEvent event) {

    if (event.paramString().substring(0, 13).equals("MOUSE_CLICKED")) {
        System.out.println("Click happens");
        Object o = event.getSource();
        if (o.getClass().equals(JTextField.class)) {
            JTextField tf = (JTextField) o;
            if (tf.isEditable()) {
                t.getInputField(tf);
                System.out.println("TR shpuld have a field");
            } else {
                t.getOutputField(tf);
            }
        }

        if (o.getClass().equals(JButton.class)) {
            JButton jb = (JButton) o;
            t.getButton(jb);
        }
    }
}
}

public class TestGUI {

public TestGUI() {
    final ThreadRunner tr = new ThreadRunner();
    final MyListener ml = new MyListener(tr);
    JFrame f1 = new JFrame();
    JButton b1 = new JButton("get locations");
    JButton b2 = new JButton("test");
    f1.setVisible(true);
    f1.setSize(500, 200);

    f1.getContentPane().add(b1, BorderLayout.NORTH);
    f1.getContentPane().add(b2, BorderLayout.SOUTH);

    b1.setVisible(true);
    b2.setVisible(true);

    b1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent ae) {
            MyGUI gui = new MyGUI();
            gui.setSize(500, 300);
            gui.setVisible(true);
            gui.setLocation(200, 200);

            Toolkit tk = Toolkit.getDefaultToolkit();

            tk.addAWTEventListener(ml, AWTEvent.MOUSE_EVENT_MASK);
        }
    }
    );

    b2.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent ae) {
            tr.go();
        }
    }
    );
}
}

调用ThreadRunner中的go()方法时会产生错误 堆栈跟踪:

run:

点击发生 点击发生 getinput func。在线程运行中工作 javax.swing.JTextField中的[,103,12,249x41,布局= javax.swing.plaf.basic.BasicTextUI中$ UpdateHandler,alignmentX = 0.0,alignmentY = 0.0,边界= javax.swing.plaf.BorderUIResource $ @ CompoundBorderUIResource b97bc3,旗帜= 296,MAXIMUMSIZE =,=的minimumSize,首选大小=,caretColor = sun.swing.PrintColorUIResource [R = 51,G = 51,b = 51],disabledTextColor = javax.swing.plaf.ColorUIResource [R = 184,G = 207, b = 229],可编辑=真,裕度= javax.swing.plaf.InsetsUIResource [顶= 0,左= 0,底部= 0,右= 0],selectedTextColor = sun.swing.PrintColorUIResource [R = 51,G = 51,b = 51],selectionColor = javax.swing.plaf.ColorUIResource [r = 184,g = 207,b = 229],columns = 0,columnWidth = 0,command =,horizo​​ntalAlignment = LEADING] TR shpuld有一个字段 点击发生 getinput func。在线程运行中工作 javax.swing.JTextField中的[,103,12,249x41,布局= javax.swing.plaf.basic.BasicTextUI中$ UpdateHandler,alignmentX = 0.0,alignmentY = 0.0,边界= javax.swing.plaf.BorderUIResource $ @ CompoundBorderUIResource b97bc3,旗帜= 296,MAXIMUMSIZE =,=的minimumSize,首选大小=,caretColor = sun.swing.PrintColorUIResource [R = 51,G = 51,b = 51],disabledTextColor = javax.swing.plaf.ColorUIResource [R = 184,G = 207, b = 229],可编辑=真,裕度= javax.swing.plaf.InsetsUIResource [顶= 0,左= 0,底部= 0,右= 0],selectedTextColor = sun.swing.PrintColorUIResource [R = 51,G = 51,b = 51],selectionColor = javax.swing.plaf.ColorUIResource [r = 184,g = 207,b = 229],columns = 0,columnWidth = 0,command =,horizo​​ntalAlignment = LEADING] TR shpuld有一个字段 线程中的异常&#34; AWT-EventQueue-0&#34; java.lang.IndexOutOfBoundsException:Index:0,Size:0 点击发生     at java.util.ArrayList.rangeCheck(ArrayList.java:653)     at java.util.ArrayList.get(ArrayList.java:429)     在ThreadRunner.go(ThreadRunner.java:43)     在TestGUI $ 2.actionPerformed(TestGUI.java:55)     在javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)     在javax.swing.AbstractButton $ Handler.actionPerformed(AbstractButton.java:2346)     在javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)     在javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)     在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)     at java.awt.Component.processMouseEvent(Component.java:6525)     在javax.swing.JComponent.processMouseEvent(JComponent.java:3324)     at java.awt.Component.processEvent(Component.java:6290)     at java.awt.Container.processEvent(Container.java:2234)     at java.awt.Component.dispatchEventImpl(Component.java:4881)     at java.awt.Container.dispatchEventImpl(Container.java:2292)     at java.awt.Component.dispatchEvent(Component.java:4703)     at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)     at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)     at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)     at java.awt.Container.dispatchEventImpl(Container.java:2278)     at java.awt.Window.dispatchEventImpl(Window.java:2750)     at java.awt.Component.dispatchEvent(Component.java:4703)     at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:751)     at java.awt.EventQueue.access $ 500(EventQueue.java:97)     at java.awt.EventQueue $ 3.run(EventQueue.java:702)     at java.awt.EventQueue $ 3.run(EventQueue.java:696)     at java.security.AccessController.doPrivileged(Native Method)     at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:75)     at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:86)     at java.awt.EventQueue $ 4.run(EventQueue.java:724)     在java.awt.EventQueue $ 4.run(EventQueue.java:722)     at java.security.AccessController.doPrivileged(Native Method)     at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:75)     at java.awt.EventQueue.dispatchEvent(EventQueue.java:721)     at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)     at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)     at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)     在java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

1 个答案:

答案 0 :(得分:0)

这里的问题是您使用了

ThreadRunner t = new ThreadRunner();
public MyListener(ThreadRunner t) {
    t = t;
}

基本上将参数赋给自身而不是上面定义的局部变量。通过使用this,您明确指定您希望引用实例变量t而不是参数。 所以要么使用

ThreadRunner t = new ThreadRunner();
public MyListener(ThreadRunner t) {
    this.t = t;
}

或者将您的类变量重命名为其他名称,例如

ThreadRunner ct = new ThreadRunner();
public MyListener(ThreadRunner t) {
    ct = t;
}