在這些代碼中,我可以獲得有多少個Job可用(JobCount),並在接下來的JFrame
中我想要JobCount
次JToggleButtton
。但JobCount
不能正確傳遞。我怎樣才能讓它可以傳遞幀的信息?從JFrames傳遞信息
public class NewJFrame extends javax.swing.JFrame {
public NewJFrame() {
initComponents();
}
public int PeopleCount;
public int JobCount;
public JFrame2 jf2;
private void PJCountjButtonActionPerformed(java.awt.event.ActionEvent evt) {
PeopleCount=(int)(PeopleCountjSpinner.getValue());
JOptionPane.showMessageDialog(null, "peoplecount="+PeopleCount);
JobCount=(int)(JobCountjSpinner.getValue());
jf2=new JFrame2();
jf2.setVisible(true);
jf2.c1=this;
this.setVisible(false);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
public class JFrame2 extends javax.swing.JFrame {
int j;
public NewJFrame c1 = new NewJFrame();
JPanel contentpane;
JButton show;
public JButton[] jbarray = new JButton[c1.JobCount];
public int array[][] = new int[c1.PeopleCount][c1.JobCount];
public JFrame2() {
initComponents();
JOptionPane.showMessageDialog(null, "peoplecount="+c1.PeopleCount);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setBounds(10, 01, 500, 400);
this.contentpane = new JPanel();
this.show = new JButton("Show array");
contentpane.add(show);
for (j = 0; j < c1.JobCount; j++) {
this.jbarray[j] = new JButton("job" + (j + 1));
contentpane.add(jbarray[j]);
}
this.setContentPane(contentpane);
this.setVisible(true);
}}
// Variables declaration - do not modify
private javax.swing.JSpinner JobCountjSpinner;
private javax.swing.JButton PJCountjButton;
private javax.swing.JSpinner PeopleCountjSpinner;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
// End of variables declaration }
見(http://stackoverflow.com/a/9554657/418556)通常這種類型的問題的解決方案是使用模態組件,如「JDialog」或「JOptionPane」。 –
_「我怎樣才能讓它可以傳遞幀的信息?」_使用setter或pass通過構造函數 –