Java 记忆/专注游戏问题
所以,我一直在做一个Java内存/注意力游戏任务。我还没有达到我想要的程度,它只完成了一半,但我确实让GUI大部分工作正常。。。直到我尝试在我的框架中添加单选按钮。我认为问题可能是因为我将JFrame(CardButtonPanelFrame)更改为JPanel。我试图将3个JPanel添加到一个更大的JPanel中,我将其添加到一个JFrame中。我刚看到一个小的空白窗口弹出时,我曾经有所有的52张卡片弹出 基本上,当我从事项目时,事情的复杂性会失控,所以我想我来这里是为了确保我朝着正确的方向前进 以下是我的主要观点:Java 记忆/专注游戏问题,java,swing,memory,Java,Swing,Memory,所以,我一直在做一个Java内存/注意力游戏任务。我还没有达到我想要的程度,它只完成了一半,但我确实让GUI大部分工作正常。。。直到我尝试在我的框架中添加单选按钮。我认为问题可能是因为我将JFrame(CardButtonPanelFrame)更改为JPanel。我试图将3个JPanel添加到一个更大的JPanel中,我将其添加到一个JFrame中。我刚看到一个小的空白窗口弹出时,我曾经有所有的52张卡片弹出 基本上,当我从事项目时,事情的复杂性会失控,所以我想我来这里是为了确保我朝着正确的方向
import javax.swing.*;
public class Project3{
public static void main(String[] args){
JFrame frame = new JFrame();
Grid game = new Grid();
frame.pack();
frame.add(game);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
这是最外面的JPAND,我想在顶端保持单选按钮,中间的卡片,最后是底部的分数。
import java.awt.*;
import javax.swing.*;
public class Grid extends JPanel{
public Grid(){
JPanel panel = new JPanel(); //construct a frame
CardButtonPanelFrame buttons = new CardButtonPanelFrame();
wtf choices = new wtf();
panel.setLayout(new GridLayout(3,1)); //that panel uses GridLayout
panel.add(choices);//add the panels to the Frame
panel.add(buttons);
//frame.add(scores);
add(panel);
setVisible(true);
}
}
这是单选按钮面板。。。命名为wtf是因为我遇到一些编译问题,并尝试更改名称。我甚至还没有弄清楚如何实现不同的玩家数量
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class wtf extends JPanel implements ActionListener {
static String zerostring = "Zero Player Game";
static String onestring = "One Player Game";
static String twostring = "Two Player Game";
public wtf() {
super(new BorderLayout());
//Create the radio buttons.
JRadioButton zeroButton = new JRadioButton(zerostring);
zeroButton.setMnemonic(KeyEvent.VK_C);
zeroButton.setActionCommand(zerostring);
JRadioButton oneButton = new JRadioButton(onestring);
oneButton.setMnemonic(KeyEvent.VK_B);
oneButton.setActionCommand(onestring);
oneButton.setSelected(true);
JRadioButton twoButton = new JRadioButton(twostring);
twoButton.setMnemonic(KeyEvent.VK_D);
twoButton.setActionCommand(twostring);
//Group the radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(zeroButton);
group.add(oneButton);
group.add(twoButton);
//Register a listener for the radio buttons.
zeroButton.addActionListener(this);
oneButton.addActionListener(this);
twoButton.addActionListener(this);
//Put the radio buttons in a column in a panel.
JPanel radioPanel = new JPanel(new GridLayout(0, 1));
radioPanel.add(zeroButton);
radioPanel.add(oneButton);
radioPanel.add(twoButton);
add(radioPanel, BorderLayout.LINE_START);
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}
/** Listens to the radio buttons. */
public void actionPerformed(ActionEvent e) {
//do something with e.getActionCommand()
}
}
所以我还有两个类,但我认为当前的问题就在这里,我担心这会成为一堵巨大的代码墙。我还有更多的问题,但我想我会一次只问一个问题,这样我就不会发布一页又一页没有人愿意阅读的代码。您正在做的一个问题是在添加组件之前在JFrame上调用
pack()
。不要这样做,而是先添加所有可以添加的组件,然后调用pack()
,然后setVisible(true)
添加到底是什么问题?基本上是另一个问题,“这是我的代码,请帮我修复它”。对不起,但这里不是这样的。请花些时间详细说明您的问题,以便我们能够充分理解并提供帮助。当我尝试将wtf面板和CardButtonPanelFrame放入一个更大的面板时,只会弹出一个空窗口。我以前只有CardButtonPanelFrame本身,这很管用。对不起,我试图简明扼要,因为我可能会过于冗长。我最大的希望是人们会诚实地友善。不是为我“修理”东西。好是好,但有帮助更好。请给我们足够的信息,让我们可能会有所帮助。这是很有帮助的。我一直认为这是一种开/关式的事情。就像在Matlab中一样!谢谢。@Middle:不,这是一笔一次性交易,在那个时候打包GUI,不管GUI是否准备好打包。