![](/img/trans.png)
[英]Passing JButton action from one class to JTextfield in another class
[英]Passing jtextfield value from one class to another
我有一個關於如何單擊按鈕將jextfield的值從一幀傳遞到第二幀的jtextfield的問題。
下面的示例在凈工資文本字段中輸入值之后,單擊添加按鈕,該值將傳遞到第二幀的凈工資文本字段中。
框架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);
}
}
}
如果在Frame1之前存在Frame2的實例
在Frame1
中,在構造函數或mutator方法中傳遞Frame2
的實例。
public Frame1(Frame2 frame2)
{
this.frame2 = frame2;
}
然后在Frame2
,有一個可以更新值的方法,例如updateNetWage
。
public void updateNetWage(String value)
{
nettNameTextField.setText(value);
}
然后,您可以在actionListener
函數中簡單地調用:
frame2.updateNetWage(value);
如果Frame2的實例不可用
您可以為Frame2
定義一個參數構造Frame2
,因此您可以創建一個包含值的新對象。
public Frame2(String netNameValue)
{
nettNameTextField.setText(netNameValue);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.