如何制作选择性 gettext()
How to make a selective gettext()
我正在尝试做一个货币兑换程序,但如果第二个输入(这里的 s1)为空,程序会给出 NumberFormatException:Empty String error
。它在 first(s here) 为空时起作用。所以我想知道是否有另一种方法可以通过一个按钮使其具有选择性。为什么当第二个字段为空时它不工作?
public class kanvas1 implements ActionListener,WindowListener{
private JTextField tf;
private JTextField tf1;
private JTextField tf2;
private JTextField tf3;
private JLabel lb;
private JLabel lb1;
private JButton bt;
private JButton bt1;
public kanvas1()
{
tf=new JTextField();
tf1=new JTextField();
tf2=new JTextField();
tf3=new JTextField();
lb=new JLabel("$");
lb1=new JLabel("TL");
bt=new JButton("Cevir");
bt1=new JButton("Sıfırla");
pencere();
}
public void pencere() {
tf.setBounds(50,20,150,50);
tf1.setBounds(50,80, 150, 50);
tf2.setBounds(220,20,150,50);
tf3.setBounds(220,80,150,50);
lb.setBounds(30,20,20, 50);
lb1.setBounds(30,80,20,50);
bt.setBounds(400,20,100, 50);
bt1.setBounds(400,80,100,50);
bt.addActionListener(this);
bt1.addActionListener(this);
JFrame ab=new JFrame();
ab.setVisible(true);
ab.setSize(600,200);
ab.setLayout(null);
ab.add(tf);ab.add(tf1);ab.add(tf2);ab.add(tf3);ab.add(bt);ab.add(bt1);ab.add(lb);ab.add(lb1);
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String s=tf.getText(); //problem is here
double a=Double.parseDouble(s);
double c=a*5.44;
String result=String.valueOf(c);
tf2.setText(result);
}
});
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String s1=tf1.getText();
//and here
double b=Double.parseDouble(s1);
double d=b*0.18;
String result1=String.valueOf(d);
tf3.setText(result1);
}
});
bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
tf.setText("");
tf1.setText("");
tf2.setText("");
tf3.setText("");
}
});
}
@Override
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
@Override
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public static void main(String args[]) {
new kanvas1();
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
你可以像这样在解析之前添加一个检查:
if (s1.isEmpty())
return;
它失败了,因为 Double.parseDouble(String arg)
试图将作为参数传递的字符串转换为该字符串表示的 Double。这意味着 "12"、"12.0" 等字符串 是有效输入,而 "12a"、"ac"、"" 等字符串[= =28=]。注定要失败;因为,毕竟 12a
不是双精度数(甚至不是数字!)。
如果文本字段为空并且您执行 getText()
,您将检索到的是 ""
(空字符串)而不是 null
。
因此,如果您想做出任何有条件的决定,请将您的条件基于检索到的文本 empty i.e. ""
而不是 null
不用为同一个按钮写两次 addActionListener
,您可以写一个 addActionListener
。
此外,避免将任何字段留空,您将得到 NumberFormatException
,因为您的 JTextField
之一给出了空白值或任何其他非双精度值。使用 Double.parseDouble("12.34") 进行解析时,应始终包含字符串格式的双精度值。解析其他值会抛出 NumberFormatException
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String s=tf.getText(); //problem is here
double a=Double.parseDouble(s);
double c=a*5.44;
String result=String.valueOf(c);
tf2.setText(result);
}
});
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String s1=tf1.getText();
//and here
double b=Double.parseDouble(s1);
double d=b*0.18;
String result1=String.valueOf(d);
tf3.setText(result1);
}
});
将上面的代码替换为:
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String s=tf.getText();
double a=Double.parseDouble(s);
double c=a*5.44;
String result=String.valueOf(c);
tf2.setText(result);
String s1=tf1.getText();
double b=Double.parseDouble(s1);
double d=b*0.18;
String result1=String.valueOf(d);
tf3.setText(result1);
}
});
我正在尝试做一个货币兑换程序,但如果第二个输入(这里的 s1)为空,程序会给出 NumberFormatException:Empty String error
。它在 first(s here) 为空时起作用。所以我想知道是否有另一种方法可以通过一个按钮使其具有选择性。为什么当第二个字段为空时它不工作?
public class kanvas1 implements ActionListener,WindowListener{
private JTextField tf;
private JTextField tf1;
private JTextField tf2;
private JTextField tf3;
private JLabel lb;
private JLabel lb1;
private JButton bt;
private JButton bt1;
public kanvas1()
{
tf=new JTextField();
tf1=new JTextField();
tf2=new JTextField();
tf3=new JTextField();
lb=new JLabel("$");
lb1=new JLabel("TL");
bt=new JButton("Cevir");
bt1=new JButton("Sıfırla");
pencere();
}
public void pencere() {
tf.setBounds(50,20,150,50);
tf1.setBounds(50,80, 150, 50);
tf2.setBounds(220,20,150,50);
tf3.setBounds(220,80,150,50);
lb.setBounds(30,20,20, 50);
lb1.setBounds(30,80,20,50);
bt.setBounds(400,20,100, 50);
bt1.setBounds(400,80,100,50);
bt.addActionListener(this);
bt1.addActionListener(this);
JFrame ab=new JFrame();
ab.setVisible(true);
ab.setSize(600,200);
ab.setLayout(null);
ab.add(tf);ab.add(tf1);ab.add(tf2);ab.add(tf3);ab.add(bt);ab.add(bt1);ab.add(lb);ab.add(lb1);
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String s=tf.getText(); //problem is here
double a=Double.parseDouble(s);
double c=a*5.44;
String result=String.valueOf(c);
tf2.setText(result);
}
});
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String s1=tf1.getText();
//and here
double b=Double.parseDouble(s1);
double d=b*0.18;
String result1=String.valueOf(d);
tf3.setText(result1);
}
});
bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
tf.setText("");
tf1.setText("");
tf2.setText("");
tf3.setText("");
}
});
}
@Override
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
@Override
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public static void main(String args[]) {
new kanvas1();
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
你可以像这样在解析之前添加一个检查:
if (s1.isEmpty())
return;
它失败了,因为 Double.parseDouble(String arg)
试图将作为参数传递的字符串转换为该字符串表示的 Double。这意味着 "12"、"12.0" 等字符串 是有效输入,而 "12a"、"ac"、"" 等字符串[= =28=]。注定要失败;因为,毕竟 12a
不是双精度数(甚至不是数字!)。
如果文本字段为空并且您执行 getText()
,您将检索到的是 ""
(空字符串)而不是 null
。
因此,如果您想做出任何有条件的决定,请将您的条件基于检索到的文本 empty i.e. ""
而不是 null
不用为同一个按钮写两次 addActionListener
,您可以写一个 addActionListener
。
此外,避免将任何字段留空,您将得到 NumberFormatException
,因为您的 JTextField
之一给出了空白值或任何其他非双精度值。使用 Double.parseDouble("12.34") 进行解析时,应始终包含字符串格式的双精度值。解析其他值会抛出 NumberFormatException
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String s=tf.getText(); //problem is here
double a=Double.parseDouble(s);
double c=a*5.44;
String result=String.valueOf(c);
tf2.setText(result);
}
});
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String s1=tf1.getText();
//and here
double b=Double.parseDouble(s1);
double d=b*0.18;
String result1=String.valueOf(d);
tf3.setText(result1);
}
});
将上面的代码替换为:
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String s=tf.getText();
double a=Double.parseDouble(s);
double c=a*5.44;
String result=String.valueOf(c);
tf2.setText(result);
String s1=tf1.getText();
double b=Double.parseDouble(s1);
double d=b*0.18;
String result1=String.valueOf(d);
tf3.setText(result1);
}
});