0

我在使用这个 GUI 时遇到了一些麻烦。我是制作 GUI 的新手,所以如果它是基本的东西,我很抱歉问。我似乎无法在谷歌的任何地方找到我的问题的答案。我遇到的问题是,当我单击任何一个按钮时,当任何一个文本字段为空时都会出现错误。到目前为止,这是我的代码

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("");
               }
 }
}
4

2 回答 2

2

wotking,但你不测试 if JTextField.isEmpty,然后解析抛出正确的异常,而不是使用

  • JFormattedTextField使用NumberFormatter,则默认值是 0(零)

  • JSpinnerSpinnerNumberModel则默认值是 0(零)

  • 通过过滤非数字字符添加DocumentFilter到纯JTextField文本,但仍需要测试 if JTextField.isEmpty()

于 2013-03-30T15:43:51.627 回答
2

我怀疑这是因为这些字段:

int ageNum = Integer.parseInt(age.getText().toLowerCase());
double heightNum = Double.parseDouble(height.getText().toLowerCase());
double weightNum = Double.parseDouble(weight.getText().toLowerCase());

如果这些字段中的任何一个为空或包含不正确的值,解析将抛出异常;确保检查它们是否包含(有效)值。例如,使用JFormattedTextField

于 2013-03-30T15:45:01.597 回答