2013-08-12 46 views
2

我準備一個簡單的系統,我的任務,我面對的,我不知道如何getText字符串的值存在問題的價值。我知道如何用整數,而不是字符串。無法gettext的字符串

我在我的代碼中使用Integer.parseInt(t2.getText());如果我想得到一個整數值 我已經將我的代碼添加到此代碼中。

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class tollRate extends JFrame implements ActionListener { 

    JLabel L1 = new JLabel("Please insert your origin (in capital letter)"); 
    JTextField t1 = new JTextField(20); 
    JLabel L2 = new JLabel("Please insert your destination (in capital letter)"); 
    JTextField t2 = new JTextField(20); 
    JLabel L3 = new JLabel("Your vehicle class"); 
    JTextField t3 = new JTextField(1); 
    JButton b1 = new JButton("Calculate"); 
    JButton b2 = new JButton("Exit"); 
    JLabel L4 = new JLabel("Class 0 : Motorcycles, bicycles, or vehicles with " 
      + "2 or less wheels" + "\nClass 1 : Vehicles wit 2 axles and 3 " 
      + "or 4 wheels excluding taxis" + "\nClass 2 : Vehicles with 2 " 
      + "axles and 5 or 6 wheels excluding busses" + "\n Class 3 : " 
      + "Vehicles with 3 or more axles" + "\nClass 4 : Taxis" 
      + "\nClass 5 : Buses"); 
    JPanel p1 = new JPanel(); 
    JPanel p2 = new JPanel(); 
    JPanel p3 = new JPanel(); 
    JPanel p4 = new JPanel(); 
    JPanel p5 = new JPanel(); 
    String i, j, k; 

    tollRate() { 
     JFrame a = new JFrame(); 
     setTitle("Highway Toll Rates System"); 
     setSize(600, 400); 
     setVisible(true); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLayout(new GridLayout(5, 2)); 
     p1.setLayout(new GridLayout(1, 2)); 
     p1.add(L1); 
     p1.add(t1); 
     p2.setLayout(new GridLayout(1, 2)); 
     p2.add(L2); 
     p2.add(t2); 
     p3.setLayout(new GridLayout(1, 2)); 
     p3.add(L3); 
     p3.add(t3); 
     p4.setLayout(new FlowLayout()); 
     p4.add(b1); 
     p4.add(b2); 
     p5.setLayout(new FlowLayout()); 
     p5.add(L4); 
     add(p1); 
     add(p2); 
     add(p3); 
     add(p4); 
     add(p5); 
     b1.addActionListener(this); 
     b2.addActionListener(this); 
    } 

    public void actionPerformed(ActionEvent a) { 
     Object source = a.getSource(); 
     if (source == b2) { 
      this.dispose(); 
     } else if (source == b1) { 
      i = Integer.parseInt(t1.getText()); 
      j = Integer.parseInt(t2.getText()); 
      k = Integer.parseInt(t3.getText()); 
     } 
    } 

    public static void main(String[] args) { 
     tollRate a = new tollRate(); 
    } 
} 
+1

試圖找到一個字符串作爲一個字符串?只需使用'getText()'返回的字符串,你不需要做任何事情。 – kiheru

回答

2

的所有String i, j, k;

 i = Integer.parseInt(t1.getText()); 
    j = Integer.parseInt(t2.getText()); 
    k = Integer.parseInt(t3.getText()); 

首先這是不對的。您正在爲int分配字符串。先糾正它們。如果需要int值,最好使用

int i, j, k;並使用trim()來避免額外的空間。

 i = Integer.parseInt(t1.getText().trim()); 
    j = Integer.parseInt(t2.getText().trim()); 
    k = Integer.parseInt(t3.getText().trim()); 

在你的情況下使用如下

 i = t1.getText(); 
     j = t2.getText(); 
     k = t3.getText(); 
+0

謝謝你,我可以看到,我做錯了,但我不知道如何將它分配給字符串。我該怎麼辦?這是我現在堅持的唯一部分。我想要得到的文字是字符串。所以,我相信這個聲明是正確的。 只是該方法的getText是錯誤的。你能幫助更多嗎? –

+1

@NoIdeaForName我想你可以閱讀我的答案。你不看我回答的最後部分嗎? –

+0

@Ruchira你我的意見後編輯它...好吧這是很好的,但他已經做字符串I,J,K;在成員部分 –

3

將一個字符串分配給一個字符串的所有你需要做的是

 i = t1.getText(); 
    j = t2.getText(); 
    k = t3.getText(); 

爲你創造了他們作爲字符串已經