在这些代码中,我会获得有多少作业可用的信息(JobCount)以及下一个JFrame
我想要JobCount
次JToggleButtton
的信息。但JobCount
未正确传递。我怎样才能使它能从帧中传递信息?
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 }
答案 0 :(得分:0)
不要使用Multiple JFrame,因为这是一个不好的做法,你可以使用一个JFrame和许多JDialog,如:
private void PJCountjButtonActionPerformed(java.awt.event.ActionEvent evt) {
PeopleCount=(int)(PeopleCountjSpinner.getValue());
JOptionPane.showMessageDialog(null, "peoplecount="+PeopleCount);
JobCount=(int)(JobCountjSpinner.getValue());
JDialog dialog = new CustomConstructor(params.) // here you can creaea your own constructor and pass the needed info.
dialog.setVisible(true);
}
答案 1 :(得分:0)
peeskillet说的是正确的。
private int jobCount;
public int getJobCount() {
return jobCount;
}
public void setJobCount(int jobCount) {
this.jobCount = jobCount;
}
其次是:
public class JFrame2() {
int passedJobCount = c1.getJobCount();
}
这就是你正确使用私有变量的方法。如需进一步阅读,请查看Oracle's Tutorial on Member Declarations。
答案 2 :(得分:0)
除了我认为,在帧之间共享值不是一个好习惯,我建议如果真的需要那样,只需将jobCount存储为整数对象并在需要的地方存储它的引用。这样它始终是最新的。
如果我理解你的意图是正确的,你只想为每个工作和个人制作一个按钮控制面板,你可以在一个视图中完成所有这些操作。 只需使用frame1通过滑块获取计数,然后动态添加slider.value的按钮。您可以使用默认的borderlayout并将滑块向北和按钮放在中心。
这个小小的ssce可以帮助你走上正轨:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class NewJFrame extends javax.swing.JFrame {
private static final long serialVersionUID = 1L;
private JSlider slider;
private JButton btnNewButton;
private JDialog d;
private JTextField text;
public NewJFrame() {
btnNewButton = new JButton("Do");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
d.setVisible(true);
}
});
getContentPane().add(btnNewButton, BorderLayout.CENTER);
slider = new JSlider();
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
text.setText(Integer.toString(slider.getValue()));
}
});
getContentPane().add(slider, BorderLayout.NORTH);
d = new JDialog();
text = new JTextField();
d.getContentPane().add(text);
d.pack();
pack();
setVisible(true);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
}