有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

获取Java中JLabel的字符

我有一个包含许多单词的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. 左侧的面板,由单词搜索组成
  2. 将单词搜索的内容放入GUI中单词搜索网格的JLabel中
  3. 现在我只需要jlabe[index].charAt(0)。这是行不通的

我试过了。这是行不通的

我在上面尝试的方法很好,但不是为了我想要的

还有另一个班

public class WordSearchException extends RuntimeException {

private static final long serialVersionUID = 1L;

public WordSearchException() {

}

public WordSearchException(String reason) {
    super(reason);
}

}


共 (0) 个答案