0

我在文本字段中显示数组 j 的值时遇到问题。运行时仅显示 1 个元素。

import java.util.Collections;
import java.util.ArrayList;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class SwingCounter extends JFrame implements ActionListener {

    // use Swing's JTextField instead of AWT's TextField
    private JTextField tfCount;
    private int counterValue = 0, numbers=0, j=0;

    public SwingCounter () {

        // retrieve the content pane of the top-level container JFrame
        Container cp = getContentPane();
        // all operations done on the content pane
        cp.setLayout(new FlowLayout());

        cp.add(new JLabel("Counter"));
        tfCount = new JTextField(10);
        tfCount.setEditable(true);
        tfCount.setText(numbers + "");
        cp.add(tfCount);

        JButton btnCount = new JButton("Count");
        cp.add(btnCount);
        btnCount.addActionListener(this);

        // exit program if close-window button clicks
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // set initial window size
        setSize(280,80);
        // location in screen
        setLocation(400,200);
        // set this JFrame's title
        setTitle("Swing Counter");
        setVisible(true);     // Show it
    }

    public void actionPerformed(ActionEvent evt) {
        counterValue++;

        //define ArrayList to hold Integer objects
        ArrayList<Integer> numbers = new ArrayList<Integer>();
        for(int i = 0; i < 45; i++)
        {
            numbers.add(i+1);
        }

        Collections.shuffle(numbers);

        System.out.print("This week's lottery numbers are: ");
        for(int j =0; j < 6; j++)
        {
            System.out.print(numbers.get(j) + " ");
            tfCount.setText(numbers.get(j) + " ");
        }
    }
}
4

4 回答 4

1

you need to do this :

String text=""
for(int j =0; j < 6; j++)
{
text+=numbers.get(j) 
}
tfCount.setText(text); 
于 2013-02-24T10:08:20.460 回答
1

You are calling this in the for loop:

tfCount.setText(numbers.get(j) + " "); 

That way always the last number will be shown in the TextField. You have to concatenate the numbers in the loop and call tfCount.setText() afterwards.

Like this:

String concatenatedNumbers = "";
for(int j =0; j < 6; j++) {
   concatenatedNumbers = concatenatedNumbers + ", " + numbers.get(j);
 }
 tfCount.setText(concatenatedNumbers); 
于 2013-02-24T10:09:16.353 回答
1

You are overriding the text of JTextField 6 times, so it shows only the last one.

You first want to create a String containing all numbers and then use its value to set the text:

// String to hold the lottery numbers
String numbersString = "";
for(int j =0; j < 6; j++)
{
  // Print number to log/terminal
  System.out.print(numbers.get(j));
  // Append the string with the current number
  numbersString += numbers.get(j) + " ";
}
// Update the value of the JTextField with all the numbers at once
tfCount.setText(numbersString); 
于 2013-02-24T10:09:53.840 回答
1

Each time you call setText() on the text field, you replace the text it displays by another text. So create a String holding the 6 first numbers, and then set the text of the text field with the result:

StringBuilder sb = new StringBuilder();
for (int i = 0; i < 6; i++) {
    sb.append(numbers.get(i));
    sb.append(' ');
}
tfCount.setText(sb.toString());
于 2013-02-24T10:10:20.207 回答