2013-03-30 54 views
0

即時通訊這個GUI有點麻煩。林新的製作圖形用戶界面,所以如果它的東西基本我很抱歉問。我似乎無法在谷歌的任何地方找到我的問題的答案。 即時通訊的問題是,當我點擊任何按鈕時,我得到一個錯誤,當任何一個文本框留空。這裏是我到目前爲止的代碼JButton不能使用textfield

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測試,然後解析拋出當前的異常,而不是說用

  • JFormattedTextFieldNumberFormatte r,則默認瓦魯爾足球俱樂部是0(零)

  • JSpinnerSpinnerNumberModel,則默認值瓦魯爾足球俱樂部是0(零)

  • 添加DocumentFilter平淡JTextField與過濾non_numeric字符,但仍需要測試,如果JTextField.isEmpty(

2

我懷疑這是因爲這些領域:

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

解析將拋出一個異常如果這些字段中的任何一個爲空,或者包含不正確的值;確保你檢查它們包含一個(有效的)值。例如,使用JFormattedTextField