[英]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.