import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
public class MainGUI extends JFrame implements ActionListener{
ArrayList<Object> list;
JTextField name, surname, age, height, weight;
JComboBox<String> sportList, gender;
JButton btnSave;
JButton btnExit;
JButton btnclrtxt;
/**
* @param args
*/
public MainGUI() {
super("Adding members");
this.setLayout(new BorderLayout());
JPanel panel = new JPanel(new FlowLayout());
name = new JTextField(10);
surname = new JTextField(10);
age = new JTextField(10);
height = new JTextField(5);
weight = new JTextField(10);
gender = new JComboBox<String>(new String[] { "Male", "Female" });
sportList = new JComboBox<String>(new String[] { "Football",
"HandBall", "BasketBall", "Rugby", "Hockey", "Tennis" });
panel.add(new JLabel("Name:"));
panel.add(name);
panel.add(new JLabel("Surname:"));
panel.add(surname);
panel.add(new JLabel("Age:"));
panel.add(age);
panel.add(new JLabel("Gender:"));
panel.add(gender);
panel.add(new JLabel("Height:"));
panel.add(height);
panel.add(new JLabel("Weight:"));
panel.add(weight);
panel.add(new JLabel("Sport Speciality:"));
panel.add(sportList);
btnSave = new JButton("Save member");
panel.add(btnSave);
btnSave.addActionListener(this);
btnclrtxt = new JButton("Clear text");
panel.add(btnclrtxt);
btnclrtxt.addActionListener(this);
btnExit = new JButton("Exit");
panel.add(btnExit);
btnExit.addActionListener(this);
this.pack();
this.add(panel);
this.setSize(400, 300);
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MainGUI();
}
public void actionPerformed(ActionEvent e) {
String namestr = name.getText().toLowerCase();
String surnameStr = surname.getText().toLowerCase();
int ageNum = Integer.parseInt(age.getText().toLowerCase());
double heightNum = Double.parseDouble(height.getText().toLowerCase());
double weightNum = Double.parseDouble(weight.getText().toLowerCase());
String sportsStr = sportList.getSelectedItem().toString();
String genderStr = gender.getSelectedItem().toString();
if(e.getSource()==btnSave) {
list = new ArrayList<Object>();
list.add("Name is "+namestr);
list.add("Surname is "+surnameStr);
list.add("Age is "+ageNum);
list.add("Height is "+heightNum);
list.add("Weight is "+weightNum);
list.add("Sport "+sportsStr);
list.add("Gender "+genderStr);
FileRead.writeFile(list);
System.out.println("Done!");
}
if(e.getSource()==btnExit) {
dispose();
System.exit(0);
}
if(e.getSource()==btnclrtxt) {
name.setText("");
surname.setText("");
age.setText("");
height.setText("");
weight.setText("");
}
}
}
答案 0 :(得分:2)
wotking,但你没有测试JTextField.isEmpty
,然后解析引发了正确的异常,而不是使用
JFormattedTextField
NumberFormatte
r,则默认值为0(零)
JSpinner
SpinnerNumberModel
,则默认值为0(零)
将DocumentFilter
添加到普通JTextField
并过滤非数字字符,但仍需要测试是否JTextField.isEmpty(
)
答案 1 :(得分:2)
我怀疑这是因为这些领域:
int ageNum = Integer.parseInt(age.getText().toLowerCase());
double heightNum = Double.parseDouble(height.getText().toLowerCase());
double weightNum = Double.parseDouble(weight.getText().toLowerCase());
如果这些字段中的任何一个为空,或者包含不正确的值,则解析将抛出异常;确保检查它们是否包含(有效)值。例如,使用JFormattedTextField。