2012-11-13 29 views
0

我使用这些代码随机读取文本文件,并向我显示标签中的输出。我不知道我是如何阅读随机文字或线条并将其放入标签中的? finaly我的目的是阅读随机字进出把这个词在标签随机读取文件并在标签中输出

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

这是你的代码? –

+0

是的,这是我的代码 – jangiz

回答

3

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

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

我还是不知道你想要的这里,但我希望这有助于