我试图弄清楚如何编写一个try和catch语句来验证我的字符串,以显示当你运行程序时,你会看到一条错误消息,告诉用户你没有输入任何内容。
以下是代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Q2L04 extends JFrame {
private JButton q2l;
private JButton L2Q;
private JButton exit;
private JPanel panel;
private final int WINDOW_WIDTH=500;
private final int WINDOW_HEIGHT=300;
private JTextField QuartstoLiters;
private JTextField Answer;
public Q2L04() {
super("Q2L04");
setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
QuartstoLiters=new JTextField(10);
Answer= new JTextField(10);
q2l= new JButton("Q2L");
L2Q=new JButton("L2Q");
exit=new JButton("EXIT");
JPanel buttonsPanel=new JPanel();
buttonsPanel.setLayout(new GridLayout(2,0));
buttonsPanel.add(q2l);
buttonsPanel.add(L2Q);
setLayout(new BorderLayout());
add(buttonsPanel, BorderLayout.WEST);
add(exit,BorderLayout.EAST);
add(QuartstoLiters,BorderLayout.NORTH);
add(Answer,BorderLayout.SOUTH);
panel=new JPanel();
panel.setBackground(Color.YELLOW);
add(panel);
setVisible(true);
q2l.addActionListener(new QuartstoLiters());
exit.addActionListener(new ExitButton());
L2Q.addActionListener(new LiterstoQuarts());
以下是动作侦听器
private class QuartstoLiters implements ActionListener {
public void actionPerformed (ActionEvent e) {
double math = 0;
String userQuarts=QuartstoLiters.getText();
int Stringtoint=Integer.parseInt(userQuarts);
double liters=0.946353;
// the math to conver quartstoliters
// 1 quart =0.946353 liters
// quarts * 0.946353= numbers of liters
math=Stringtoint*liters;
// convert the double to a string
Answer.setText(Double.toString(math));
这是try和catch语句
try
{
{
if (userQuarts!= null)
throw(new Exception());
}
}
catch (Exception exe){
JOptionPane.showMessageDialog(null,"No input data was entered","Error",JOptionPane.ERROR_MESSAGE);
return;
}
}
}
用户不会在输入QuartstoLiters文本字段中输入任何内容,然后会显示没有输入数据的错误消息。
答案 0 :(得分:2)
我不知道您的代码是什么样的。 这是我的完整测试代码。
我准备了两个版本的actionlistener类,QuartstoLiters
和QuartstoLiters2
。
前者是具有检查变量状态的版本(textfield-txtQuartstoLiters),后者使用try catch机制。当parseInt方法中的空字符串时抛出NumberFormatException。
package com.tobee.tests.swing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Q2L04 extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new Q2L04();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
private JButton q2l;
private JButton L2Q;
private JButton exit;
private JPanel panel;
private final int WINDOW_WIDTH = 500;
private final int WINDOW_HEIGHT = 300;
private JTextField txtQuartstoLiters;
private JTextField Answer;
public Q2L04() {
super("Q2L04");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
txtQuartstoLiters = new JTextField(10);
txtQuartstoLiters.setText("0");
Answer = new JTextField(10);
q2l = new JButton("Q2L");
L2Q = new JButton("L2Q");
exit = new JButton("EXIT");
JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new GridLayout(2, 0));
buttonsPanel.add(q2l);
buttonsPanel.add(L2Q);
setLayout(new BorderLayout());
add(buttonsPanel, BorderLayout.WEST);
add(exit, BorderLayout.EAST);
add(txtQuartstoLiters, BorderLayout.NORTH);
add(Answer, BorderLayout.SOUTH);
panel = new JPanel();
panel.setBackground(Color.YELLOW);
add(panel);
setVisible(true);
q2l.addActionListener(new QuartstoLiters2());
//exit.addActionListener(new ExitButton());
//L2Q.addActionListener(new LiterstoQuarts());
}
class QuartstoLiters implements ActionListener {
public void actionPerformed(ActionEvent e) {
double math = 0;
int Stringtoint = 0;
final double liters = 0.946353;
String userQuarts = txtQuartstoLiters.getText();
if(!userQuarts.equals(""))
{
Stringtoint = Integer.parseInt(userQuarts);
}
else
{
JOptionPane.showMessageDialog(null, "No input data was entered", "Error", JOptionPane.ERROR_MESSAGE);
}
//try {
// if (userQuarts == null) throw (new Exception());
//} catch (Exception exe) {
// JOptionPane.showMessageDialog(null, "No input data was entered", "Error", JOptionPane.ERROR_MESSAGE);
//
// return;
//}
// the math to conver quartstoliters
// 1 quart =0.946353 liters
// quarts * 0.946353= numbers of liters
math = Stringtoint * liters;
// convert the double to a string
Answer.setText(Double.toString(math));
}
}
class QuartstoLiters2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
double math = 0;
int Stringtoint = 0;
final double liters = 0.946353;
String userQuarts = txtQuartstoLiters.getText();
try {
Stringtoint = Integer.parseInt(userQuarts);
} catch (NumberFormatException exe) {
if(userQuarts.equals(""))
JOptionPane.showMessageDialog(null, "Empty string not allowed", "Number Error", JOptionPane.ERROR_MESSAGE);
else
JOptionPane.showMessageDialog(null, "Only number is allowed", "Number Error", JOptionPane.ERROR_MESSAGE);
return;
}
// the math to conver quartstoliters
// 1 quart =0.946353 liters
// quarts * 0.946353= numbers of liters
math = Stringtoint * liters;
// convert the double to a string
Answer.setText(Double.toString(math));
}
}
}
您可以通过两种情况对文本字段值进行验证检查。
try {
Stringtoint = Integer.parseInt(userQuarts);
} catch (NumberFormatException exe) {
if(userQuarts.equals(""))
JOptionPane.showMessageDialog(null, "Empty string not allowed", "Number Error", JOptionPane.ERROR_MESSAGE);
else
JOptionPane.showMessageDialog(null, "Only number is allowed", "Number Error", JOptionPane.ERROR_MESSAGE);
return;
}
它将显示一个空字符串或不显示带有if语句的数字消息,具体取决于用户输入。