2017-02-21 32 views
-2

多くの単語を含むJLabel配列を持っています。私は単語の最初の文字を取得しようとしています。実際に私はすべてのキャラクターを手に入れようとしていますが、最初の方法を見れば他のキャラクターも手に入れます。JavaのJLabelの文字を取得

私はすべて次の持っているこの

mport java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.StringTokenizer; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.SwingConstants; 
import javax.swing.border.EtchedBorder; 
import javax.swing.border.TitledBorder; 

public class WordSearchFrame extends JFrame { 

    private static final long serialVersionUID = 1L; 

    private static final int COLS_IN_WORDLIST = 1; 


    private static final int FRAME_WIDTH = 640; 


    private static final int FRAME_HEIGHT = 480; 


    private JLabel[][] wordSearch; 


    private JLabel[] wordListLabels; 


    private JPanel wordListPanel; 


    private JPanel wordsearchPanel; 


    private JPanel rightSidePanel; 


    private JPanel leftSidePanel; 


    private JPanel searchButtonPanel; 


    private JButton searchButton; 


    private int numRows; 


    private int numCols; 


    private ActionListener searchButtonListener; 

    private ArrayList<String> wordList; 

    class SearchListener implements ActionListener { 

     private int x = 0; 

     public void actionPerformed(ActionEvent event) { 
      wordListLabels[0].setForeground(Color.BLACK); 
      if (x == 0) { 
       findword(x); 
       x++; 
      } else { 
       wordListLabels[x - 1].setForeground(Color.BLACK); 
       findword(x); 
       x++; 
      } 


     } 

     private void findword(int x) { 
      wordListLabels[x].setForeground(Color.RED); 
      findword2(x); 

     } 

     private void findword2(int x) { 
      for (int i = 0; i < 7; i++) 
       wordSearch[x + i][x].setForeground(Color.RED); 

     } 
    } 

    private void buildLeftSidePanel() throws WordSearchException, IOException { 

     leftSidePanel = new JPanel(); 
     leftSidePanel.setLayout(new BorderLayout()); 
     leftSidePanel.setBorder(new TitledBorder(new EtchedBorder(), "Word Search")); 


     wordsearchPanel = new JPanel(); 
     wordsearchPanel.setLayout(new GridLayout(numRows, numCols)); 
     leftSidePanel.add(wordsearchPanel, BorderLayout.CENTER); 


     this.getContentPane().add(leftSidePanel, BorderLayout.CENTER); 
    } 


    private void initGridFromFile(String wordSearchFilename) throws WordSearchException, IOException { 
     this.numRows = 0; 
     this.numCols = 0; 
     BufferedReader reader = new BufferedReader(new FileReader(wordSearchFilename)); 
     String line = reader.readLine(); 
     while (line != null) { 
      StringTokenizer tokenizer = new StringTokenizer(line, " "); 
      if (this.numCols == 0) { 
       this.numCols = tokenizer.countTokens(); 
      } else { 
       if (tokenizer.countTokens() != this.numCols) { 
        throw new WordSearchException("Invalid number of columns in word search"); 
       } 
      } 
      line = reader.readLine(); 
      this.numRows++; 
     } 
     reader.close(); 
     this.wordSearch = new JLabel[numRows][numCols]; 
    } 


    protected void loadGridFromFile(String wordSearchFilename) throws IOException { 
     int row = 0; 
     BufferedReader reader = new BufferedReader(new FileReader(wordSearchFilename)); 
     String line = reader.readLine(); 
     while (line != null) { 
      StringTokenizer tokenizer = new StringTokenizer(line, " "); 
      int col = 0; 
      while (tokenizer.hasMoreTokens()) { 
       String tok = tokenizer.nextToken(); 
       wordSearch[row][col] = new JLabel(tok); 
       wordSearch[row][col].setForeground(Color.BLACK); 
       wordSearch[row][col].setHorizontalAlignment(SwingConstants.CENTER); 
       wordsearchPanel.add(wordSearch[row][col]); 
       col++; 
      } 
      line = reader.readLine(); 
      row++; 
     } 
     reader.close(); 
    } 


    private void buildRightSidePanel() { 

     rightSidePanel = new JPanel(); 
     rightSidePanel.setBorder(new TitledBorder(new EtchedBorder(), "Word List")); 
     rightSidePanel.setLayout(new BorderLayout()); 


     wordListLabels = new JLabel[wordList.size()]; 
     wordListPanel = new JPanel(); 
     wordListPanel.setLayout(new GridLayout(wordList.size(), 1)); 
     for (int i = 0; i < this.wordList.size(); i++) { 
      // If the line below won't compile in Java 1.4.2, make it 
      // String word = (String)this.wordList.get(i); 
      String word = this.wordList.get(i); 
      wordListLabels[i] = new JLabel(word); 
      wordListLabels[i].setForeground(Color.BLUE); 
      wordListLabels[i].setHorizontalAlignment(SwingConstants.CENTER); 
      wordListPanel.add(wordListLabels[i]); 
     } 
     rightSidePanel.add(wordListPanel, BorderLayout.CENTER); 


     searchButton = new JButton("Search"); 
     searchButtonListener = new SearchListener(); 
     searchButton.addActionListener(searchButtonListener); 
     searchButtonPanel = new JPanel(); 
     searchButtonPanel.add(searchButton); 
     rightSidePanel.add(searchButtonPanel, BorderLayout.SOUTH); 


     this.getContentPane().add(rightSidePanel, BorderLayout.EAST); 
    } 

    private void loadWordList(String wordListFilename) throws WordSearchException, IOException { 
     int row = 0; 

     wordList = new ArrayList<String>(); 
     BufferedReader reader = new BufferedReader(new FileReader(wordListFilename)); 
     String line = reader.readLine(); 
     while (line != null) { 
      StringTokenizer tokenizer = new StringTokenizer(line, " "); 
      if (tokenizer.countTokens() != COLS_IN_WORDLIST) { 
       throw new WordSearchException("Error: only one word per line allowed in the word list"); 
      } 
      String tok = tokenizer.nextToken(); 
      wordList.add(tok); 
      line = reader.readLine(); 
      row++; 
     } 
     reader.close(); 
    } 


    public WordSearchFrame(String wordSearchFilename, String wordListFilename) throws IOException, WordSearchException { 
     this.setSize(FRAME_WIDTH, FRAME_HEIGHT); 
     this.getContentPane().setLayout(new BorderLayout()); 
     this.initGridFromFile(wordSearchFilename); 
     buildLeftSidePanel(); 
     this.loadGridFromFile(wordSearchFilename); 
     loadWordList(wordListFilename); 
     buildRightSidePanel(); 
    } 


    public WordSearchFrame(String[][] wordSearch, String[] wordList) throws IOException, WordSearchException { 
     this.setSize(FRAME_WIDTH, FRAME_HEIGHT); 
     this.getContentPane().setLayout(new BorderLayout()); 
     this.numRows = wordSearch.length; 
     this.numCols = wordSearch[0].length; 
     this.wordSearch = new JLabel[this.numRows][this.numCols]; 
     buildLeftSidePanel(); 
     for (int row = 0; row < this.numRows; row++) { 
      for (int col = 0; col < this.numCols; col++) { 
       this.wordSearch[row][col] = new JLabel(wordSearch[row][col]); 
       this.wordSearch[row][col].setForeground(Color.BLACK); 
       this.wordSearch[row][col].setHorizontalAlignment(SwingConstants.CENTER); 
       this.wordsearchPanel.add(this.wordSearch[row][col]); 
      } 
     } 

     this.wordList = new ArrayList<String>(); 
     for (int i = 0; i < wordList.length; i++) { 
      this.wordList.add(wordList[i]); 
     } 
     buildRightSidePanel(); 
    } 

    public static void main(String[] args) { 
     try { 
      if (args.length != 2) { 
       System.out.println("Command line arguments: <word search file> <word list>"); 
      } else { 
       WordSearchFrame frame = new WordSearchFrame(args[0], args[1]); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setVisible(true); 
      } 
     } catch (Exception e) { 
      System.out.println(e.getMessage()); 
      e.printStackTrace(); 
     } 
    } 
} 

を試してみました:単語検索

  • ワード検索の内容にで構成され、左側に

    1. パネルを、 GUI内の単語検索グリッドのJLabel
    2. 今はjlabe[index].charAt(0)が必要です。それは動作しません。

    私はjlabe[index].getText().charAt(0)を試しました。それは動作しません。

    私が上で試したことはうまく動作しますが、私が望むものではありません。また

    他のクラス

    public class WordSearchException extends RuntimeException { 
    
    private static final long serialVersionUID = 1L; 
    
    public WordSearchException() { 
    
    } 
    
    public WordSearchException(String reason) { 
        super(reason); 
    } 
    

    は}

  • +0

    あなたの質問は、私たちが力を持っていると仮定することが表示されるドン表示されていないコードを表示するなど、存在しません。有効な[mcve]を作成してこのコードを質問に追加することを検討してください。コードを実行して自分自身のエラーを確認しようとすることができます。 –

    +0

    申し訳ありませんが、コードを追加するにはあまりにも明確な質問があると思ったので入れません。しかしそこにはあります。 –

    +0

    回答が得られるほど明確であることを願っていますが、今後の予定がない場合は、私のリクエストを考慮してください。 –

    答えて

    0

    それを行うための最善の方法)(のgettextすることです。のcharAt(インデックス)

    関連する問題