如何为此项目使用密钥监听器? 我想在输入正确的字符后看到下一个字符... 我用字母随机字母。 右边输入char将加上5秒进度条,它可以帮助你在游戏中生存更多。 我怎样才能把时间设定为前5秒=显示5让,第二秒5秒=显示6让......?
package test;
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class singleplayer {
public JFrame frame;
private Timer pt ;
public String rndchar() {
Random r1 = new Random();
int b = r1.nextInt(26)+97;
char show1 =(char) b;
Random r2 = new Random();
int l = r2.nextInt(26)+65;
char show2 =(char) l;
if (r1.nextInt(2) % 2 == 0 )
{
return show1+"";
}
else if (r2.nextInt(2) % 2 == 1)
{
return show2+"";
}
return show2+"";
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
singleplayer window = new singleplayer();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public singleplayer() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(400, 180, 600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JDesktopPane desktopPane = new JDesktopPane();
desktopPane.setBackground(Color.WHITE);
frame.getContentPane().add(desktopPane, BorderLayout.CENTER);
Font rndchar = new Font("Old English Text MT", Font.PLAIN, 50);
JLabel lblAlphabet = new JLabel();
lblAlphabet.setText(rndchar());
lblAlphabet.setBounds(185, 168, 360, 94);
desktopPane.add(lblAlphabet);
lblAlphabet.setFont(rndchar);
JProgressBar progressBar = new JProgressBar();
progressBar.setForeground(Color.GREEN);
progressBar.setValue(100);
progressBar.setBounds(25, 78, 146, 14);
desktopPane.add(progressBar);
pt = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
progressBar.setValue(progressBar.getValue()-1);
if (progressBar.getValue() == 0) {
lblAlphabet.setText("Game Over !");
}
}
});
pt.start();
}
}