gpt4 book ai didi

java - JLabel.setText ("__") 在 for 循环中不会打印多次

转载 作者:行者123 更新时间:2023-11-29 05:32:09 24 4
gpt4 key购买 nike

我将我的 JLabel.setText("__") 放在一个 for 循环中,这样它就可以打印一个单词的长度,用一个 'space' 替换每个字母。这是一个 hangman 游戏,使用空格显示单词的长度。但是它只打印一次。关于为什么的任何提示?此外,如果您有任何关于更好地组织我的代码的提示,我们将不胜感激。提前致谢。

/*PACKAGE DECLARATION*/
package Game;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


/************************
* GAME MECHANICS CLASS *
* **********************/
public class GameStructure {

/* INSTANCE DECLARATIONS */
private String []wordList = {"computer","java","activity","alaska","appearance","article",
"automobile","basket","birthday","canada","central","character","chicken","chosen",
"cutting","daily","darkness","diagram","disappear","driving","effort","establish","exact",
"establishment","fifteen","football","foreign","frequently","frighten","function","gradually",
"hurried","identity","importance","impossible","invented","italian","journey","lincoln",
"london","massage","minerals","outer","paint","particles","personal","physical","progress",
"quarter","recognise","replace","rhythm","situation","slightly","steady","stepped",
"strike","successful","sudden","terrible","traffic","unusual","volume","yesterday"};
private int []length = new int [64];
private JTextField tf;//text field instance variable (used)
private JLabel jl2;//label instance variable (used)
private JLabel jl3;//label instance (working on)
private String letter;


/*****************
* LENGTH METHOD *
* ***************/
public void length(){

jl3 = new JLabel();

int j = 0;
for(j = 0; j<64; j++) {
length[j] = wordList[j].length();//gets length of words in wordList
}//end for
int l = 0;
for(int m = 0; m<length[l]; m++) {//supposed to print length of word with '__' as each letter
jl3.setText("__ ");//instead only printing once
l++;
}//end for
}//end length method


/*****************
* WINDOW METHOD *
* ***************/
public void window() {

LoadImageApp i = new LoadImageApp();//calling image class

JFrame gameFrame = new JFrame();//declaration
JPanel jp = new JPanel();
JPanel jp2 = new JPanel();//jpanel for blanks
JLabel jl = new JLabel("Enter a Letter:");//prompt with label

tf = new JTextField(1);//length of text field by character
jl2 = new JLabel("Letters Used: ");

jp2.add(jl3);
jp.add(jl);//add label to panel
jp.add(tf);//add text field to panel
jp.add(jl2);//add letters used

gameFrame.add(i); //adds background image to window
i.add(jp); // adds panel containing label to background image panel
i.add(jp2);

gameFrame.setTitle("Hangman");//title of frame window
gameFrame.setSize(850, 600);//sets size of frame
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//exit when 'x' button pressed
gameFrame.setIconImage(new ImageIcon("Hangman-Game-grey.png").getImage());//set the frame icon to an image loaded from a file
gameFrame.setLocationRelativeTo(null);//window centered
gameFrame.setResizable(false);//user can not resize window
gameFrame.setVisible(true);//display frame

}//end window method


/*********************
* USER INPUT METHOD *
* *******************/
public void userInput() {

tf.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {//when enter key pressed

JTextField tf = (JTextField)e.getSource();

letter = tf.getText();
jl2.setText(jl2.getText() + letter + " ");//sets jlabel text to users entered letter

}//end actionPerformed method

});

}//end userInput method

}//end GameMechanics class

最佳答案

"However it is only printing once. Any tips on why?"

setText 只会设置一次文本。所以你在循环中所做的就是一遍又一遍地设置文本。这是一个建议。在循环中连接字符串,然后设置文本。像这样:

String line = "";
for(int m = 0; m<length[l]; m++) {
line += "__ ";
l++;
}
jl3.setText(line);

" Also, if you have any tips on better organizing my code that would be appreciated. "

尝试 Code Review为此。

关于java - JLabel.setText ("__") 在 for 循环中不会打印多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20731679/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com