我正在做一個有多個結果的自選項目。我想要做的事情之一就是加入快速時間事件(QTEs),或者您必須在短時間內點擊按鈕或者遇難的遊戲中的事件。在這種情況下,我試圖做到這一點,以便在時間到了的時候,遊戲會說:「突然間你聽到了從你右邊傳來的蹄聲!」,此時調用了一個方法使GUI彈出用一個按鈕,你只需要幾秒鐘就可以點擊按鈕。如果您在適當的時間內點擊該按鈕,它應該繼續遊戲(以'CRACK !!'開頭)。如果沒有,遊戲應該結束了,並且會打印一條消息('GAME OVER - 你被Minotaur監獄看守殺死了,但是當我運行它時,GUI不會彈出,Game Over消息會打印三次在sleepLines延遲,然後將「裂紋!你快轉...」消息出現在這裏是我的代碼:在Java中使用GUI的快速事件
import java.util.Scanner;
import java.awt.Container;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class KCP1Main extends JPanel{
public static JButton AttackButton;
static Scanner myScanner = new Scanner(System.in);
static int quicktimecompletion = 0;
public static void main(String[] args) throws InterruptedException {
//insert leading up to this point here
System.out.println("Suddenly, you hear the clomping of hooves to your right!");
QuickTimeEvent(args);
sleepLines(500,2);
System.out.println("CRACK!!");
sleepLines(1000,1);
scrollText("You quickly turn right, swinging your metal pole at the same time... it swings into the head of ");
scrollText("a minotaur that was charging towards you, with enough force to knock him out.");
}
}
public static void QuickTimeEvent(String[] args) throws InterruptedException
{
for(int timer=0 ; timer<3 ; timer++){
ButtonHandler handler = new ButtonHandler();
AttackButton = new JButton("<!");
//static add(AttackButton);
AttackButton.addActionListener(handler);
sleepLines(1000,1);
if(quicktimecompletion == 1)
{
break;
}
if(timer > 3);
{
System.out.println("GAME OVER");
System.out.printf("You were slain by a Minotaur Prison Guard.");
}
}
}
public static class ButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if (event.getSource() == AttackButton)
{
quicktimecompletion = 1;
}
}
}
public static void scrollText(String message) throws InterruptedException{
for(int i = 0; i < message.length(); i++)
{
System.out.print(message.charAt(i));
Thread.sleep(62);
}
System.out.print("\n");
}
public static void JPanel(String[] args){
JFrame theGUI = new JFrame();
theGUI.setTitle("Incoming!");
KCP1Main makeButtons = new KCP1Main();
theGUI.add(makeButtons);
theGUI.setSize(300, 200);
theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
theGUI.setBackground(Color.red);
Container pane = theGUI.getContentPane();
theGUI.setVisible(true);
}
public static void sleepLines(int duration, int lines) throws InterruptedException{
for(int i=0; i < lines; i++){
Thread.sleep(duration);
System.out.println("");
}
}
}
很抱歉的直率,但這種貌似有人在太急於創造閱讀教程意識碼隨機流。接受我的建議,放棄這些代碼,全部選擇你想使用的任何GUI庫,無論是Swing還是更新的JavaFX,然後在嘗試與庫創建複雜程序之前先學習這些教程。如果你遵循這個規定,你將會拯救你自己(和我們)很多的悲傷。 –