Jtextarea错误,不会显示结果

时间:2015-03-08 08:57:05

标签: java

我的JTextArea有问题。我是编程新手。我想在jtextarea上超过所有通过和失败的成绩。构建时没有错误,但是当我运行程序时,会出现一条长消息,显示对话框中的错误,并且未输出成绩。

public static void main (String [] args) {
    String a;
    String output = "Passed\tFailed";
    int array [] = new int [20];
    int passed = 0, failed = 0;

    JTextArea outputA = new JTextArea();

    for (int counter = 0; counter < array.length; counter++) {
        a = JOptionPane.showInputDialog("Enter Student " + (counter + 1) + " Score: ");
        array[counter] = Integer.parseInt(a);
    }

    for (int counter1 = 0; counter1 < array.length ; counter1++) {
        if (array[counter1] >= 75) {
            passed += 1;
            output += array[counter1] + "\t";
        }
        else  {
            failed += 1;
            output += array[counter1] + "\n";
        }
    }

    outputA.setText(output);

    JOptionPane.showMessageDialog(  null, 
                                    outputA + "\n" + 
                                    "Number of students who passed: " + passed + "\n" + 
                                    "Number of students who failed: " + failed);
}

1 个答案:

答案 0 :(得分:0)

您将outputA作为文本放在邮件中。 outputA是一个JTextArea。因此,您将textArea.toString()作为消息放在JOptionPane中,这就是您所看到的内容。

outputA.getText()作为消息放入JOptionPane

JOptionPane.showMessageDialog(null, outputA.getText() + "\n" + "Number of students who passed: " + passed +
                                    "\n" + "Number of students who failed: " + failed);

或者简单地将output添加为窗格

的消息
JOptionPane.showMessageDialog(null, output + "\n" + "Number of students who passed: " + passed +
                                    "\n" + "Number of students who failed: " + failed);