我的JFormattedTextField物体上有两个物体JFrame.我希望通过这些JFormattedTextField对象的值得到基本的数学(加法).当焦点丢失第一个或第二个文本字段时,我希望它发生.但是当" focusLost()",事件没有得到最后一个值时,它会得到之前的值.
例如; tf1为tf20,最初为0.我写了2 tf1,当when focusLost()(tf1+tf2)变为0时,当我改变其中任何一个时,结果变为2(前一个值)
如何获取focusLost上的最后一个值?
这是我的代码:
JFormattedTextField tf1,tf2;
NumberFormat format=NumberFormat.getNumberInstance();
tf1=new JFormattedTextField(format);
tf1.addFocusListener(this);
tf2=new JFormattedTextField(format);
tf2.addFocusListener(this);
Run Code Online (Sandbox Code Playgroud)
并且focusLost():
public void focusLost(FocusEvent e) {
if(tf1.getValue() == null) tf1.setValue(0);
if(tf2.getValue() == null) tf2.setValue(0);
//because if I dont set, it throws nullPointerException for tf.getValue()
BigDecimal no1 = new BigDecimal(tf1.getValue().toString());
BigDecimal no2 = new BigDecimal(tf2.getValue().toString());
System.out.println("total: " + (no1.add(no2)));
}
Run Code Online (Sandbox Code Playgroud) 我在使用Netbeans 7.2的JAVA中使用文本字段自动计算时遇到问题
我的问题是我是否会在文本字段中输入数值,即(入场费,月费,运费等)自动添加,然后在文本字段中输入数值,即(会费)自动从上面的自动添加中减去,然后再单击提交用于在数据库中插入总值的按钮,以便在单击"提交"按钮之前如何在"文本字段(总计)"中获取这些数值的结果.
请检查快照:
图片http://s14.postimage.org/95zxgp575/image.jpg
我的源代码:
try
{
String insrt = "Insert into fee (admission, monthly, transport, dues, total) values (?, ?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(insrt);
pstmt.setString(1, adm_fee.getText());
pstmt.setString(2, mnth_fee.getText());
pstmt.setString(3, trnsprt_fee.getText());
pstmt.setString(4, dues_fee.getText());
pstmt.setString(5, total_fee.getText());
pstmt.executeUpdate();
JOptionPane.showMessageDialog(null,"Record successfully inserted");
}
catch (Exception exp)
{
JOptionPane.showMessageDialog(null, exp);
}
Run Code Online (Sandbox Code Playgroud)