Java 我想在textfield上显示数组的所有6个元素

Java 我想在textfield上显示数组的所有6个元素,java,Java,在文本字段中显示数组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 JTextF

在文本字段中显示数组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) + " ");
        }
    }
}
import java.util.Collections;
导入java.util.ArrayList;
导入java.awt.*;
导入javax.swing.*;
导入java.awt.event.*;
公共类SwingCounter扩展JFrame实现ActionListener{
//使用Swing的JTextField而不是AWT的TextField
私有JTextField tfCount;
私有int计数器值=0,数字=0,j=0;
公共SwingCounter(){
//检索顶级容器框架的内容窗格
容器cp=getContentPane();
//在内容窗格上执行的所有操作
cp.setLayout(新的FlowLayout());
cp.add(新JLabel(“计数器”));
tfCount=新的JTextField(10);
tfCount.setEditable(true);
tfCount.setText(数字+“”);
cp.add(tfCount);
JButton btnCount=新JButton(“计数”);
cp.add(btnCount);
btnCount.addActionListener(此);
//如果单击“关闭窗口”按钮,则退出程序
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置初始窗口大小
设置大小(280,80);
//屏幕中的位置
设置位置(400200);
//设置此JFrame的标题
setTitle(“摆动计数器”);
setVisible(true);//显示它
}
已执行的公共无效操作(操作事件evt){
计数器值++;
//定义ArrayList以保存整数对象
ArrayList编号=新的ArrayList();
对于(int i=0;i<45;i++)
{
增加(i+1);
}
收藏。洗牌(数字);
System.out.print(“本周的彩票号码是:”);
对于(int j=0;j<6;j++)
{
System.out.print(number.get(j)+”);
tfCount.setText(numbers.get(j)+”);
}
}
}
您需要执行以下操作:

String text=""
for(int j =0; j < 6; j++)
{
text+=numbers.get(j) 
}
tfCount.setText(text); 
String text=“”
对于(int j=0;j<6;j++)
{
text+=数字。获取(j)
}
tfCount.setText(文本);
您需要执行以下操作:

String text=""
for(int j =0; j < 6; j++)
{
text+=numbers.get(j) 
}
tfCount.setText(text); 
String text=“”
对于(int j=0;j<6;j++)
{
text+=数字。获取(j)
}
tfCount.setText(文本);

您正在for循环中调用此函数:

tfCount.setText(numbers.get(j) + " "); 
这样,最后一个数字将始终显示在文本字段中。您必须连接循环中的数字,然后调用
tfCount.setText()

像这样:

String concatenatedNumbers = "";
for(int j =0; j < 6; j++) {
   concatenatedNumbers = concatenatedNumbers + ", " + numbers.get(j);
 }
 tfCount.setText(concatenatedNumbers); 
String concatenatedNumbers=“”;
对于(int j=0;j<6;j++){
concatenatedNumbers=concatenatedNumbers+“,”+numbers.get(j);
}
tfCount.setText(连接的数字);

您正在for循环中调用此函数:

tfCount.setText(numbers.get(j) + " "); 
这样,最后一个数字将始终显示在文本字段中。您必须连接循环中的数字,然后调用
tfCount.setText()

像这样:

String concatenatedNumbers = "";
for(int j =0; j < 6; j++) {
   concatenatedNumbers = concatenatedNumbers + ", " + numbers.get(j);
 }
 tfCount.setText(concatenatedNumbers); 
String concatenatedNumbers=“”;
对于(int j=0;j<6;j++){
concatenatedNumbers=concatenatedNumbers+“,”+numbers.get(j);
}
tfCount.setText(连接的数字);

您覆盖了
JTextField的文本6次,因此它只显示最后一次

首先要创建一个包含所有数字的
字符串
,然后使用其值设置文本:

// 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); 
//用于保存彩票号码的字符串
字符串编号String=“”;
对于(int j=0;j<6;j++)
{
//将号码打印到日志/终端
系统输出打印(number.get(j));
//用当前数字追加字符串
numbersString+=numbers.get(j)+;
}
//一次用所有数字更新JTextField的值
tfCount.setText(numberstring);

您覆盖了
JTextField的文本6次,因此它只显示最后一次

首先要创建一个包含所有数字的
字符串
,然后使用其值设置文本:

// 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); 
//用于保存彩票号码的字符串
字符串编号String=“”;
对于(int j=0;j<6;j++)
{
//将号码打印到日志/终端
系统输出打印(number.get(j));
//用当前数字追加字符串
numbersString+=numbers.get(j)+;
}
//一次用所有数字更新JTextField的值
tfCount.setText(numberstring);

每次在文本字段上调用
setText()
,都会用另一个文本替换它显示的文本。因此,创建一个包含前6个数字的字符串,然后使用结果设置文本字段的文本:

StringBuilder sb = new StringBuilder();
for (int i = 0; i < 6; i++) {
    sb.append(numbers.get(i));
    sb.append(' ');
}
tfCount.setText(sb.toString());
StringBuilder sb=新建StringBuilder();
对于(int i=0;i<6;i++){
某人附加(数字)得到(i);
某人附加(“”);
}
tfCount.setText(sb.toString());

每次在文本字段上调用
setText()
,都会用另一个文本替换它显示的文本。因此,创建一个包含前6个数字的字符串,然后使用结果设置文本字段的文本:

StringBuilder sb = new StringBuilder();
for (int i = 0; i < 6; i++) {
    sb.append(numbers.get(i));
    sb.append(' ');
}
tfCount.setText(sb.toString());
StringBuilder sb=新建StringBuilder();
对于(int i=0;i<6;i++){
某人附加(数字)得到(i);
某人附加(“”);
}
tfCount.setText(sb.toString());

非常感谢您的投入。。。这对我帮助很大。。现在开始工作了。非常感谢您的投入。。。这对我帮助很大。。现在开始工作了。