如何影响另一个线程? (例如:setSelect复选框)

时间:2016-01-12 21:12:39

标签: java netbeans

我创建了两个" jFrame表单"与netbeans。我可以同时打开这两个。此代码运行但没有效果。我尝试使用form1中的按钮setSelect form2中的复选框。不知何故,我无法影响另一个线程。你能帮我解决一下吗?谢谢。 (对不起,我的英语不好,我也在学英语)

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

var typeCodes = ["061", "064", "065", "089", "090"];

$("fieldset[data-typecode]").filter(function() {
    //Get fieldsets with typecodes in above array and with selected options
    var code = $(this).data("typecode");
    return typeCodes.indexOf(code) > -1 && $(this).find("select option:selected").length
}).each(function() {
    //Do something on each fieldset
});

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

package test;
import javax.swing.SwingUtilities;
public class form1 extends javax.swing.JFrame {
public form1() {
    initComponents();
}                                      
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
  new form2().setVisible(true);
}                                        
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            final form2 click = new form2();
            SwingUtilities.invokeLater(
            new Runnable() {
            public void run() {
            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                  
}

1 个答案:

答案 0 :(得分:0)

在您的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