2013-02-08 73 views
1

問題是,我嘗試使第二個文本框(textFieldGen)打印出精確的結果,如控制檯。目前,第二個文本字段僅顯示一個字符串。使用java textfield.setText()使用for循環輸出字符串

這是我在java GUI上的第一個編碼。 (額外的信息,我建立了它使用Eclipse用的WindowBuilder + GEF)

import java.awt.EventQueue; 
import javax.swing.JFrame; 
import javax.swing.JOptionPane; 
import java.awt.TextField; 
import java.awt.Label; 
import java.awt.Button; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.Random; 

public class PassGenerator { 
private JFrame frmPasswordGenerator; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       PassGenerator window = new PassGenerator(); 
       window.frmPasswordGenerator.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the application. 
*/ 
public PassGenerator() { 
    initialize(); 
} 

/** 
* Initialize the contents of the frame. 
*/ 
private void initialize() { 
    frmPasswordGenerator = new JFrame(); 
    frmPasswordGenerator.setTitle("Password Generator"); 
    frmPasswordGenerator.setBounds(100, 100, 419, 229); 
    frmPasswordGenerator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frmPasswordGenerator.getContentPane().setLayout(null); 

    final TextField textFieldGen = new TextField(); 
    final TextField textFieldPass = new TextField(); 

    textFieldPass.setBounds(74, 60, 149, 19); 
    frmPasswordGenerator.getContentPane().add(textFieldPass); 

    Label labelPass = new Label("Password Length:"); 
    labelPass.setBounds(74, 33, 177, 21); 
    frmPasswordGenerator.getContentPane().add(labelPass); 

    Button genButton = new Button("Generate"); 
    genButton.setBounds(262, 60, 86, 23); 
    frmPasswordGenerator.getContentPane().add(genButton); 
    // Add action listener to button 
    genButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
     // Execute when button is pressed 
     //System.out.println("You clicked the button"); 
      String getTxt = textFieldPass.getText(); 
      boolean y = true; 

      do{ 
       try{ 
        int c = Integer.parseInt(getTxt); 
        Random r = new Random(); 
        String[] alphabet = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","w","x","y","z"}; 
        int nc = 0-c; 
        int c2 = c/2; 
        int nc2 = 0-c2; 
        int ncm = (nc+1)/2; 

        if (c%2 == 0){ 
         for(int x = nc2; x<0;x++){ 
          int alphaNum = r.nextInt(26); 
          System.out.print(alphabet[alphaNum]); 
          String alpha = alphabet[alphaNum]; 
          textFieldGen.setText(alpha.toString()); 
          int numNum = r.nextInt(10); 
          System.out.print(numNum); 
          textFieldGen.setText(Integer.toString(numNum)); 
         } 
        } 
        else { 
         for(int x = ncm; x<0;x++){ 
          int alphaNum = r.nextInt(26); 
          System.out.print(alphabet[alphaNum]); 
          String alpha = alphabet[alphaNum]; 
          textFieldGen.setText(alpha.toString()); 
          int numNum = r.nextInt(10); 
          System.out.print(numNum); 
          textFieldGen.setText(Integer.toString(numNum)); 
         } 
         int numNum = r.nextInt(10); 
         System.out.print(numNum); 
         textFieldGen.setText(Integer.toString(numNum)); 
        } 
        y = false; 
       } 
       catch (NumberFormatException e1){ 
        int messageType = JOptionPane.PLAIN_MESSAGE; 
        JOptionPane.showMessageDialog(null, "Please Enter Integer only", "Error!!", messageType); 
        y = false; 
       } 
      }while (y); 
     } 
    });   

    Label labelGen = new Label("Generated Password:"); 
    labelGen.setBounds(74, 109, 149, 21); 
    frmPasswordGenerator.getContentPane().add(labelGen); 


    textFieldGen.setBounds(74, 136, 149, 19); 
    frmPasswordGenerator.getContentPane().add(textFieldGen); 

    Button copyButton = new Button("Copy"); 
    copyButton.setBounds(262, 136, 86, 23); 
    frmPasswordGenerator.getContentPane().add(copyButton); 
} 

} 

回答

3

使用textFieldGen.setText (textFieldGen.getText() + "\n" + Integer.toString(numNum))

+0

哇感謝..你救我的一天:) – 2013-02-08 21:51:28

+1

@MarkJue如果回答你的問題,它標記爲一個答案。 – dreamcrash 2013-02-08 21:54:51

+0

@MarkJue UR wellcome – 2013-02-08 21:56:36

3

我不明白你爲什麼要設置文本字段的內容兩倍。我只想做:

int alphaNum = r.nextInt(26); 
System.out.print(alphabet[alphaNum]); 
String alpha = alphabet[alphaNum]; 
//textFieldGen.setText(alpha.toString()); 
int numNum = r.nextInt(10); 
System.out.print(numNum); 
//textFieldGen.setText(Integer.toString(numNum)); 
textFieldGen.setText(alpha + Integer.toString(numNum)); 

一般來說,是不是一個好的做法,以替換整個文本時,所有你需要做的是一些字符追加到文本字段。如果你不喜歡,然後上面你可以這樣做:

textField.setText(alpha); 
... 
Document doc = textField.getDocument(); 
doc.insertString(doc.getLength(), Integer.toString(numNum), null); 

編輯:

import java.awt.*; 
import javax.swing.*; 
import javax.swing.text.*; 

public class SSCCE extends JPanel 
{ 
    public SSCCE() 
    { 
     JTextField textField = new JTextField(20); 
     add(textField); 

     textField.setText("testing: "); 

     try 
     { 
      Document doc = textField.getDocument(); 
      doc.insertString(doc.getLength(), "1", null); 
     } 
     catch(Exception e) {} 
    } 

    private static void createAndShowUI() 
    { 
     JFrame frame = new JFrame("SSCCE"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(new SSCCE()); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       createAndShowUI(); 
      } 
     }); 
    } 
} 
+0

當使用textField.getDocument – 2013-02-09 06:52:48

+0

@MarkJue時,我無法正確使用它,然後發佈證明問題的SSCCE。我無法準確猜出你做了什麼。請參閱我的編輯瞭解SSCCE的示例。 – camickr 2013-02-09 16:47:58