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

class NICCode extends JFrame {

  NICCode() {
    setSize(600, 250);
    setResizable(false);
    setDefaultCloseOperation(3);
    setLocationRelativeTo(null);

    // JPanel
    JPanel labelPanel = new JPanel(new FlowLayout(1));
    JPanel leftBodyPanel = new JPanel(new GridLayout(3, 1));
    JPanel bodyPanel = new JPanel(new GridLayout(3, 1));
    JPanel buttonPanel = new JPanel(new FlowLayout(2));
    JPanel textFieldPanel = new JPanel(new FlowLayout(0));

    // JLabel
    JLabel titleLabel = new JLabel("Find Your Birthday By NIC");
    titleLabel.setFont(new Font("", 1, 25));
    JLabel myLabel = new JLabel("CSG");
    myLabel.setFont(new Font("", 1, 10));

    JLabel enterNicLabel = new JLabel("Enter Your NIC :");
    JLabel yourBirthDayLabel = new JLabel("Your Birth Day :");
    JLabel yourGenderLabel = new JLabel("Gender :");

    JLabel printBirthDayLabel = new JLabel("Your Birth Day");
    JLabel printGenderLabel = new JLabel("Your Gender");

    // JTextField
    JTextField nicText = new JTextField(25);
    nicText.setText("920000000V");

    // JButton
    JButton searchAgainButton = new JButton("Search Again");
    JButton exitButton = new JButton("Exit");

    // adds
    add("North", labelPanel);
    add("West", leftBodyPanel);
    add("South", buttonPanel);
    add(bodyPanel);

    labelPanel.add(titleLabel);
    leftBodyPanel.add(enterNicLabel);
    leftBodyPanel.add(yourBirthDayLabel);
    leftBodyPanel.add(yourGenderLabel);
    textFieldPanel.add(nicText);
    bodyPanel.add(textFieldPanel);
    bodyPanel.add(printBirthDayLabel);
    bodyPanel.add(printGenderLabel);
    buttonPanel.add(myLabel);
    buttonPanel.add(searchAgainButton);
    buttonPanel.add(exitButton);

    setVisible(true);
    // pack();

    String yearText = nicText.substring(0, 2);
    String dateText = nicText.substring(2, 5);
    String sex = "";
    int year = Integer.parseInt(yearText);
    int date = Integer.parseInt(dateText);
    int month = 0;
    if (date > 500) {
      sex = "Feamale";
      date -= 500;
    } else {
      sex = "Male";
    }
    int datesOfMonths[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    for (int i = 0; i < 12; i++) {
      date = date - datesOfMonths[i];
      month = i;
      if (date < datesOfMonths[i + 1]) {
        break;
      }
    }
    if (month > 0) {
      month += 1;
    }
  }
}

我刚刚创建了一个从 nic 查找生日的程序。我无法修复这个编译错误。我创建了一个 JTextField 来获取 Nic,然后我添加了子字符串方法来获取查找 nic 所需的数字。不幸的是,子字符串方法找不到 textField。它显示错误为“找不到符号”。这是一个文本字段。为什么方法找不到那个TextField。?

4

2 回答 2

1

消息:

cannot find symbol

不是指文本字段,而是指substring(..)不存在的方法JTextField。但是像..

textField.getText().substring(...);

..可能会起作用,因为getText()返回 aStringString具有该方法。

于 2013-01-13T05:40:38.960 回答
0

改变

String yearText = nicText.substring(0, 2);
String dateText = nicText.substring(2, 5);

String yearText = nicText.getText().substring(0, 2);
String dateText = nicText.getText().substring(2, 5);
于 2013-01-13T05:40:59.337 回答