gpt4 book ai didi

java - 随机读取文件并输出到标签中

转载 作者:行者123 更新时间:2023-12-02 00:08:33 24 4
gpt4 key购买 nike

我使用这些代码随机读取文本文件,并在 label 中显示输出。我不知道如何读取随机单词或行并输出到标签中?最后我的目标是读取随机单词并将该单词放入标签中

static JLabel lbl;
JLabel word ;

a(){
ButtonComponent ();
OtherParts ();
labels();

setTitle("HangmanGame");
setSize(840, 310);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLayout(null);
setVisible(true);
setLocation(320, 150);
}

public void labels(){
for(int s=19; s>=8;s--){
word = new JLabel ("");
word.setBounds( s*30, 60, 20, 20);
add(word);
}

for (int a = 19; a >= 8; a--) {
JLabel lbl = new JLabel("_");
lbl.setBounds(a * 30, 60, 20, 20);
add(lbl);
}
}

public void OtherParts () {
JTextField tf = new JTextField();
tf.setBounds(55, 190, 340, 30);
add(tf);

JButton Guess = new JButton("Guess");
Guess.setBounds(410, 190, 355, 30);
add(Guess);
JLabel chance = new JLabel ("Remaining Chance");
chance.setBounds(55, 215, 340, 30);
add(chance);

}

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);
}
}

public void MenuComponent () {
JMenuBar menubar = new JMenuBar();
setJMenuBar(menubar);

JMenu file = new JMenu("File");
menubar.add(file);
JMenuItem newgame = new JMenuItem("New");
JMenuItem savegame = new JMenuItem("Save Game");
JMenuItem Loadgame = new JMenuItem("Load");
JMenuItem exit = new JMenuItem("Exit");

file.add(savegame);
file.add(Loadgame);
file.add(exit);
file.add(newgame);

exit.addActionListener(new exitListener());
JMenu option = new JMenu("Option");
menubar.add(option);
JMenuItem op = new JMenuItem("Option");
option.add(op);
}

class exitListener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
}

class BtnListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JButton clickedButton = (JButton) e.getSource();
String text = clickedButton.getText();
System.out.println(text + lbl);
//word.setText(text);
}
}

public static void main(String[] args) {
new a();
Properties readfile = new Properties();
try {
readfile.load(new FileInputStream("ciu"));
} catch (Exception e) {
System.out.println(e.toString());
}
for (int i = 1; i <5; i++) {
String line = readfile.getProperty("" + i);
System.out.println(line);
}
}

最佳答案

您有很多与您的问题无关的代码。如果你想生成随机数,你可以使用 Random

 Random random = new Random();
int randomInt = random.nextInt(10);//generate random numbers between 0..10

我仍然不确定你想要什么,但我希望这有帮助

关于java - 随机读取文件并输出到标签中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13360644/

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