Java 用户在JFrame中不输入任何内容,并获取错误消息
我试图找出如何编写一个try-and-catch语句来验证我的字符串,以表明当运行程序时,您会看到一条错误消息,告诉用户您没有输入任何内容 代码如下:Java 用户在JFrame中不输入任何内容,并获取错误消息,java,user-interface,jframe,Java,User Interface,Jframe,我试图找出如何编写一个try-and-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; privat
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
{
{
if (userQuarts!= null)
throw(new Exception());
}
}
catch (Exception exe){
JOptionPane.showMessageDialog(null,"No input data was entered","Error",JOptionPane.ERROR_MESSAGE);
return;
}
}
}
用户没有在input QuartsToolls文本字段中输入任何内容,然后错误消息说没有输入数据。我不知道您的代码是什么样子。 这是我给你的完整测试代码 我准备了两个版本的actionlistener类,
QuartstoLiters
和QuartstoLiters2
前者是一个具有检查变量状态(textfield txtQuartstoll)的版本,后者使用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语句的数字消息,具体取决于用户输入
和输出图像。
好的,我为您添加代码,这是有效的…告诉我区别。当用户按下按钮时,文本字段需要为空,然后错误立即显示。我必须输入零才能弹出错误消息。@Super\u Fly,检查我刚刚修改的Quartstollals类…再见,抱歉,奇怪的是,我的老师要求我做一个“尝试-抓住”的陈述,而我试图查找更多使用“尝试-抓住”陈述的答案,但我猜这不起作用。但是谢谢你的帮助@当然,try-catch语句有时很有用,您不需要检查文本字段的值是否为空。我添加了一个QuartstoLiters2类,该类使用该语句。