我的GUI不是';对于我的基于文本的冒险游戏,代码不会运行,但我所做的选择按钮不会运行;不行。为什么?Java Swing
我正在尝试用GUI制作一个基于文本的冒险游戏,一切都进行得很顺利,直到我遇到了一个问题,我的开始按钮可以工作,但我的选择按钮不能工作。我正在关注YouTube教程()并在10:00左右陷入困境,有人能帮我吗?这是我的密码: 我几乎可以肯定,我的问题在我的故事和游戏课程中的某个地方,但为了以防万一,我把其余的都包括进去了。如果有帮助的话,我正在使用Eclipse我的GUI不是';对于我的基于文本的冒险游戏,代码不会运行,但我所做的选择按钮不会运行;不行。为什么?Java Swing,java,swing,user-interface,Java,Swing,User Interface,我正在尝试用GUI制作一个基于文本的冒险游戏,一切都进行得很顺利,直到我遇到了一个问题,我的开始按钮可以工作,但我的选择按钮不能工作。我正在关注YouTube教程()并在10:00左右陷入困境,有人能帮我吗?这是我的密码: 我几乎可以肯定,我的问题在我的故事和游戏课程中的某个地方,但为了以防万一,我把其余的都包括进去了。如果有帮助的话,我正在使用Eclipse ``` package package01; import java.awt.event.ActionEvent; import ja
```
package package01;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Game {
ChoiceHandler cHandler = new ChoiceHandler();
UI ui = new UI();
VisibilityManager vm = new VisibilityManager(ui);
Story story = new Story(this,ui,vm);
String nextPosition1, nextPosition2, nextPosition3, nextPosition4;
public static void main(String[] args) {
new Game();
}
public Game() {
ui.makeUI(cHandler);
story.defaultSetup();
vm.showTitleScreen();
}
public class ChoiceHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
String yourChoice = event.getActionCommand();
switch(yourChoice) {
case"Start": vm.TitleToStart(); story.WasteLand(); break;
case "c1": story.selectPosition(nextPosition1);break;
case "c2": story.selectPosition(nextPosition2);break;
case "c3": story.selectPosition(nextPosition3); break;
case "c4": story.selectPosition(nextPosition4); break;
}
}
}
}
```
package package01;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import package01.Game.ChoiceHandler;
public class UI {
JFrame window;
JPanel titleNamePanel, startButtonPanel, mainTextPanel, choiceButtonPanel, playerPanel;
JLabel titleNameLabel, hpLabel, hpLabelNumber, weaponLabel, weaponLabelName;
JButton startButton, choice1,choice2,choice3,choice4;
JTextArea mainTextArea;
Font titleFont = new Font("Times New Roman", Font.PLAIN, 90);
Font normalFont = new Font("Times New Roman", Font.PLAIN, 28);
public void makeUI(ChoiceHandler cHandler) {
//WINDOW
window = new JFrame();
window.setSize(800, 600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setBackground(Color.black);
window.setLayout(null);
//TITLE
titleNamePanel = new JPanel();
titleNamePanel.setBounds(100, 100, 600, 150);
titleNamePanel.setBackground(Color.black);
titleNameLabel = new JLabel("ADVENTURE");
titleNameLabel.setForeground(Color.white);
titleNameLabel.setFont(titleFont);
titleNamePanel.add(titleNameLabel);
startButtonPanel = new JPanel();
startButtonPanel.setBounds(300, 400, 200, 100);
startButtonPanel.setBackground(Color.black);
startButton = new JButton("START");
startButton.setBackground(Color.black);
startButton.setForeground(Color.white);
startButton.setFont(normalFont);
startButton.setFocusPainted(false);
startButton.addActionListener(cHandler);
startButton.setActionCommand("Start");
startButtonPanel.add(startButton);
window.add(titleNamePanel);
window.add(startButtonPanel);
//GAME SCREEN
mainTextPanel = new JPanel();
mainTextPanel.setBounds(100, 100, 600, 250);
mainTextPanel.setBackground(Color.black);
window.add(mainTextPanel);
mainTextArea = new JTextArea();
mainTextArea.setBounds(100, 100, 600, 250);
mainTextArea.setBackground(Color.black);
mainTextArea.setForeground(Color.white);
mainTextArea.setFont(normalFont);
mainTextArea.setLineWrap(true);
mainTextPanel.add(mainTextArea);
choiceButtonPanel = new JPanel();
choiceButtonPanel.setBounds(250, 350, 300, 150);
choiceButtonPanel.setBackground(Color.black);
choiceButtonPanel.setLayout(new GridLayout(4,1));
window.add(choiceButtonPanel);
choice1 = new JButton("Choice 1");
choice1.setBackground(Color.black);
choice1.setForeground(Color.white);
choice1.setFont(normalFont);
choice1.setFocusPainted(false);
choice1.addActionListener(cHandler);
choice1.setActionCommand("c1");
choiceButtonPanel.add(choice1);
choice2 = new JButton("Choice 2");
choice2.setBackground(Color.black);
choice2.setForeground(Color.white);
choice2.setFont(normalFont);
choice2.setFocusPainted(false);
choice1.addActionListener(cHandler);
choice2.setActionCommand("c2");
choiceButtonPanel.add(choice2);
choice3 = new JButton("Choice 3");
choice3.setBackground(Color.black);
choice3.setForeground(Color.white);
choice3.setFont(normalFont);
choice3.setFocusPainted(false);
choice1.addActionListener(cHandler);
choice3.setActionCommand("c3");
choiceButtonPanel.add(choice3);
choice4 = new JButton("Choice 4");
choice4.setBackground(Color.black);
choice4.setForeground(Color.white);
choice4.setFont(normalFont);
choice4.setFocusPainted(false);
choice1.addActionListener(cHandler);
choice4.setActionCommand("c4");
choiceButtonPanel.add(choice4);
playerPanel = new JPanel();
playerPanel.setBounds(100, 15, 600, 50);
playerPanel.setBackground(Color.black);
playerPanel.setLayout(new GridLayout(1,4));
window.add(playerPanel);
hpLabel = new JLabel("HP:");
hpLabel.setFont(normalFont);
hpLabel.setForeground(Color.white);
playerPanel.add(hpLabel);
hpLabelNumber = new JLabel();
hpLabelNumber.setFont(normalFont);
hpLabelNumber.setForeground(Color.white);
playerPanel.add(hpLabelNumber);
weaponLabel = new JLabel("Weapon:");
weaponLabel.setFont(normalFont);
weaponLabel.setForeground(Color.white);
playerPanel.add(weaponLabel);
weaponLabelName = new JLabel();
weaponLabelName.setFont(normalFont);
weaponLabelName.setForeground(Color.white);
playerPanel.add(weaponLabelName);
window.setVisible(true);
}
}
为什么要多次将choice1.setActionListner设置为cHandler?“我正在关注YouTube教程”-就我个人而言,我会避免它-它似乎没有给你提供很多好的想法-天哪,你解决了我的问题。我没有检查就复制并粘贴了它。谢谢
package package01;
public class VisibilityManager {
UI ui;
public VisibilityManager(UI userInterface) {
ui = userInterface;
}
public void showTitleScreen() {
//Show the title screen
ui.titleNamePanel.setVisible(true);
ui.startButtonPanel.setVisible(true);
//Hide the game screen
ui.mainTextPanel.setVisible(false);
ui.choiceButtonPanel.setVisible(false);
ui.playerPanel.setVisible(false);
}
public void TitleToStart() {
//hide title screen
ui.titleNamePanel.setVisible(false);
ui.startButtonPanel.setVisible(false);
//show game screen
ui.mainTextPanel.setVisible(true);
ui.choiceButtonPanel.setVisible(true);
ui.playerPanel.setVisible(true);
}
}
```
package package01;
public class VisibilityManager {
UI ui;
public VisibilityManager(UI userInterface) {
ui = userInterface;
}
public void showTitleScreen() {
//Show the title screen
ui.titleNamePanel.setVisible(true);
ui.startButtonPanel.setVisible(true);
//Hide the game screen
ui.mainTextPanel.setVisible(false);
ui.choiceButtonPanel.setVisible(false);
ui.playerPanel.setVisible(false);
}
public void TitleToStart() {
//hide title screen
ui.titleNamePanel.setVisible(false);
ui.startButtonPanel.setVisible(false);
//show game screen
ui.mainTextPanel.setVisible(true);
ui.choiceButtonPanel.setVisible(true);
ui.playerPanel.setVisible(true);
}
}