Reputation: 77
I am making a card game application just for fun while I am bored over the summer. I am pretty new to JFrames so I decided to look at others code and edit it to what I want it to be. After looking at the answer on this url: How to arrange multiple panels in JFrame, I tried to replicate it to my needs. The issue is that when I run the program nothing shows up other than the title of the base JFrame but no functionality (such as selecting to see players hand). I cant seem to find the reason why none of the functionality is showing up.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PlayGame extends JFrame {
private static void createAndShowUI(){
HandGui gui = new HandGui();
HandMenu menu = new HandMenu(gui);
JFrame frame = new JFrame("Hands of Players");
frame.getContentPane().add(gui.getMainPanel());
frame.setJMenuBar(menu.getJMenuBar());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run(){
createAndShowUI();
}
});
}
private PlayGame(){
}
}
class HandGui {
private static final String[][] PLAYER1_BIN_TEXTS = {
{"Card 1", "Card 2", "Card 3" , "Card 4"}, {"Card 5", "Card 6", "Card 7", "Card 8"},
{"Card 9", "Card 10", "Card 11" , "Card 12"}, {"Card 13"}};
private static final String[][] PLAYER2_BIN_TEXTS = {
{"Card 1", "Card 2", "Card 3" , "Card 4"}, {"Card 5", "Card 6", "Card 7", "Card 8"},
{"Card 9", "Card 10", "Card 11" , "Card 12"}, {"Card 13"}};
private static final String[][] PLAYER3_BIN_TEXTS = {
{"Card 1", "Card 2", "Card 3" , "Card 4"}, {"Card 5", "Card 6", "Card 7", "Card 8"},
{"Card 9", "Card 10", "Card 11" , "Card 12"}, {"Card 13"}};
private static final String[][] PLAYER4_BIN_TEXTS = {
{"Card 1", "Card 2", "Card 3" , "Card 4"}, {"Card 5", "Card 6", "Card 7", "Card 8"},
{"Card 9", "Card 10", "Card 11" , "Card 12"}, {"Card 13"}};
private static final int GAP = 5;//
private static final Font BTN_FONT = new Font(Font.DIALOG, Font.BOLD, 20);
private JPanel mainPanel = new JPanel();
private JPanel p1Panel;
private JPanel p2Panel;
private JPanel p3Panel;
private JPanel p4Panel;
private JTextField display = new JTextField();//May not need
HandGui() {
display.setFont(BTN_FONT);
p1Panel = createBtnPanel(PLAYER1_BIN_TEXTS, "Player 1 Hand");
p2Panel = createBtnPanel(PLAYER2_BIN_TEXTS, "Player 2 Hand");
p3Panel = createBtnPanel(PLAYER3_BIN_TEXTS, "Player 3 Hand");
p4Panel = createBtnPanel(PLAYER4_BIN_TEXTS, "Player 4 Hand");
mainPanel.setLayout(new BorderLayout());
mainPanel.setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
mainPanel.add(p1Panel, BorderLayout.CENTER);
mainPanel.add(p2Panel, BorderLayout.CENTER);
mainPanel.add(p3Panel, BorderLayout.CENTER);
mainPanel.add(p4Panel, BorderLayout.CENTER);
p1Panel.setVisible(true);
p2Panel.setVisible(false);
p3Panel.setVisible(false);
p4Panel.setVisible(false);
}
public void p1PanelSetVisable(boolean visable) {
p1Panel.setVisible(true);
Window win = SwingUtilities.getWindowAncestor(mainPanel);
win.pack();
}
public void p2PanelSetVisable(boolean visable) {
p2Panel.setVisible(true);
Window win = SwingUtilities.getWindowAncestor(mainPanel);
win.pack();
}
public void p3PanelSetVisable(boolean visable) {
p3Panel.setVisible(true);
Window win = SwingUtilities.getWindowAncestor(mainPanel);
win.pack();
}
public void p4PanelSetVisable(boolean visable) {
p4Panel.setVisible(true);
Window win = SwingUtilities.getWindowAncestor(mainPanel);
win.pack();
}
public JPanel getMainPanel() {
return mainPanel;
}
private JPanel createBtnPanel(String[][] texts, String title){
JPanel btnPanel = new JPanel();
int rows = texts.length;
int cols = texts[0].length;
btnPanel.setLayout(new GridLayout(rows, cols, GAP, GAP));
for(int row = 0; row < texts.length; row++){
for(int col = 0; col < texts[row].length; col++){
JButton btn = new JButton(texts[row][col]);
btn.setFont(BTN_FONT);
btnPanel.add(btn);
}
}
btnPanel.setBorder(BorderFactory.createTitledBorder(title));
return btnPanel;
}
}
class HandMenu {
private static final String PLAYER1 = "Player 1";
private static final String PLAYER2 = "Player 2";
private static final String PLAYER3 = "Player 3";
private static final String PLAYER4 = "Player 4";
private HandGui gui;
private JMenuBar menuBar = new JMenuBar();
private JMenuItem p1Hand;
private JMenuItem p2Hand;
private JMenuItem p3Hand;
private JMenuItem p4Hand;
HandMenu(HandGui gui) {
this.gui = gui;
p1Hand = new JMenuItem(PLAYER1, KeyEvent.VK_1);
p2Hand = new JMenuItem(PLAYER2, KeyEvent.VK_2);
p3Hand = new JMenuItem(PLAYER3, KeyEvent.VK_3);
p4Hand = new JMenuItem(PLAYER4, KeyEvent.VK_4);
ViewAction viewAction = new ViewAction();
p1Hand.addActionListener(viewAction);
p2Hand.addActionListener(viewAction);
p3Hand.addActionListener(viewAction);
p4Hand.addActionListener(viewAction);
p1Hand.setEnabled(false);
JMenu viewMenu = new JMenu("Change Players");
viewMenu.setMnemonic(KeyEvent.VK_P);
viewMenu.add(p1Hand);
viewMenu.add(p2Hand);
viewMenu.add(p3Hand);
viewMenu.add(p4Hand);
}
public JMenuBar getJMenuBar(){
return menuBar;
}
private class ViewAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if(command.equals(PLAYER1)){
gui.p1PanelSetVisable(true);
gui.p2PanelSetVisable(false);
gui.p3PanelSetVisable(false);
gui.p4PanelSetVisable(false);
p1Hand.setEnabled(false);
p2Hand.setEnabled(true);
p3Hand.setEnabled(true);
p4Hand.setEnabled(true);
} else if(command.equals(PLAYER2)){
gui.p1PanelSetVisable(false);
gui.p2PanelSetVisable(true);
gui.p3PanelSetVisable(false);
gui.p4PanelSetVisable(false);
p1Hand.setEnabled(true);
p2Hand.setEnabled(false);
p3Hand.setEnabled(true);
p4Hand.setEnabled(true);
} else if(command.equals(PLAYER3)){
gui.p1PanelSetVisable(false);
gui.p2PanelSetVisable(false);
gui.p3PanelSetVisable(true);
gui.p4PanelSetVisable(false);
p1Hand.setEnabled(true);
p2Hand.setEnabled(true);
p3Hand.setEnabled(false);
p4Hand.setEnabled(true);
} else if(command.equals(PLAYER4)){
gui.p1PanelSetVisable(false);
gui.p2PanelSetVisable(false);
gui.p3PanelSetVisable(false);
gui.p4PanelSetVisable(true);
p1Hand.setEnabled(true);
p2Hand.setEnabled(true);
p3Hand.setEnabled(true);
p4Hand.setEnabled(false);
}
}
}
}
This is my first time posting on stack overflow so I am sorry if I did not clarify my problem correctly.
Upvotes: 0
Views: 58
Reputation: 347204
BorderLayout
only allows a single component to occupy a given position, so...
mainPanel.add(p1Panel, BorderLayout.CENTER);
mainPanel.add(p2Panel, BorderLayout.CENTER);
mainPanel.add(p3Panel, BorderLayout.CENTER);
mainPanel.add(p4Panel, BorderLayout.CENTER);
Means that only p4Panel
will be shown...
For switching between panels, you are better of using CardLayout
instead of...
p1Panel.setVisible(true);
p2Panel.setVisible(false);
p3Panel.setVisible(false);
p4Panel.setVisible(false);
This is why you frame is blank, because p4Panel
is invisible, but it is the only component that BorderLayout
will try to layout...
Now, essentially as near as I can tell, nothing in HandMenu
is getting applied to any UI components (none of the menus are been set)...
You need to be adding your JMenu
to the menuBar
, for example...
menuBar.add(viewMenu);
Instead of looking at other peoples example, I would suggest checking out:
Upvotes: 2
Reputation: 11327
You cannot add more than one component to the center of the panel
mainPanel.add(p1Panel, BorderLayout.CENTER);
mainPanel.add(p2Panel, BorderLayout.CENTER);
mainPanel.add(p3Panel, BorderLayout.CENTER);
mainPanel.add(p4Panel, BorderLayout.CENTER);
p1Panel.setVisible(true);
p2Panel.setVisible(false);
p3Panel.setVisible(false);
p4Panel.setVisible(false);
To do what you want you need a proxy panel with a CardLayout
JPanel proxyPanel = new JPanel(new CardLayout());
proxyPanel.add(p1Panel, "0");
proxyPanel.add(p1Panel, "1");
proxyPanel.add(p1Panel, "2");
proxyPanel.add(p1Panel, "3");
mainPanel.add(proxyPanel, BorderLayout.CENTER);
Do not use setVisible(boolean)
. To make panel with index 1 visible use CardLayout.show(mainPanel, "1")
.
Upvotes: 2