2012-11-25 37 views
0

所以,我一直在研究Java內存/集中游戲任務。我沒有達到我想要的程度,只有一半完成,但我的GUI大部分都在工作......直到我嘗試向我的Frame添加單選按鈕。我認爲問題可能是因爲我將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); 

} 
} 

這是單選按鈕面板......指定跆拳道,因爲我有一些問題,編制與更改名稱試驗。我甚至還沒有弄清楚如何實現不同的玩家數量。

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() 
} 
} 

所以我有兩個類,但我認爲目前的問題在這裏,我害怕使這個巨大的代碼牆。我有更多的問題,但我想我會一次一個接一個,所以我最終不會發布任何人不想閱讀的頁面和代碼頁。你正在做

+3

添加什麼問題是什麼呢? – 2012-11-25 01:34:40

+0

這基本上是另一個,「這是我的代碼,請爲我修復」。對不起,但這不是它在這裏的工作原理。請花點時間詳細說明您的問題,以便我們能夠充分理解並提供幫助。 –

+0

當我試圖把wtf面板和CardButtonPanelFrame放到一個更大的面板上時,只彈出一個空窗口。我以前只是自己有CardButtonPanelFrame,並且工作正常。 – Confused

回答

2

的一個問題是前添加組件調用你的JFrame pack()。不這樣做,而是補充說,可以先所有組件,然後電話pack(),然後setVisible(true)

+0

那是有幫助的。我一直認爲這就像一種開/關類的事情。喜歡在Matlab中保留!謝謝。 – Confused

+0

@Confused:不會,這是一次性交易,並且在該時間點包裝GUI,無論GUI是否已準備好打包。 –