2013-08-01 124 views
0

我有问题,关于如何在单击按钮时将jxtfield的值从一帧传递到第二帧中的jtextfield。将jtextfield值从一个类传递到另一个类

下面的示例中,在nett工资文本字段中输入值后,单击添加按钮后,该值将被传递到第二个框架的nett工资文本字段。

框架1.我试图添加actionlistener到我的按钮来获取文本,但我如何设置它在第二帧?感谢任何人都可以提供一些建议和帮助。

public class Frame1 extends JFrame{ 
public Frame1() { 

    JPanel guiPanel = new JPanel(new GridBagLayout()); 

    JLabel Nett = new JLabel("Nett Wage: "); 
    final JTextField nettNameTextField = new JTextField(10); 
    JButton addButton = new JButton("Add"); 


    JPanel fields = new JPanel(new GridBagLayout()); 

    GridBagConstraints labelGBC = new GridBagConstraints(); 
    labelGBC.insets = new Insets(10, 3, 3, 3); 
    GridBagConstraints fieldGBC = new GridBagConstraints(); 
    fieldGBC.insets = new Insets(10, 3, 3, 3); 
    GridBagConstraints titleGBC = new GridBagConstraints(); 
    fieldGBC.gridwidth = GridBagConstraints.REMAINDER; 


    fields.add(Nett, labelGBC); 
    fields.add(nettNameTextField, fieldGBC); 


    JPanel buttons = new JPanel(new GridBagLayout()); 

    GridBagConstraints addButtonGBC = new GridBagConstraints(); 
    addButtonGBC.insets = new Insets(40, 3, 3, 3); 
    cancelButtonGBC.gridwidth = GridBagConstraints.REMAINDER; 
    buttons.add(addButton, addButtonGBC); 


    GridBagConstraints gbc = new GridBagConstraints(); 
    gbc.gridwidth = GridBagConstraints.REMAINDER; 
    guiPanel.add(fields, gbc); 
    guiPanel.add(buttons, gbc); 


    add(guiPanel, BorderLayout.CENTER); 

    /*addButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      String value = nettNameTextField.getText(); 

     } 
    });*/ 

第2帧如何设置文本框中的值?

public class Frame2 extends JFrame { 
public Frame2() { 


    JPanel guiPanel = new JPanel(new GridBagLayout()); 


    JLabel nett = new JLabel("Nett Wage: "); 
    JTextField nettNameTextField = new JTextField(10); 


    JPanel fields = new JPanel(new GridBagLayout()); 


    GridBagConstraints labelGBC = new GridBagConstraints(); 
    labelGBC.insets = new Insets(10, 3, 3, 3); 
    GridBagConstraints fieldGBC = new GridBagConstraints(); 
    fieldGBC.insets = new Insets(10, 3, 3, 3); 
    //GridBagConstraints titleGBC = new GridBagConstraints(); 
    fieldGBC.gridwidth = GridBagConstraints.REMAINDER; 




    JPanel savingspanel = new JPanel(new GridBagLayout()); 

    GridBagConstraints totallabelsGBC = new GridBagConstraints(); 
    totallabelsGBC.insets = new Insets(10, 3, 3, 3); 
    GridBagConstraints totalfieldGBC = new GridBagConstraints(); 
    totalfieldGBC.insets = new Insets(10, 3, 3, 3); 
    totalfieldGBC.gridwidth = GridBagConstraints.REMAINDER; 
    savingspanel.add(nett, labelGBC); 
    savingspanel.add(nettNameTextField, fieldGBC); 



    add(guiPanel, BorderLayout.CENTER);      
     }  
}     
} 

回答

0

如果Frame1中

Frame1之前存在式2的一个实例,通过在Frame2一个实例,在任一构造中或在增变方法。

public Frame1(Frame2 frame2) 
{ 
    this.frame2 = frame2; 
} 

然后在Frame2,具有可更新的价值,像updateNetWage的方法。

public void updateNetWage(String value) 
{ 
    nettNameTextField.setText(value); 
} 

在你actionListener功能,您可以直接电话咨询:

frame2.updateNetWage(value); 

如果式2的实例不可

您可以定义一个参数的构造函数用于Frame2,所以您可以使用其中的值创建一个新对象。

public Frame2(String netNameValue) 
{ 
    nettNameTextField.setText(netNameValue); 
} 
+0

也许他不应该将完整的Frame1引用传递给构造函数。我宁愿让实现Frame1的接口使用特殊的get方法,并将此接口传递给Frame2构造函数。 – sk2212

+0

如果你只是传递接口,你仍然传递对象。这是一个很好的观点,但在我看来,这是一个不必要的扩展,只会使代码更加复杂,以至于程序员可以在几秒钟内使用一种方法。 – christopher

相关问题