【发布时间】:2021-02-02 15:20:31
【问题描述】:
所以我试图用java创建一个简单的游戏来练习,但我一直遇到问题。 应该出现在程序上的第一个 JLabel(text) 没有显示,我不知道该怎么做。我希望它与其他 JLabel 无关,因为那会搞砸我的整个游戏。 有谁知道为什么它没有出现?
另外,如果您有更多问题,请随时在下面发表评论。 我为我糟糕的英语道歉
我的代码
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class MyGame extends JFrame {
JButton wwBtnSelect;
JButton wwBtnNo;
JButton wwBtnExit;
JButton wwBtnYes;
MyGame(){
// creates the window
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
this.setVisible(true);
this.setTitle("LoL Game");
this.setResizable(false);
this.setLayout(null);
this.setLocationRelativeTo(null);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenu helpMenu = new JMenu("Help");
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(e -> System.exit(0));
fileMenu.add(exitItem);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
this.setJMenuBar(menuBar);
this.setVisible(true);
// creates the text label
JLabel label = new JLabel();
label.setText("Choose your Champion");
label.setBounds(170, 50, 134, 100);
label.setVisible(true);
//creates buttons
wwBtnSelect = new JButton("Warwick");
wwBtnNo = new JButton();
wwBtnNo.setVisible(false);
wwBtnNo.setBorder(BorderFactory.createLineBorder(Color.WHITE));
wwBtnYes = new JButton();
wwBtnYes.setVisible(false);
wwBtnYes.setBorder(BorderFactory.createLineBorder(Color.WHITE));
wwBtnYes.setFocusable(false);
wwBtnYes.setForeground(Color.WHITE);
wwBtnYes.setBackground(Color.BLACK);
wwBtnYes.setOpaque(true);
wwBtnExit = new JButton();
wwBtnExit.setVisible(false);
wwBtnExit.setFocusable(false);
wwBtnExit.setForeground(Color.WHITE);
wwBtnExit.setBackground(Color.BLACK);
wwBtnExit.setOpaque(true);
wwBtnExit.setBorder(BorderFactory.createLineBorder(Color.WHITE));
wwBtnSelect.setBounds(170, 150, 134, 50);
wwBtnSelect.setFocusable(false);
wwBtnSelect.setForeground(Color.WHITE);
wwBtnSelect.setBackground(Color.BLACK);
wwBtnSelect.setOpaque(true);
wwBtnSelect.setBorder(BorderFactory.createLineBorder(Color.WHITE));
wwBtnSelect.addActionListener(
e -> {
label.setBounds(120, 75, 250, 50);
label.setText("<html><div style='text-align: center;'>Oh, a Lee Sin just challenged you to a 1v1! <br/>Will you take the fight?</div></html>");
wwBtnYes.setVisible(true);
wwBtnYes.setText("Yes");
wwBtnYes.setBounds(170, 150, 134, 50);
wwBtnNo.setVisible(true);
wwBtnSelect.setVisible(false);
}
);
// player refuses the challenge
wwBtnNo.setText("No");
wwBtnNo.setBounds(170, 200, 134, 50);
wwBtnNo.setFocusable(false);
wwBtnNo.setForeground(Color.WHITE);
wwBtnNo.setBackground(Color.BLACK);
wwBtnNo.setOpaque(true);
wwBtnNo.addActionListener(
e -> {
wwBtnYes.setVisible(false);
wwBtnNo.setVisible(false);
label.setText("<html><div style='text-align: center;'>What a coward.</div></html>");
label.setBounds(190, 75, 250, 50);
wwBtnExit.setVisible(true);
wwBtnExit.setText("Exit");
wwBtnExit.setBounds(170, 200, 134, 50);
wwBtnExit.addActionListener(r -> this.dispose());
}
);
//player accepts the challange
ImageIcon QAbility = new ImageIcon( getClass().getResource("images/Jaws_of_the_Beast.png"));
ImageIcon WAbility = new ImageIcon( getClass().getResource("images/Blood_Hunt.png"));
ImageIcon EAbility = new ImageIcon( getClass().getResource("images/Primal_Howl.png"));
ImageIcon RAbility = new ImageIcon( getClass().getResource("images/Infinite_Duress.png"));
JLabel Qability = new JLabel();
Qability.setText("Q");
Qability.setIcon(QAbility);
Qability.setVisible(false);
Qability.setBounds(90, 350, 80, 80);
Qability.setHorizontalTextPosition(JLabel.CENTER);
Qability.setVerticalTextPosition(JLabel.TOP);
JLabel Wability = new JLabel();
Wability.setText("W");
Wability.setIcon(WAbility);
Wability.setVisible(false);
Wability.setBounds(160, 350, 80, 80);
Wability.setHorizontalTextPosition(JLabel.CENTER);
Wability.setVerticalTextPosition(JLabel.TOP);
JLabel Eability = new JLabel();
Eability.setText("E");
Eability.setIcon(EAbility);
Eability.setVisible(false);
Eability.setBounds(230, 350, 80, 80);
Eability.setHorizontalTextPosition(JLabel.CENTER);
Eability.setVerticalTextPosition(JLabel.TOP);
JLabel Rability = new JLabel();
Rability.setText("R");
Rability.setIcon(RAbility);
Rability.setVisible(false);
Rability.setBounds(300, 350, 80, 80);
Rability.setHorizontalTextPosition(JLabel.CENTER);
Rability.setVerticalTextPosition(JLabel.TOP);
wwBtnYes.addActionListener(
r -> {
wwBtnNo.setVisible(false);
wwBtnYes.setVisible(false);
label.setVisible(false);
Qability.setVisible(true);
Wability.setVisible(true);
Eability.setVisible(true);
Rability.setVisible(true);
}
);
// adds the elements
this.add(label);
this.add(wwBtnSelect);
this.add(wwBtnNo);
this.add(wwBtnExit);
this.add(wwBtnYes);
this.add(Qability);
this.add(Wability);
this.add(Eability);
this.add(Rability);
}
}
【问题讨论】:
-
我们不知道您的“第一个标签”是什么。白手起家。使用“您的第一个 JLabel”创建一个 JFrame 以确保它显示。然后添加另一个 JLabel 并重新测试以确保它显示。重复该过程。当标签停止显示时,您就发现了问题。关键是在开始测试之前不要编写整个应用程序。逐步进行测试。
-
另外,变量名不应以大写字符开头。学习并遵循 Java 约定。这些约定可以在任何教科书或在线教程中找到。
-
使用 CardLayout 为游戏的每个阶段交换面板,而不是玩弄组件的可见性。见:How to Use a CardLayout
-
感谢您的回复!!我开始使用 CardLayout,一切似乎都很好:DD
标签: java swing jlabel layout-manager null-layout-manager