Java 我怎样才能影响到另一个线程?(例如:设置复选框)

Java 我怎样才能影响到另一个线程?(例如:设置复选框),java,netbeans,Java,Netbeans,我用netbeans创建了两个“jFrame表单”。我可以同时打开两个。此代码运行但不起作用。我尝试在form2中设置一个复选框,并在form1中设置一个按钮。不知何故,我不能影响另一个线程。你能帮我修一下吗。谢谢(对不起,我的英语不好,我也在学英语) 这是form1.java(我删除了一些自动代码) 这是form2.java(我删除了一些自动代码) 在jButton2ActionPerformed方法中,您正在创建一个从未显示过的新JFrame。您正在操作在jButton1ActionPerf

我用netbeans创建了两个“jFrame表单”。我可以同时打开两个。此代码运行但不起作用。我尝试在form2中设置一个复选框,并在form1中设置一个按钮。不知何故,我不能影响另一个线程。你能帮我修一下吗。谢谢(对不起,我的英语不好,我也在学英语)

这是form1.java(我删除了一些自动代码)

这是form2.java(我删除了一些自动代码)


jButton2ActionPerformed
方法中,您正在创建一个从未显示过的新JFrame。您正在操作在
jButton1ActionPerformed
中显示的JFrame

试试这个:

package test;
import javax.swing.SwingUtilities;
public class form1 extends javax.swing.JFrame {
private form2 click = null; // this will hold the second JFrame (the one with the checkbox)
public form1() {
    initComponents();
}                                      
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  if (click == null) { // only create the second JFrame once
      click = new form2(); // this stores the second JFrame with the name "click"
      click.setVisible(true);
  }
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            SwingUtilities.invokeLater(
            new Runnable() {
            public void run() {
                if (click != null) { // don't do anything if the second JFrame isn't displayed yet
                    click.jCheckBox1.setSelected(true);
                }
            }
        }
    );
}                                        
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new form1().setVisible(true);
    }
    });
}  
// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
// End of variables declaration                  
}

顺便说一句,以小写字母开头的类名格式不好,而且非常容易混淆。将
form1
重命名为
form1
并将
form2
重命名为
form2

谢谢阿尔文。但请解释更多。最后一个问题,在这个阶段;我可以在对面吗;我是说,就像布里奇一样。我将把复选框放在表格1中,并在表格2中设置带有按钮的复选框。因此,我可以相互影响。@Sertaç:您可以向包含实例的
form1
类添加一个静态字段,例如
publicstaticform1 myForm1
并在“main”方法中使用类似于“jButton1ActionPerformed”中使用的内容:
myForm1=newform1();myForm1.setVisible(真)。然后在
form2
中,您可以使用
form1.myForm1
参考
form1
。例如,
form1.myForm1.jCheckBox1.setSelected(true)package test;
public class form2 extends javax.swing.JFrame {
public form2() {
    initComponents();
}                 
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new form2().setVisible(true);
        }
    });
}  
// Variables declaration - do not modify                     
public javax.swing.JCheckBox jCheckBox1;
// End of variables declaration                
}
package test;
import javax.swing.SwingUtilities;
public class form1 extends javax.swing.JFrame {
private form2 click = null; // this will hold the second JFrame (the one with the checkbox)
public form1() {
    initComponents();
}                                      
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  if (click == null) { // only create the second JFrame once
      click = new form2(); // this stores the second JFrame with the name "click"
      click.setVisible(true);
  }
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            SwingUtilities.invokeLater(
            new Runnable() {
            public void run() {
                if (click != null) { // don't do anything if the second JFrame isn't displayed yet
                    click.jCheckBox1.setSelected(true);
                }
            }
        }
    );
}                                        
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new form1().setVisible(true);
    }
    });
}  
// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
// End of variables declaration                  
}