Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用于在标签上添加后者的方法按钮_Java_Swing - Fatal编程技术网

Java 用于在标签上添加后者的方法按钮

Java 用于在标签上添加后者的方法按钮,java,swing,Java,Swing,我有一个HangGame程序,当我按下后一个按钮(A,B,…)时,我不会写这段代码。我按下后一个按钮,它会出现在我在文本文件中读取的单词标签部分的正确位置。例如,我在文本文件中读取单词,如(木桩),屏幕上会出现五个标签(),当我按住S按钮时,字符S将出现在第一个标签部分(S______;)上,依此类推。我不知道如何为Action listener inside按钮编写代码。最后,我添加标签时没有任何问题,但我不知道如何在正确的位置放置正确的字符。 我希望你能理解我的问题 public v

我有一个HangGame程序,当我按下后一个按钮(A,B,…)时,我不会写这段代码。我按下后一个按钮,它会出现在我在文本文件中读取的单词标签部分的正确位置。例如,我在文本文件中读取单词,如(木桩),屏幕上会出现五个标签(),当我按住S按钮时,字符S将出现在第一个标签部分(S______;)上,依此类推。我不知道如何为Action listener inside按钮编写代码。最后,我添加标签时没有任何问题,但我不知道如何在正确的位置放置正确的字符。 我希望你能理解我的问题

    public void ButtonComponent () {
    for (int i = 65; i < 78; i++) {
        JButton temp = new JButton("" + (char) i);
        temp.addActionListener(new BtnListener());
        temp.setBounds((i - 64) * 55, 110, 50, 30);
        add(temp);
    }
    for (int i = 78; i < 91; i++) {
        JButton temp = new JButton("" + (char) i);
        temp.addActionListener(new BtnListener());
        temp.setBounds((i - 77) * 55, 150, 50, 30);
        add(temp);
    }
}
class BtnListener implements ActionListener {
public void actionPerformed(ActionEvent e) {

    for (int s=0;s<splitword.length;s++) {
    String btnText = ((JButton)e.getSource()).getText().toLowerCase();
    if (splitword[s].contains(btnText)) {
        System.out.println("This letter is contained in the word:" + btnText);
    }
public void按钮组件(){
对于(int i=65;i<78;i++){
JButton temp=新JButton(“+(char)i);
temp.addActionListener(新的BTNLListener());
温度立根((i-64)*55、110、50、30);
添加(临时);
}
对于(int i=78;i<91;i++){
JButton temp=新JButton(“+(char)i);
temp.addActionListener(新的BTNLListener());
温度立根((i-77)*55、150、50、30);
添加(临时);
}
}
类BtnListener实现ActionListener{
已执行的公共无效操作(操作事件e){

对于(int s=0;s由于这是Hangman,您必须跟踪所有使用的正确字母。以下是解决方案的模板:

String guessWord = "STAKE";
StringBuilder builder = new StringBuilder();
builder.append("S"); // hint: btnText...
builder.append("T");
String displayWord = guessWord.replaceAll("[^" + builder.toString() + "]", "_");

我不会直接解决这个问题,但这会给你足够的时间来解决问题。

有人能解决我的问题吗?我会试试,可能对我有用