Reputation: 41
I use these codes for reading in a text file randomly and show me the output in label . I don't know how I read random word or line and out put in the label? finaly my aim is reading random word and out put that word in the 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);
}
}
Upvotes: 0
Views: 297
Reputation: 370
You have a lot of code not related to your problem. If you want to generate random numbers you can use Random
Random random = new Random();
int randomInt = random.nextInt(10);//generate random numbers between 0..10
I'm still not sure what you want here, but I hope this helps
Upvotes: 3
Reputation: 9579
Here is how to read a line from a text file https://stackoverflow.com/a/9181778/1360074
This is how you set text to a label:
JLabel label = new JLabel();
label.setText(str);
Upvotes: 0