随机读取文件并在标签中输出

时间:2012-11-13 12:19:24

标签: java

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

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

2 个答案:

答案 0 :(得分:3)

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

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

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

答案 1 :(得分:0)

以下是如何从文本文件中读取一行 https://stackoverflow.com/a/9181778/1360074

这是将文字设置为标签的方式:

JLabel label = new JLabel();
label.setText(str);