2013-02-24 20 views
0

我有一個問題,顯示我的文本字段上的數組j的值。我運行時只顯示1個元素。我想在文本字段顯示數組的所有6個元素

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) + " "); 
     } 
    } 
} 

回答

1

你需要這樣做:

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

謝謝...的幫助 – manishmuu 2013-02-24 11:18:34

1

您在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); 
1

要覆蓋的JTextField 6倍的文字,所以只顯示最後一個。

您首先要創建一個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); 
+0

謝謝大家的幫助.. – manishmuu 2013-02-24 11:18:49

1

每次文本字段調用setText()時間,則在更換顯示另一個文本的文本。因此,創建一個字符串拿着6號第一次,然後將文本字段與結果的文本:

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

非常感謝你對於投入......它幫助了我很多......現在的工作。 – manishmuu 2013-02-24 11:18:11

相關問題