简体   繁体   English

JButton在JLabel中显示文本

[英]JButton Display text in JLabel

I develop a game 我开发游戏

In my first button, I write a word. 在第一个按钮中,我写了一个字。 in my second button , I enter a letter 在第二个按钮中,我输入一个字母

I would like to find the word I register with my first button. 我想用第一个按钮找到我注册的单词。 My problem is that I can not back up previous research that I already do. 我的问题是我无法备份以前已经做过的研究。

I do not understand how I could do, so maybe you could help me, it will be great! 我不知道该怎么办,所以也许您可以帮助我,那太好了! Sorry for my approximativ 对不起,我的近似值

Example : 范例:

if I write a word with my first jButton : "hello" my program return _ _ _ 如果我用第一个jButton写一个单词:“ hello”,我的程序返回_ _ _

If I search "h", with my second button the program return : h _ _ _ _ // in my labelword 如果我搜索“ h”,则使用第二个按钮,程序将在我的标签字中返回:h _ _ _ _ //

but now, if I search "e" the program return e _ _ _, the "h" is not repost. 但是现在,如果我搜索“ e”,程序返回e _ _ _,则“ h”不会重新发布。

My code, First Jbutton : 我的代码,第一个Jbutton:

ControllerPendu a = new ControllerPendu();
final JFrame popup = new JFrame();
this.word = JOptionPane.showInputDialog(popup, "Enter one word", null);

//control lenght of word, if the word < = 3 
String afterCtrl = a.controleW(word);


labelWord.setText("");
for (int i = 0; i < afterCtrl.length(); i++) {
    labelWord.setText(labelWord.getText() + "_ ");
    labelWord.validate();
    labelCompteur.repaint();
}

//counter key on the word, panel top/right
String mot = this.word;
labelCompteur.setText(String.valueOf(mot.length()));

and the 2nd jButton : 和第二个jButton:

ControllerPendu a = new ControllerPendu();

final JFrame popup = new JFrame();
this.key = JOptionPane.showInputDialog(popup, "Enter one key", null);

char ctrlKey = a.controleK(key).charAt(0);     
// loop for print key of the word
for (int i = 0; i < this.word.length(); i++) {

    //retourn the key in function to the index 
    char lettre = word.charAt(i);


    if (ctrlKey == lettre) {
        labelWord.setText(key);

    } else  {
        labelWord.setText(labelWord.getText() + "_ ");

    }
}

    } else  {
        //is the ke isn't match, display "_ " 
        labelWord.setText(labelWord.getText() + "_ ");

    }
}

ControllerPendu is the class is to check the word and the letter ctrlKey = the key tiped in second jButton ControllerPendu是用于检查单词和字母ctrlKey =第二个jButton中提示的键的类

Why don't you store the search result in some instance variable String searchResult , then you can update it each time a new search has place. 为什么不将搜索结果存储在某个实例变量String searchResult ,那么每次进行新搜索时都可以对其进行更新。

Sorry, I wish I could post in how to do that, but your code is missing some parts and can't make a wild guess on top of what you have already forwarded. 抱歉,我希望可以发布相关操作方法,但是您的代码缺少某些部分,因此无法对已经转发的内容进行大胆的猜测。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM