秋前辈解决NumberFormatException: 数字格式化异常
package com.gem.swing;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLException;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import com.gem.dao.BaseDao;
import com.gem.dao.impl.BaseDaoImpl;
/***
* 主界面操作
* @author GYYue
*
*/
public class MainSwing implements ActionListener{
private static JFrame frame;
private static JLabel label, label1, label2;
private static JButton button, button1;
private static JTextField tf, tf2;
private static JTextArea ta;
private static JCheckBox jb, jb1;
private static ButtonGroup group;
public void run() {
// 构建frame界面
frame = new JFrame("员工基本信息");
// 构建label标签
label = new JLabel("用户名:");
label1 = new JLabel("性别:");
label2 = new JLabel("工资:");
// 构建按钮
button1 = new JButton("其他");
button1.addActionListener((ActionListener) this);
button = new JButton("提交");
button.addActionListener((ActionListener) this);
// 构建文字框
tf = new JTextField("", 20);
tf.setEditable(true);
tf.addActionListener((ActionListener) this);
// tf1 = new JTextField("",6);
tf2 = new JTextField("", 10);
tf2.addActionListener((ActionListener) this);
// 构建单选框
jb = new JCheckBox("男", true);
jb.addActionListener((ActionListener) this);
jb1 = new JCheckBox("女", false);
jb1.addActionListener((ActionListener) this);
// 构建组按钮
group = new ButtonGroup();
group.add(jb);
group.add(jb1);
// 构建输出文本框
ta = new JTextArea(100, 20);
ta.setBounds(20, 175, 290, 120);
// 标签绝对布局
label.setBounds(20, 20, 50, 30);
label1.setBounds(20, 60, 50, 30);
label2.setBounds(20, 100, 50, 30);
// 文字框据对布局
tf.setBounds(80, 20, 80, 30);
jb.setBounds(80, 60, 40, 30);
jb1.setBounds(120, 60, 40, 30);
tf2.setBounds(80, 100, 80, 30);
// 提交按钮绝对布局
button.setBounds(20, 150, 60, 25);
button1.setBounds(98, 150, 60, 25);
// 垂直滚动条
frame.add(label);
frame.add(label1);
frame.add(label2);
frame.add(tf);
frame.add(jb);
frame.add(jb1);
frame.add(tf2);
frame.add(ta);
frame.setLayout(null);
frame.setSize(350, 370);
frame.setLocation(500, 200);
frame.setVisible(true);
frame.add(button);
frame.add(button1);
frame.add(ta);
frame.addWindowListener(new WindowAdapter() {
@SuppressWarnings("unused")
public void WindowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args){
MainSwing MainSwing = new MainSwing();
MainSwing.run();
}
@Override
public void actionPerformed(ActionEvent e) {
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
BaseDao baseDao = new BaseDaoImpl();
baseDao.add(tf.getText(), jb.getText(), jb1.getText(),
tf2.getText());
String s1 = tf.getText();
String s4 = tf2.getText();
if ("".equals(s1)) {
JOptionPane.showMessageDialog(null, "提交失败,用户名不能为空!");
} else {
if ("".equals(s4)) {
JOptionPane.showMessageDialog(null, "提交失败,工资金额不能为空!",
"消息提示", JOptionPane.WARNING_MESSAGE);
} else {
for (int i = 0; i < s4.length(); i++) {
if (s4.charAt(i) < '0' || s4.charAt(i) > '9') {
JOptionPane.showMessageDialog(null,
"提交失败!工资必须是数字", "消息提示",
JOptionPane.WARNING_MESSAGE);
} else {
if (!jb.isSelected() && !jb1.isSelected()) {
JOptionPane.showMessageDialog(null,
"提交失败!性别必须填写", "消息提示",
JOptionPane.WARNING_MESSAGE);
}
JOptionPane.showMessageDialog(null, "提交成功",
"消息提示", JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
}
}
}
}
});
if(e.getSource()==jb){
ta.setText("用户名:" + tf.getText() + "\n" + "性别:" + jb.getText()
+ "\n" + "工资:" + tf2.getText());
}else{
if (e.getSource() == jb1) {
ta.setText("用户名:" + tf.getText() + "\n" + "性别:" + jb1.getText()
+ "\n" + "工资:" + tf2.getText());
}
}
}
}