您好,当我在“商品价格”TextField中输入文本时,我在“销售成本”文本字段中显示空白。我想显示我在“物料价格”文本框中输入的文本,并在“销售成本”文本框中显示它未显示的文本。Java GUI:从文本字段获取文本
public class ZipTimer extends JFrame {
private JTextField input_2;
private JTextField itemprice;
btnInsert = new JButton("Insert");
btnInsert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(new DBUpdater().add(itemname.getText(),itemprice.getText(),itemcategory.getText())){
JOptionPane.showMessageDialog(null,"Successfully Inserted");
//Clear Text
itemprice.setText("");
input_2.setText(itemprice.getText());
retrieve();
}else{
JOptionPane.showMessageDialog(null, "Not Saved");
}
}
});
在销售成本的文本字段的名称是input_2
和文本字段的项目价格名字是itemprice
我试试其他的代码是这样input_2.setText(Integer.toString(itemprice));
但我有一个在toString
错误我不知道为什么。它说“Integer类型中的方法toString
(int)不适用于参数(JTextField)”。
您是否尝试过['itemprice.getText()'](https://docs.oracle.com/javase/8/docs/ API /使用javax /秋千/文本/ JTextComponent.html#getText--)? – bradimus
另外,'itemprice'初始化在哪里?如果未初始化,此代码将抛出NullPointerException(npe)。你看到这个了吗? 'input_2'一样(可怕的变量名称 - 请改进)。请创建并发布有效的[mcve]。 –
itemprice是一个文本字段,而不是一个整数?异常是自我解释 –