2013-05-09 36 views
0

我想用可以和JLabel一起使用的東西替換setCharAt ...我一直在oracle文檔中尋找解決方案。我不知道我是否在尋找錯誤的東西,或者它只是不存在..如果它不存在我怎麼能解決這個問題?我明白我的命名約定是關閉的,將被儘快改變他們...setCharAt a for JLabel

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.Collections; 

import javax.swing.*; 



public class HangmanPanel extends JPanel { 
    static Boolean FOUND; 
    private static final long serialVersionUID = -5793357804828609325L; 

    public static String answerKey() { 
     //get random array element 
     String array[] = new String[10]; 
     array[0] = "hamlet"; 
     array[1] = "mysts of avalon"; 
     array[2] = "the iliad"; 
     array[3] = "tales from edger allan poe"; 
     array[4] = "the children of hurin"; 
     array[5] = "the red b" + 
       "+adge of courage"; 
     array[6] = "of mice and men"; 
     array[7] = "utopia"; 
     array[8] = "chariots of the gods"; 
     array[9] = "a brief history of time"; 

     ArrayList<String> list = new ArrayList<String>(Arrays.asList(array)); 
     Collections.shuffle(list); 
     String s = list.get(0); 
     return s; 
    } 

    public StringBuilder dashReplace(String s) { 
     //replace non-white space char with dashes and creates StringBuilder Object 
     String tW = s.replaceAll("\\S", "-"); 
     System.out.print(tW + "\n"); 
     StringBuilder AnswerKey = new StringBuilder(tW); 
     return AnswerKey; 
    } 
    public static int findAndReplace(String s, JLabel answerKey, String sc, 
      char ch) { 
     //find position of user input and replace 
     int pos = -1; 
     FOUND = false; 
     while(true){ 
     pos = s.indexOf(sc, pos+1); 
     if(pos < 0){ 

      break; 
     }else{ 
      FOUND = true; 
      //setCharAt dosen't work for JLable 
      answerKey.setCharAt(pos, ch); 
     } 

     } 
     JLabel AnswerKey2 = new JLabel(answerKey.toString()); 
     return pos; 
    } 




    public HangmanPanel(final String s){ 
     this.setLayout(null); 

     JLabel heading = new JLabel("Welcome to the Hangman App"); 
     JButton Button = new JButton("Ok"); 
     //get input 

     JLabel tfLable = new JLabel("Please Enter a Letter:"); 


     final JLabel AnswerKey = new JLabel(dashReplace(answerKey()).toString()); 

     final JTextField text = new JTextField(10); 


     heading.setSize(200, 50); 
     tfLable.setSize(150, 50); 
     text.setSize(50, 30); 
     Button.setSize(60, 20); 
     AnswerKey.setSize(200, 100); 

     heading.setLocation(300, 10); 
     tfLable.setLocation(50, 40); 
     text.setLocation(50, 80); 
     Button.setLocation(100, 85); 
     AnswerKey.setLocation(100,85); 

     this.add(heading); 
     this.add(tfLable); 
     this.add(text); 
     this.add(Button); 
     this.add(AnswerKey); 


     Button.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       // can't access text 
       String sc = text.getText(); 
       char ch = sc.charAt(0); 
       findAndReplace(s, AnswerKey, sc, ch); 
      } 
     }); 

    } 

} 
+0

'我明白我的命名約定已關閉,將盡快改變它們......' - 改變他們在發佈問題之前,而不是之後。讓你的問題儘可能容易讓我們理解。 – camickr 2013-05-09 15:23:50

回答

3

的唯一方法可用於JLabel組件設置文本setTextStrings也是不可變的。因此,你可以使用StringBuilder

StringBuilder builder = new StringBuilder(answerKey.getText()); 
builder.setCharAt(pos, ch); 
answerKey.setText(builder.toString()); 
+0

感謝您的幫助! :) – Sage1216 2013-05-09 15:57:09

4

你爲什麼要使用setCharAt(...)有一個JLabel。標籤是爲了顯示靜態文本。改變它的唯一方法是替換整個字符串。

我想你可以這樣做:

StringBuilder text = label.getText(); 
text.setCharAt(...); 
label.setText(text.toString()); 

另一種選擇是使用一個JTextField,看起來像一個JL​​abel:

JTextField label = new JTextField(...); 
label.setEditable(false); 
label.setBorder(null); 
label.setOpaque(false); 

然後,當您需要更改文本你可以做:

label.select(...); 
label.replaceSelection(...); 
+0

謝謝你也是非常有幫助的 – Sage1216 2013-05-09 15:58:00