我想自动减去JTextInput
值,并希望在另一个文本字段中减去该值
Q值。我怎样才能做到这一点?
这是我的jtextfield代码:
public class Showdata1 extends JFrame
{
public Showdata1()
{
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Container c = getContentPane();
c.setLayout( new FlowLayout());
// setUndecorated(true);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(0,0,screenSize.width, screenSize.height);
opening_km= new JLabel("Opening Km");
opening_km.setBounds(0,280,258,30);
opening_km.setFont(new java.awt.Font("Times New Roman", 1,25)); // NOI18N
c.add(opening_km);
opening_kms= new JTextField("");
opening_kms.setBounds(200,280,258,30);
opening_kms.setFont(new java.awt.Font("Times New Roman", 1,25)); // NOI18N
c.add(opening_kms);
closing_km= new JLabel("Closing Km");
closing_km.setBounds(500,280,258,30);
closing_km.setFont(new java.awt.Font("Times New Roman", 1,25)); // NOI18N
c.add(closing_km);
closing_kms= new JTextField("");
closing_kms.setBounds(700,280,258,30);
closing_kms.setFont(new java.awt.Font("Times New Roman", 1,25)); // NOI18N
c.add(closing_kms);
totalkm= new JLabel("Total Km");
totalkm.setBounds(0,330,258,30);
totalkm.setFont(new java.awt.Font("Times New Roman", 1,25)); // NOI18N
c.add(totalkm);
totalkms= new JTextField("");
totalkms.setBounds(200,330,258,30);
totalkms.setFont(new java.awt.Font("Times New Roman", 1,25)); // NOI18N
c.add(totalkms);
setLayout(null);
}
public static void main( String[] args )
{
final Showdata1 frame = new Showdata1();
frame.setVisible(true);
}
}
答案 0 :(得分:0)
试试这个..
int val = Integer.parseInt(textField1.getText()) - Integer.parseInt(textField2.getText());
了解更多信息,请参阅https://docs.oracle.com/javase/tutorial/java/data/converting.html
创建一个这样的类..
public class MyTextField extends JTextField {
// whaterver you want...
public Integer getValue(){
if(getText().length() == 0)
return 0;
return Integer.parseInt(getText());
}
}
然后你可以通过调用getValue()
方法