简体   繁体   English

我想在文本字段上显示数组的所有6个元素

[英]I would like to display all the 6 element of the array on the textfield

I have a problem displaying the value of array j on my text field. 我在文本字段上显示数组j的值时遇到问题。 Only 1 element is display when I run it. 运行时仅显示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) + " ");
        }
    }
}

you need to do this : 您需要这样做:

String text=""
for(int j =0; j < 6; j++)
{
text+=numbers.get(j) 
}
tfCount.setText(text); 

You are calling this in the for loop: 您在for循环中调用此方法:

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

That way always the last number will be shown in the TextField. 这样,始终将最后一个数字显示在TextField中。 You have to concatenate the numbers in the loop and call tfCount.setText() afterwards. 您必须在循环中连接数字,然后再调用tfCount.setText()

Like this: 像这样:

String concatenatedNumbers = "";
for(int j =0; j < 6; j++) {
   concatenatedNumbers = concatenatedNumbers + ", " + numbers.get(j);
 }
 tfCount.setText(concatenatedNumbers); 

You are overriding the text of JTextField 6 times, so it shows only the last one. 您将JTextField的文本覆盖6次,因此它仅显示最后一个文本。

You first want to create a String containing all numbers and then use its value to set the text: 您首先要创建一个包含所有数字的String ,然后使用其值来设置文本:

// 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); 

Each time you call setText() on the text field, you replace the text it displays by another text. 每次在文本字段上调用setText()时,都将其显示的文本替换为另一个文本。 So create a String holding the 6 first numbers, and then set the text of the text field with the result: 因此,创建一个包含前6个数字的字符串,然后使用结果设置文本字段的文本:

StringBuilder sb = new StringBuilder();
for (int i = 0; i < 6; i++) {
    sb.append(numbers.get(i));
    sb.append(' ');
}
tfCount.setText(sb.toString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何显示数组中元素的位置? - How would I display the position of an element in an array? 我希望Arrays能够记住这一切 - I would like the Arrays to remember it all 我想返回数组的中间三分之一 - I would like to return middle third of an array a 我想知道 array[array[i]]++ 在 java 中是如何工作的 - I would like to know how array[array[i]]++ works in java 我想使用foreach逐一显示hashmap数据吗? - I would like to display hashmap data one by one using foreach? 我想使用 ArrayList 在不同的行中显示我的 output - I would like to display my output in an different line using ArrayList 如何将JSONObject的元素转换为单个元素数组? - How would I convert an element of a JSONObject to a single element array? 执行所有M操作所产生的数组中的最大元素 - Maximum element in the array that would result from performing all M operations 我将如何遍历数组以查看用户输入变量(数字)是否等于数组中的一个元素并用“@”替换该元素 - how would i loop through an array to see if the user input variable(number) is equal to a element in the array and replace that element with a “@” 我希望该程序在用户输入其尺寸的长度和宽度时显示一个星号框 - I would like this program to display a box of asteriks when the user enters the lenght and width of its dimensions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM