点击SAVE按钮后如何更新JTextfield [英] How to update JTextfield after click SAVE button
问题描述
我有一个主框架:JFrame> contentFrame> ScrollPane> BigPanel> panel_1T
private JPanel contentPane;
私人JPanel BigPanel;
私人JPanel panel_1T;
在panel_1T中,我已经放置了一个带有actionListener的食物按钮:
JButton button_19 = new JButton(FOOD);
button_19.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
newFoodUI nf = new newFoodUI(); //打开其他类
nf.setVisible(true);
nf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
});
panel_1T.setLayout(new GridLayout(0,2,0,0));
panel_1T.add(button_19);
当用户单击FOOD按钮时, newFoodUI中的新JFrame
类将显示:
JFrame> contentPane> panel> tabbedPane> panel_3> panel_5
在panel_5中,我放了一个JTextField:
public static JTextField textField_3;
textField_3 = new JTextField();
panel_5.add(textField_3,9,4,fill,default);
textField_3.setColumns(10);
用户将把一些文本写入 textField_3
。然后用户点击panel_3中的SAVE按钮,将执行以下操作:
JButton button_4 = new JButton(SAVE);
button_4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
setContentPane(contentPane);
panel_3.revalidate();
panel_3.repaint();
panel_3.updateUI();
panel_5.revalidate();
panel_5.repaint();
panel_5.updateUI );
contentPane.revalidate();
contentPane.repaint();
JOptionPane.showMessageDialog(null,Saved!
}
});
button_4.setBounds(873,396,75,33);
contentPane.add(button_4);结果是,当我点击保存按钮并关闭newFoodUI中的框架时,我将通过点击食物按钮重新打开,以检查我写的文本是否已保存。但它不保存我写的文本。 解决方案您必须保存textfeld中的值 textField_3.getText()
并在显示 textField_3.setText(value)
时手动将此值设置为textfeld。所以你必须保持你的价值在你的项目或存储持久的某个地方。
I have a main frame : JFrame>contentFrame>ScrollPane>BigPanel>panel_1T
private JPanel contentPane;
private JPanel BigPanel;
private JPanel panel_1T;
In panel_1T, I have put a FOOD button WITH its actionListener:
JButton button_19 = new JButton("FOOD");
button_19.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
newFoodUI nf = new newFoodUI();//Open other class
nf.setVisible(true);
nf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
});
panel_1T.setLayout(new GridLayout(0, 2, 0, 0));
panel_1T.add(button_19);
When user click FOOD button, new JFrame in newFoodUI
class will be shown.:
JFrame>contentPane>panel>tabbedPane>panel_3>panel_5
In panel_5, I put a JTextField:
public static JTextField textField_3;
textField_3 = new JTextField();
panel_5.add(textField_3, "9, 4, fill, default");
textField_3.setColumns(10);
User will write some text into textField_3
. Then user click SAVE button in panel_3, it will perform this:
JButton button_4 = new JButton("SAVE");
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setContentPane(contentPane);
panel_3.revalidate();
panel_3.repaint();
panel_3.updateUI();
panel_5.revalidate();
panel_5.repaint();
panel_5.updateUI();
contentPane.revalidate();
contentPane.repaint();
JOptionPane.showMessageDialog(null, "Saved !");
}
});
button_4.setBounds(873, 396, 75, 33);
contentPane.add(button_4);
}
The result is, when I click SAVE button and close the Frame in newFoodUI, I will reopen back by click the FOOD button to check whether the text I wrote has been saved or not. But its not saving the text I wrote.
解决方案 You have to save the value from the textfeld textField_3.getText()
and set this value manually to textfeld when showing textField_3.setText(value)
. So you have to keep your value in your project or store persistent somewhere.
这篇关于点击SAVE按钮后如何更新JTextfield的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!