储物柜程序...单击按钮后显示方法

时间:2014-02-03 00:37:48

标签: java output

我正在做1000个储物柜程序。我有一个问题,代码函数和system.out.println它将显示我希望它显示的方式,但在文本区域它不会显示。请帮助我,评论将是一个很大的帮助。在此先感谢!

String output = "";
int numOpen = 0;
String numCount = "";

(上面的代码在程序顶部声明)

private int runLockers(int Lockers)
{
    final int numLockers = Integer.valueOf(numLockerTextField.getText());
    int numClosed = Integer.valueOf(numLockerTextField.getText());
    boolean lockers[] = new boolean[numLockers+1];

    for(int studentNum = 1; studentNum <= numLockers; studentNum++)
    {
        for(int locker = studentNum; locker <= numLockers; locker+=studentNum)
        {
            lockers[locker]=!lockers[locker];
        }
    }

   for(int count=1; count <= numLockers; count++)
   {
       if(lockers[count])
       {
           numOpen++;
           numCount+="Locker: "+count+"\n";
           System.out.println("Open Lockers:" + numOpen);
           System.out.println(numCount);
           output+="Open Lockers:" + numOpen + numCount;
       }
   }
    //outputTextArea.setText(output);
    return(numOpen);
}

private void simulateBtnActionPerformed (ActionEvent event)
{
    outputTextArea.setText(output);
    outputTextArea.setText("There are "+ Integer.valueOf(numLockerTextField.getText()) +" lockers, and "+ runLockers(0) +" are left open.");
}

这是总计按钮代码。它在方法之前:

simulateButton = new JButton(); //declaring my new JButton
simulateButton.setText("Simulate"); //setting the text of the JButton
simulateButton.setBounds(160,500,100,30); //setting the bounds to which the Button is set to; The int x&y also the width&height
contentPane.add(simulateButton); //adding label to contentPane so it will be visible to user
simulateButton.addActionListener // adding an interface used to listen for an action event
            (
                    new ActionListener()
                    {
                        public void actionPerformed( ActionEvent event )
                        {
                            simulateBtnActionPerformed(event);
                        }
                    } // referencing what action is to be perform when button is pressed
            );

以下是打印出来的示例:

Open Lockers:1
Locker: 1

Open Lockers:2
Locker: 1
Locker: 4

Open Lockers:3
Locker: 1
Locker: 4
Locker: 9

这是文本区域中唯一出现的行:

outputTextArea.setText(&#34;有&#34; + Integer.valueOf(numLockerTextField.getText())+&#34;储物柜,&#34; + runLockers(0)+&#34;保持开放。&#34;);

...看起来像

There are 1000 lockers, and 31 are left open.

这就是文本区应该是这样的:

Open Lockers:1
Locker: 1

Open Lockers:2
Locker: 1
Locker: 4

Open Lockers:3
Locker: 1
Locker: 4
Locker: 9

(.... that goes to 1000)

There are 1000 lockers, and 31 are left open.

1 个答案:

答案 0 :(得分:0)

您正在将outputTextArea的文本设置为output,然后立即将其替换为其他文本("There are ..."字符串)。您可能希望附加字符串而不是将文本设置两次。也许是这样的:

private void simulateBtnActionPerformed (ActionEvent event)
{
    outputTextArea.setText(output
        + "\nThere are "+ Integer.valueOf(numLockerTextField.getText()) +" lockers, and "+ runLockers(0) +" are left open.");
}