我如何影响另一个班级? (例如:单击复选框) [英] How can I effect to another class? (for example: to click the checkbox)

查看:40
本文介绍了我如何影响另一个班级? (例如:单击复选框)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何点击另一个课程中的复选框。请你帮助我好吗?谢谢。



这个代码在class2.java中。没有错误但也没有效果。

Hi, how can I click the checkbox in another class. Could you please help me? Thanks.

This code in class2.java. No error but no effect, too.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
      class1 click=new class1();
      click.jCheckBox1.setSelected(true);  // jCheckBox1 in class1.java
   }

推荐答案

它应该真的崩溃你的应用程序。您创建了一个新的class1实例,尝试单击其中尚未创建的复选框,然后保留此方法,此时此对象将被删除。您需要选择屏幕上和原始对象中存在的复选框,您(可能)在程序开头创建。
It should really crash your app. You create a new instance of class1, try to click a checkbox in it which you have not yet created, then leave this method, at which time this object gets deleted. You need to select the checkbox that exists on the screen, and in your original object, that you (presumably) created at the beginning of your program.


问题是:class1不完整实例化了。

你应该使用Swingutilities.invokelater(...)。
The problem is: class1 is not fully instantiated yet.
You should use Swingutilities.invokelater(...).


package test;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;

/**
 *
 * @author Roland
 */
public class Test {
    
    private static class A extends JFrame {
        {
            JButton b = new JButton("click");
            add(b);
            pack();
            b.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    B f = new B();
                    f.cb.setSelected(true);
                }
            });
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
    };
    
    private static class B extends JFrame {
        public JCheckBox cb = new JCheckBox("Hallo");
    
        {
            add(cb);
            pack();
            setVisible(true);
        }
    };
    
    public static void main(String[] args) {
        new A().setVisible(true);
    }
    
}


这篇关于我如何影响另一个班级? (例如:单击复选框)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆