0
嗨我創建一個程序,用戶需要提供某些輸入來獲得積分,並有一個計時器,但計時器是我創建的是使光標在jtextfield中閃爍得太快,以致程序有時需要輸入。在我的計時器類中,我導入了TimerTask並使用了timer.scheduleAtFixedRate()來計算時間。這不是我所有的代碼,但這是主要的類。jtextfield光標閃爍太快,並不時註冊輸入
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class Interface extends JPanel implements ActionListener {
private JTextField textField;
private int fontSize;
private Font font;
private GridBagConstraints c = new GridBagConstraints();
private int x, y;
private collection userInputData;
private boolean initialized = false;
private String[] answers;
private String[] questions;
private int points = 0;
private timer timer;
private boolean timerStarted = false;
Interface() {
super(new GridBagLayout());
x = 0;
y = 0;
userInputData = new collection();
setBackground(Color.BLACK);
timer = new timer(60);
questions = new String[] {"a", "b", "c", "d"};
answers = new String[] {"a","b","c","d","e","f","g" };
/*questions = new String[] {"雲", "人", "口", "一"};
answers = new String[] {"去","十","二","大","運","中","丁" };*/
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
fontSize = getWidth()/25;
Font font = new Font(g.getFont().getFontName(), Font.BOLD, fontSize);
g.setFont(font);
g.setColor(Color.WHITE);
if(initialized){
updateUserDataFrame();
updateFrame();
textField.requestFocus();
}
if (!timer.timeUp()) {
questions(g);
fontSize = getWidth()/50;
Font n = new Font(g.getFont().getFontName(), Font.BOLD, fontSize);
g.setFont(n);
displayTimer(g);
points(g);
displayUserInput(g);
}
if (timer.timeUp()) {
displayScoreboard(g);
remove(textField);
}
}
private void displayTimer(Graphics g) {
if (!timerStarted) {
timer.start();
timerStarted = true;
}
//fontSize = getWidth()/35;
timer.setColor(Color.RED);
timer.setPosition(getWidth() - (int) (getWidth()), fontSize);
timer.displayTimer(g);
repaint();
}
private void displayScoreboard(Graphics g) {
int x = getWidth()/50;
int y = getHeight()/5;
g.drawString("Your Answers:", x, y);
/* String correctWrongNumber = "Correct: " + correctAnswers + " "
+ "Wrong: " + (question.length - correctAnswers);
x = getWidth() - g.getFontMetrics().stringWidth(correctWrongNumber) - getWidth()/25;
g.drawString(correctWrongNumber, x, y);
x = getWidth()/50;
y += getHeight()/15 + fontSize;
for (int i = 0; i < studentAnswers.length; i++) {
g.drawString(i + 1 + ". " + studentAnswers[i], x, y);
x += getWidth()/10.5;
}*/
}
private void points(Graphics g) {
int x = getWidth()/4;
int y = getHeight()/4;
g.setColor(Color.white);
g.drawString("Points: " + points, x, y);
}
private void questions(Graphics g) {
int x = getWidth()/4;
int y = getHeight()/8;
int borderY = getHeight() /60;
fontSize = getWidth()/15;
Font font = new Font(g.getFont().getFontName(), Font.BOLD, fontSize);
g.setFont(font);
for(int i = 0; i < questions.length; i++) {
g.setColor(Color.red.brighter());
g.fillRect(x + 1, borderY, fontSize, fontSize);
g.setColor(Color.orange.brighter());
g.drawRect(x, borderY, fontSize, fontSize);
g.setColor(Color.white);
g.drawString(questions[i],x,y);
x += getWidth()/6;
}
}
private void updateFrame() {
fontSize = getWidth()/20;
font = new Font("SansSerif", Font.BOLD, fontSize);
textField.setFont(font);
c.insets = new Insets(0, 0, getHeight()/2, 0);
remove(textField);
add(textField, c);
}
private void updateUserDataFrame(){
int x = getWidth()/60;
int y = getHeight()/3 + getHeight()/20;
int width = getWidth()/16;
int height = getWidth()/16;
int fontSize = getWidth()/20;
Font font = new Font("SansSerif", Font.BOLD, fontSize);
userInputData.reset();
while(userInputData.hasNext()){
structure temp = (structure)userInputData.next();
x += getWidth()/16;
temp.updateData(font, x, y, width, height);
}
}
private void displayUserInput(Graphics g) {
userInputData.paint(g);
}
void init(){
x = getWidth()/60;
y = getHeight()/3 + getHeight()/20;
fontSize = getWidth()/20;
font = new Font("SansSerif", Font.BOLD, fontSize);
textField = new JTextField(3);
textField.setFont(font);
textField.addActionListener(this);
//Add Components to this panel.
c.insets = new Insets(0, 0, getHeight()/2, 0);
c.gridx = 0;
c.gridy = 0;
add(textField, c);
updateUI();
initialized = true;
repaint();
}
@Override
public void actionPerformed(ActionEvent e) {
int buttonWidth = getWidth()/16;
int buttonHeight = getWidth()/16;
x += getWidth()/16;
String userInput = textField.getText();
for(int i = 0; i < answers.length; i++) {
if(answers[i].equals(textField.getText()))
points++;
}
userInputData.add(new structure(userInput, Color.WHITE, font,x,y,buttonWidth,buttonHeight));
textField.setText("");
repaint();
}
}
編輯:Timer類
import java.awt.*;
import java.util.Timer;
import java.util.TimerTask;
class timer {
private Timer timer = new Timer();
private int timeLimit;
private int x, y;
private Color color;
private TimerTask task = new TimerTask() {
public void run() {
timeLimit--;
if(timeLimit == 0){
timer.cancel();
}
}
};
timer() {
timeLimit = 120;
}
timer(int limit) {
timeLimit = limit;
}
void start() {
timer.scheduleAtFixedRate(task, 1000, 1000);
}
void displayTimer(Graphics g) {
int minute = timeLimit/60;
int second = timeLimit % 60;
g.setColor(color);
if (minute < 10) {
if (second < 10)
g.drawString("0" + minute + ":0" + second, x, y);
else
g.drawString("0" + minute + ":" + second, x, y);
} else {
if (second < 10)
g.drawString(minute + ":0" + second, x, y);
else
g.drawString(minute + ":" + second, x, y);
}
}
void setColor(Color color) {
this.color = color;
}
void setPosition(int x, int y) {
this.x = x;
this.y = y;
}
int getTime() {
return timeLimit;
}
void setTime(int time) {
timeLimit = time;
}
boolean timeUp() {
return timeLimit == 0;
}
}
對不起,我做了一個單獨的計時器類。我會添加它。 –