【问题标题】:Memory/Concentration Game Problems记忆/注意力游戏问题
【发布时间】:2012-11-25 01:32:17
【问题描述】:

所以,我一直在做 Java 记忆/注意力游戏作业。我还没有达到我想要的程度,它只完成了一半,但我确实让 GUI 大部分工作......直到我尝试将单选按钮添加到我的框架。我认为问题可能是因为我将 JFrame(CardButtonPanelFrame) 更改为 JPanel。我正在尝试将 3 个 JPanel 添加到我添加到 JFrame 的较大 JPanel 中。当我以前弹出所有 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()
}
}

所以我还有两个课程,但我认为当前的问题就在这里,我害怕把它变成一堵巨大的代码墙。我还有更多问题,但我想我会一次解决一个问题,这样我就不会最终发布一页又一页没人愿意阅读的代码。

【问题讨论】:

  • 添加到底是什么问题?
  • 它基本上是另一个,“这是我的代码,请为我修复它”。抱歉,但这不是这里的工作方式。请花一些时间详细说明您的问题,以便我们能够充分理解并提供帮助。
  • 当我尝试将 wtf 面板和 CardButtonPanelFrame 放入更大的面板时,除了弹出一个空窗口外,什么也没有。我以前只有 CardButtonPanelFrame 本身,而且效果很好。
  • 对不起,我试图简洁,因为我可能过于罗嗦。我最大的希望是人们会诚实地善良。不是为我“解决”问题。
  • 很好很好,但有帮助更好。请向我们提供足够的信息,以便我们提供帮助。

标签: java swing memory


【解决方案1】:

您正在做的一个问题是在添加组件之前在您的 JFrame 上调用pack()。不要那样做,而是先添加所有可以添加的组件,然后调用pack(),然后调用setVisible(true)

【讨论】:

  • 这很有帮助。我一直认为这就像一种开/关的东西。就像在Matlab中坚持!谢谢。
  • @Confused:不,这是一次性交易,并在该时间点打包 GUI,无论 GUI 是否准备好打包。
猜你喜欢
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-14
  • 2014-03-24
  • 2012-05-05
相关资源
最近更新 更多