java - 内存/注意力游戏问题

标签 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);

}
}

然后这是最外面的 JPanel,我想将单选按钮放在顶部,将卡片放在中间,最后将分数放在底部。

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)

关于java - 内存/注意力游戏问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13547473/

相关文章:

java - 如何将 *.doc 读入 JTextPane?

JavaScript 内存 : Heap, native ,WebGL/GPU

java - 检查 HashSet 是否包含特定子集

Java Swing 自定义文本 JEditorPane

Java 在负坐标上绘制图像

java - run 中不会调用 repaint()

c - 将结构指针指向 C 中动态分配内存的第一个 block

java - 如何设置 Firebase Storage 存储订阅???因此用户使用我的应用程序,但他们必须支付额外的存储费用

java - 使用 Mina 模拟 XMPP 服务器仅在部分时间有效

java - 列表项之间的差异