1

我正在准备一个简单的系统作为我的任务,而且我面临一个问题,我不知道如何获取getTextString 的值。我知道如何使用整数,但不知道如何使用字符串。

如果我想获得一个整数值,我会在我的代码中使用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();
    }
}
4

2 回答 2

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();
于 2013-08-12T07:18:22.097 回答
2

要将字符串分配给字符串,您需要做的就是

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

因为您已经将它们创建为字符串

于 2013-08-12T07:22:55.510 回答