2013-08-12 44 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(); 

为你创造了他们作为字符串已经