我正在制作纸牌游戏应用程序只是为了好玩,而我在夏天很无聊。我对JFrames很陌生,所以我决定查看其他代码并将其编辑为我想要的代码。在查看此网址的答案后:How to arrange multiple panels in JFrame,我尝试将其复制到我的需求中。问题在于,当我运行程序时,除了基本JFrame的标题之外没有任何其他功能显示但没有功能(例如选择查看玩家手)。我似乎无法找到没有出现任何功能的原因。
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);
}
}
}
}
这是我第一次发布堆栈溢出,所以如果我没有正确澄清我的问题,我很抱歉。
答案 0 :(得分:2)
您无法在面板中心添加多个组件
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);
要执行您想要的操作,您需要一个带有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);
请勿使用setVisible(boolean)
。要使索引1的面板可见,请使用CardLayout.show(mainPanel, "1")
。
答案 1 :(得分:2)
BorderLayout
仅允许单个组件占据给定位置,因此......
mainPanel.add(p1Panel, BorderLayout.CENTER);
mainPanel.add(p2Panel, BorderLayout.CENTER);
mainPanel.add(p3Panel, BorderLayout.CENTER);
mainPanel.add(p4Panel, BorderLayout.CENTER);
表示只显示p4Panel
...
要在面板之间切换,最好使用CardLayout
代替...
p1Panel.setVisible(true);
p2Panel.setVisible(false);
p3Panel.setVisible(false);
p4Panel.setVisible(false);
这就是框架为空的原因,因为p4Panel
是不可见的,但它是BorderLayout
尝试布局的唯一组件...
现在,基本上就像我所知,HandMenu
中的任何内容都没有应用于任何UI组件(没有设置任何菜单)......
您需要将JMenu
添加到menuBar
,例如......
menuBar.add(viewMenu);
我建议不要查看其他人的例子: