2014-01-21 43 views
-1

在这些代码中,我可以获得有多少个Job可用(JobCount),并在接下来的JFrame中我想要JobCountJToggleButtton。但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   } 
+4

见(http://stackoverflow.com/a/9554657/418556)通常这种类型的问题的解决方案是使用模态组件,如“JDialog”或“JOptionPane”。 –

+2

_“我怎样才能让它可以传递帧的信息?”_使用setter或pass通过构造函数 –

回答

0

不要使用Multible的JFrame beccause它是一个不好的做法,你可以使用一个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); 
} 
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

+0

的值是真的。但问题没有解决。每个类中的jobCount是不同的 – user3212478

+0

因此,创建一个新的JFrame,并再次调用getJobCount()方法。 – Gorbles

+0

我做到了,但jobcount在两者都不同。我可以将信息从JFrame传递给JDialog吗? – user3212478

0

此外,我认为,帧之间共享值不是一个好习惯,我会建议,如果真的需要这种方式,只需将jobCount存储为整型对象并在需要时存储其引用。这样它总是最新的。

如果我明白你的意图是正确的,你只是想为每个工作和人员制作一个按钮控制面板,你可以在一个视图中完成这一切。 只需将frame1通过滑块获取计数,然后动态添加slider.value的按钮。您可以使用默认的borderlayout并将滑块向北并将按钮置于中心。

这个小SSCE应该帮助你走上正轨:?多JFrames,好/坏的做法的用途]

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); 
     } 
    }); 
} 
}