在 java swing 中创建石头、纸、剪刀游戏的建议 [英] Advice for creating a rock, paper, scissors game in java swing
问题描述
我正在自学 Java,我想创建一个带有 GUI 的石头剪刀布游戏.我已经使用扫描仪创建了一个基于文本的版本,但我还有很多工作要做.
I'm teaching myself Java and I want to create a rock, paper, scissors game with a GUI. I have created a text based version with scanners, but I have a lot of work to go.
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
public class RPS extends JFrame {
JRadioButton rock, paper, scissors;
ButtonGroup choices;
public static void main(String[] args) {
new RPS();
}
public RPS() {
super("Rock, Paper, Scissors");
setSize(400,300);
setResizable(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel p=new JPanel();
rock = new JRadioButton("Rock");
paper = new JRadioButton("Paper");
scissors = new JRadioButton("Scissors");
choices = new ButtonGroup();
choices.add(rock);
choices.add(paper);
choices.add(scissors);
p.add(rock);
p.add(paper);
p.add(scissors);
add(p);
setVisible(true);
}
}
这是我的代码.我已经让它创建了一个窗口并显示了 3 个单选按钮,这些按钮只允许选择一个选项.从这里开始,我想实现一个下一步按钮,并创建逻辑以根据两个选择生成答案.我相信我需要卡片布局,但 Oracle 文档对我没有帮助.我也不知道如何实现逻辑.任何帮助表示赞赏,抱歉长篇幅.再次感谢!
Here is my code. I already have it creating a window and displaying 3 radio buttons that will only allow one choice to be selected. From here, I want to implement a next button, and create logic to produce an answer based on both choices. I believe I need a card layout, but the Oracle documentation doesn't help me. I also have no idea how I can go around implementing logic. Any help is appreciated, sorry for the long post. Thanks again!
抱歉我没说清楚,我想设计这个让一个人轮流,按嵌套按钮,然后第二个人轮流,按完成并得到结果.我会把这个展示给我 8 年级的班级.
I'm sorry I didn't make it clear, I want to design this for one person to take a turn, press the nest button, then the second person will take a turn, press finish and get the results. I will present this to my 8th grade class.
推荐答案
这是在您的程序中实现 CardLayout
的热点.此布局的目的是对组件进行分层.在您的情况下,您需要为每个玩家设置一个面板.所以你需要两个面板.
Here's hot to implement CardLayout
into your program. The purpose of this layout is to layer components. In your case, you would need a panel for each player. So you would need two panels.
- 播放器 1 的 JPanel(带有三个单选按钮)
- 播放器 2 的 JPanel(带有三个单选按钮)
以上两个是组成Card(layered)Layout
(你可以将一个堆叠在另一个之上.
The above two are what comprise the Card(layered)Layout
(you would stack one on top of the other.
- 您需要一个标签来显示获胜者
- 你需要一个 JPanel 来保存 Next Button
所以总的来说,你的布局应该是这样的
So all together, you layout should look like this
Wrapped in JPanel(new BoderLayout())
-------------------------------------
| label to show status | BorderLayout.NORTH
-------------------------------------
| |
| CardLayout holding |
| two JPanels with RBs | BorderLayout.CENTER
|___________________________________|
|(JPanel) Next JButton | BorderLayout.SOUTH
-------------------------------------
点击next按钮时,可以调用CardLayout
的next()
方法显示下一个面板
When you click the next button, you can call the next()
method of the CardLayout
to show the next panel
示例
private CardLayout cardLayout = new CardLayout(10, 10); // hgap and vgap args
private JPanel cardPanel = new JPanel(cardLayout);
JPanel panel1 = new JPanel(); // holds first player
JPanel panel2 = new JPanel(); // holds second player
cardPanel.add(panel1);
cardPanel.add(panel2);
nextButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cardLayout.next(cardPanel);
}
});
有关其他移动,请参阅 CardLayout 文档方法
See CardLayout docs for other movement methods
逻辑部分
- 正如我之前建议的,你应该有六个单选按钮(每个玩家三个)
- 在你的逻辑中,你可以检查哪些被选中
- 并且您可能需要 checkWinner JButton 来执行操作.
示例
JRadioButton p1Scissors = new JRadioButton("Scissors");
JRadioButton p1Paper = new JRadioButton("Paper");
JRadioButton p1Rock = new JRadioButton("Rock");
// group them
JRadioButton p2Scissors = new JRadioButton("Scissors");
JRadioButton p2Paper = new JRadioButton("Paper");
JRadioButton p2Rock = new JRadioButton("Rock");
// group them
JLabel statusLabel = new JLabel(" ");
JButton checkWinner = new JButton("Check Winner"); // You can add to bottom panel
checkWinner.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if (p1Scissors.isSelected() && p2Rock.isSelected()){
statusLabel.setText("Player 2 wins: Rock beats Scissors");
} else if ( ..... ) {
...
}
...
}
});
这篇关于在 java swing 中创建石头、纸、剪刀游戏的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!