本文介紹了Java Swing計(jì)時(shí)器未清除的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
我在使用Java Swing的計(jì)時(shí)器功能時(shí)遇到了一些問(wèn)題。我對(duì)Java編程相當(dāng)陌生,因此非常感謝您的幫助。我已經(jīng)在這個(gè)網(wǎng)站上看了很多其他的計(jì)時(shí)器問(wèn)題,但沒(méi)有一個(gè)回答我的問(wèn)題。我已經(jīng)做了一個(gè)圖形用戶界面,允許你玩石頭布剪刀,你可以點(diǎn)擊三個(gè)按鈕進(jìn)行選擇。我希望我的程序在您單擊按鈕后休眠大約1秒,并在它顯示一條消息后再次休眠。在我意識(shí)到Thad.sleep()不適用于我的圖形用戶界面后,我嘗試實(shí)現(xiàn)了一個(gè)計(jì)時(shí)器。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;
import java.io.*;
public class rps {
//ROCK PAPER SCISSORS
static JLabel middle = new JLabel();
static JLabel them = new JLabel();
static JLabel yourWins = new JLabel();
static JLabel theirWins = new JLabel();
static JPanel yourPanel = new JPanel();
static JPanel middlePanel = new JPanel();
static JLabel blank1 = new JLabel();
static JLabel blank2 = new JLabel();
static JButton rock = new JButton("Rock");
static JButton paper = new JButton("Paper");
static JButton scissors = new JButton("Scissors");
static int yw = 0;
static int tw = 0;
static ButtonHandler listener = new ButtonHandler();
public static void main(String[] args) {
//Create the frame
JFrame frame = new JFrame("Rock Paper Scissors");
frame.setSize(500, 500); //Setting the size of the frame
middle.setFont(new Font("Serif", Font.PLAIN, 30));
middle.setHorizontalAlignment(SwingConstants.CENTER);
them.setFont(new Font("Serif", Font.PLAIN, 15));
them.setHorizontalAlignment(SwingConstants.CENTER);
yourWins.setHorizontalAlignment(SwingConstants.CENTER);
theirWins.setHorizontalAlignment(SwingConstants.CENTER);
//Creating panels
JPanel bigPanel = new JPanel();
Border border = BorderFactory.createLineBorder(Color.BLACK, 1);
Border wlb = BorderFactory.createLineBorder(Color.RED, 1);
them.setBorder(border);
yourPanel.setBorder(border);
bigPanel.setBorder(border);
yourWins.setBorder(wlb);
theirWins.setBorder(wlb);
middlePanel.setBorder(border);
//Creating grid layouts
GridLayout yourGrid = new GridLayout(1,3,10,10);
GridLayout theirGrid = new GridLayout(1,1); //One row, one column
GridLayout middleGrid = new GridLayout(5,1);
GridLayout bigGrid = new GridLayout(3,1);//Two rows, one column
//Setting the layouts of each panel to the grid layouts created above
yourPanel.setLayout(yourGrid); //Adding layout to buttons panel
them.setLayout(theirGrid); //Adding layout to label panel
middlePanel.setLayout(middleGrid);
bigPanel.setLayout(bigGrid);
//Adding r/p/s to your grid.
yourPanel.add(rock);
yourPanel.add(paper);
yourPanel.add(scissors);
//Adding w/l rations to middlegrid.
middlePanel.add(theirWins);
middlePanel.add(blank1);
middlePanel.add(middle);
middlePanel.add(blank2);
middlePanel.add(yourWins);
//Attaching the listener to all the buttons
rock.addActionListener(listener);
paper.addActionListener(listener);
scissors.addActionListener(listener);
bigPanel.add(them);
bigPanel.add(middlePanel);
bigPanel.add(yourPanel);
//Shows the score at 0-0.
yourWins.setText("Your wins: " + yw);
theirWins.setText("Their wins: " + tw);
frame.getContentPane().add(bigPanel); //panel to frame
frame.setVisible(true); // Shows frame on screen
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//Class represents what do when a button is pressed
private static class ButtonHandler implements ActionListener {
public void actionPerformed (ActionEvent e) {
Timer timer = new Timer(1000, this);
String tc = random();
them.setText("They chose: " + tc + "!");
if (e.getSource() == rock) {
whoWins("rock", tc);
} else if (e.getSource() == paper) {
whoWins("paper", tc);
} else if (e.getSource() == scissors) {
whoWins("scissors", tc);
}
yourWins.setText("Your wins: " + yw);
theirWins.setText("Their wins: " + tw);
timer.setRepeats(false);
timer.start();
}
}
public static String random() {
int random = (int) (Math.random() * 3);
if (random == 0) {
return "Rock";
} else if (random == 1) {
return "Paper";
} else if (random == 2) {
return "Scissors";
}
return "";
}
public static void whoWins(String yc, String tc) {
if (yc.equals("rock")) {
if (tc.equals("Rock")) {
middle.setText("It's a tie!");
} else if (tc.equals("Paper")) {
middle.setText("You lose!");
tw++;
} else if (tc.equals("Scissors")) {
middle.setText("You win!");
yw++;
}
} else if (yc.equals("paper")) {
if (tc.equals("Rock")) {
middle.setText("You win!");
yw++;
} else if (tc.equals("Paper")) {
middle.setText("It's a tie!");
} else if (tc.equals("Scissors")) {
middle.setText("You lose!");
tw++;
}
} else if (yc.equals("scissors")) {
if (tc.equals("Rock")) {
middle.setText("You lose!");
tw++;
} else if (tc.equals("Paper")) {
middle.setText("You win!");
yw++;
} else if (tc.equals("Scissors")) {
middle.setText("It's a tie!");
}
}
}
}
實(shí)際發(fā)生的情況是,從我按下按鈕到顯示一條消息沒(méi)有延遲,因?yàn)楹苊黠@我沒(méi)有正確使用計(jì)時(shí)器。我希望計(jì)時(shí)器只運(yùn)行一次,運(yùn)行后代碼將執(zhí)行。但是,當(dāng)我單擊一個(gè)按鈕時(shí),計(jì)時(shí)器將在Repeat上運(yùn)行,盡管setRepeats為假。因此,我想要顯示的消息不會(huì)被延遲,而是會(huì)立即顯示,但隨后會(huì)循環(huán)顯示一條消息(該消息是隨機(jī)的),直到我關(guān)閉程序。如果我再次點(diǎn)擊按鈕,它似乎會(huì)使計(jì)時(shí)器的速度加倍,消息的顯示速度也會(huì)快一倍,以此類(lèi)推。
them.setText("They chose: " + tc + "!");
這是在Repeat上顯示的消息,變量TC每次都會(huì)更改。計(jì)時(shí)器似乎只是在每隔計(jì)時(shí)器間隔(1s)顯示此消息。
如有任何幫助,我們將不勝感激。
編輯:
所以我添加了這個(gè)部分:
private static class ButtonHandler implements ActionListener {
public void actionPerformed (ActionEvent e) {
// I'd be disabling the buttons here to prevent
// the user from trying to trigger another
// update...
// This is an instance field which is used by your
// listener
Timer timer = new Timer(1000, listenert);
timer.setRepeats(false);
timer.start();
}
}
private static class timer implements ActionListener {
public void actionPerformed (ActionEvent e) {
String tc = random(); //A method that chooses a random word.
them.setText("They chose: " + tc + "!");
if (e.getSource() == rock) {
whoWins("rock", tc); //whoWins is a method that will display a message.
} else if (e.getSource() == paper) {
whoWins("paper", tc);
} else if (e.getSource() == scissors) {
whoWins("scissors", tc);
}
yourWins.setText("Your wins: " + yw);
theirWins.setText("Their wins: " + tw);
// Start another Timer here that waits 1 second
// and re-enables the other buttons...
}
}
所以我認(rèn)為現(xiàn)在發(fā)生的事情是,當(dāng)我單擊一個(gè)按鈕時(shí),按鈕處理程序偵聽(tīng)器啟動(dòng)定時(shí)器,該定時(shí)器附加到定時(shí)器偵聽(tīng)器(名為listenert),該定時(shí)器將運(yùn)行Timer類(lèi)的action Perform中的代碼。但是,睡眠功能仍然不起作用
編輯2.5版:
private static class ButtonHandler implements ActionListener {
public void actionPerformed (ActionEvent e) {
final JButton button = (JButton)e.getSource();
Timer timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
String tc = random();
them.setText("They chose: " + tc + "!");
if (button == rock) {
whoWins("rock", tc);
} else if (button == paper) {
whoWins("paper", tc);
} else if (button == scissors) {
whoWins("scissors", tc);
}
yourWins.setText("Your wins: " + yw);
theirWins.setText("Their wins: " + tw);
}
});
timer.setRepeats(false);
timer.start();
}
}
這就是我到目前為止所擁有的,我只需要在她的睡眠之后再增加一點(diǎn)
The.setText(“他們選擇:”+TC+”!”);
我應(yīng)該把計(jì)時(shí)器放在哪里?如果有,我會(huì)把它放在哪里?Timer.start()位于方法的末尾,我不太理解。
推薦答案
因此,您提供給Timer
的ActionListener
會(huì)在計(jì)時(shí)器”滴答”時(shí)收到通知,因此ButtonHandler
actionPerformed
應(yīng)該看起來(lái)更像…
public void actionPerformed (ActionEvent e) {
// I'd be disabling the buttons here to prevent
// the user from trying to trigger another
// update...
// This is an instance field which is used by your
// listener
choice = e.getSource();
Timer timer = new Timer(1000, listener);
timer.setRepeats(false);
timer.start();
}
和您的listener
應(yīng)該更像
public void actionPerformed (ActionEvent e) {
String tc = random(); //A method that chooses a random word.
them.setText("They chose: " + tc + "!");
if (choice == rock) {
whoWins("rock", tc); //whoWins is a method that will display a message.
} else if (choice == paper) {
whoWins("paper", tc);
} else if (choice == scissors) {
whoWins("scissors", tc);
}
yourWins.setText("Your wins: " + yw);
theirWins.setText("Their wins: " + tw);
// Start another Timer here that waits 1 second
// and re-enables the other buttons...
}
例如…
您可以考慮查看How to use Swing Timers以了解更多詳細(xì)信息
更新
從一個(gè)簡(jiǎn)單的例子開(kāi)始…
公共類(lèi)TestPane擴(kuò)展JPanel{
private JLabel label;
private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
public TestPane() {
setLayout(new GridBagLayout());
label = new JLabel();
add(label);
tick();
Timer timer = new Timer(500, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
tick();
}
});
timer.start();
}
protected void tick() {
label.setText(sdf.format(new Date()));
}
}
這只是每隔半秒調(diào)用tick
方法以更新JLabel
…
上的時(shí)間
這篇關(guān)于Java Swing計(jì)時(shí)器未清除的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,