当我运行程序时,我得到弹出框,当我输入number1或number2中的数字时,异常处理在我点击“输入”-key之前给出了“仅允许的数字”错误。它一直这样做,直到我在两个文本字段中填写了一个数字。除非我在文本字段中添加除数字以外的其他内容,否则不应该给我错误。 也。我无法从CM转换为INCH两次,它会再次给我错误。我必须在从CM转换为INCH和INCH转换为CM之间切换。 它转换得很好,所以问题必须是设置异常处理的问题?这就像它在我输入之前读取文本字段,因此它给了我一个错误,因为没有插入数字。
任何人都可以帮助我吗?提前谢谢!
//丹尼尔
我有两节课。
第1课的代码:
package inchcm;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class InchCM extends JFrame {
private JTextField num1 = new JTextField(8);
private JTextField num2 = new JTextField(8);
private JLabel l1 = new JLabel();
private JLabel l2 = new JLabel();
public InchCM() {
setLayout(new BorderLayout());
//panel 1 med GridLayout (2,0) til 2 x JTextField(8).
JPanel p1 = new JPanel(new GridLayout(2, 0));
p1.add(new JLabel(""));
p1.add(num1);
p1.add(new JLabel(""));
p1.add(num2);
JPanel p2 = new JPanel(new GridLayout(2, 0));
p2.add(new JLabel(" Inches"));
p2.add(l1);
p2.add(new JLabel(" Centimeters"));
p2.add(l2);
add(p1, BorderLayout.EAST);
add(p2, BorderLayout.WEST);
num1.addKeyListener(
new KeyAdapter() {
public void keyReleased(KeyEvent e) {
try {
double nummer1 = Double.parseDouble(num1.getText());
double nummer2 = Double.parseDouble(num2.getText());
Udregning udr = new Udregning(nummer1, nummer2);
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
num2.setText(String.format("%.2f", udr.inchtocm()));
}
}
//end tal i et af JTextFieldsne.
catch (NumberFormatException ex) {
System.out.println(ex);
JOptionPane.showMessageDialog(null,
"Only numbers allowed \nTry again",
"Input error", JOptionPane.ERROR_MESSAGE);
}
}
});
num2.addKeyListener(
new KeyAdapter() {
public void keyReleased(KeyEvent e) {
try {
//objekt af num1 og num2.
double nummer1 = Double.parseDouble(num1.getText());
double nummer2 = Double.parseDouble(num2.getText());
Udregning udr = new Udregning(nummer1, nummer2);
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
num1.setText(String.format("%.2f", udr.cmtoinch()));
}
}
catch (NumberFormatException ex) {
System.out.println(ex);
JOptionPane.showMessageDialog(null,
"Only numbers allowed \nTry againn",
"Input error", JOptionPane.ERROR_MESSAGE);
}
}
});
}
public static void main(String[] args) {
InchCM frame = new InchCM();
frame.pack();
frame.setTitle("InchCM");
frame.setSize(300, 130);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
第2课的代码: “计算类”
package inchcm;
public class Udregning {
private double num1 = 0;
private double num2 = 0;
Udregning() {
}
Udregning(double newnum1, double newnum2) {
setnum1(newnum1);
setnum2(newnum2);
}
private void setnum1(double newnum1) {
num1 = newnum1;
}
private void setnum2(double newnum2) {
num2 = newnum2;
}
public double inchtocm() {
double result;
result = num1 * 2.54;
return result;
}
public double cmtoinch() {
double result;
result = num2 * .39;
return result;
}
}
答案 0 :(得分:1)
正如我最近自己在这里学到的,没有必要KeyListener
来实现这种行为。您应该向ActionListener
添加JTextField
,当在文本字段内推送ENTER时将触发该KeyListener
。
如果要触发另一个键的操作,然后按ENTER键,则应使用键绑定而不是{{1}}。请参阅Swing tutorial
答案 1 :(得分:-1)
每次触发KeyReleased事件时都会执行try catch块 - 即每次按键。在尝试解析输入数字之前,您需要更改它以检查VK_ENTER:
try {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
double nummer1 = Double.parseDouble(num1.getText());
double nummer2 = Double.parseDouble(num2.getText());
Udregning udr = new Udregning(nummer1, nummer2);
num2.setText(String.format("%.2f", udr.inchtocm()));
}
}
//end tal i et af JTextFieldsne.
catch (NumberFormatException ex) {
System.out.println(ex);
JOptionPane.showMessageDialog(null,
"Only numbers allowed \nTry again",
"Input error", JOptionPane.ERROR_MESSAGE);
}
答案 2 :(得分:-1)
我同意@mcfinnigan的回答,但是在num1
的事件处理程序中你不应该对num2
做出反应。如果您想更新每个密钥:
num1.addKeyListener(
new KeyAdapter() {
public void keyReleased(KeyEvent e) {
try {
double nummer1 = Double.parseDouble(num1.getText());
// Synchronize both fields:
try {
double nummer2 = Double.parseDouble(num2.getText());
Udregning udr = new Udregning(nummer1, nummer2);
//if (e.getKeyCode() == KeyEvent.VK_ENTER) {
num2.setText(String.format("%.2f", udr.inchtocm()));
//}
} catch (NumberFormatException ex2) { }
}
//end tal i et af JTextFieldsne.
catch (NumberFormatException ex) {
System.out.println(ex);
JOptionPane.showMessageDialog(null,
"Only numbers allowed \nTry again",
"Input error", JOptionPane.ERROR_MESSAGE);
}
}
});