gpt4 book ai didi

java - 我的 JPanel 中的 GridBagLayout 不起作用

转载 作者:行者123 更新时间:2023-11-30 08:52:34 25 4
gpt4 key购买 nike

<分区>

我对扩展 JPanel 的类有疑问。在我基于 JFrame 的游戏中,我有几个 JPanel,我在程序运行时交换它们(登录面板、新的 acc 表单面板等......都在一个 JFrame 中)。我想切换到带有 GBL 的面板,但不幸的是它不能很好地显示内容......它只在屏幕中央显示聊天框(JTextArea)和输入字段(JTextField),但我想将它们放在旁边其他。

public class InGameMenu extends JPanel 

private JTextArea chatBox;
private JPanel buttonPanel;
private TextField chatTextField;
private JList playersList;
private DefaultListModel listModel;
private ConnectionToServer conn;
private JFrame mainFrame;

public InGameMenu (ConnectionToServer conn, JFrame mainFrame)
{
this.setBackground(Color.CYAN);
this.conn = conn;
this.mainFrame = mainFrame;
mainFrame.setTitle("Game menu");
super.setLayout(new GridBagLayout());
chatBox = new JTextArea("This is player's chat.\n");

GridBagConstraints gbc = new GridBagConstraints();

// chat box
gbc.gridheight = 10;
gbc.gridwidth = 10;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
this.add(chatBox, gbc);

// chat text field
chatTextField = new TextField("dsadasda");
chatTextField.setBackground(Color.GRAY);
gbc.gridheight = 1;
gbc.gridy = 1;
this.add (chatTextField, gbc);

// lista graczy
listModel = new DefaultListModel();
playersList = new JList(listModel);

gbc.gridheight = 15;
gbc.gridwidth = 4;
gbc.gridx = 1;
gbc.gridy = 0;
this.add (playersList, gbc);

// panel przyciskow
buttonPanel = new JPanel(new FlowLayout());
gbc.gridheight = 1;
gbc.gridy = 1;
this.add (buttonPanel, gbc);
}
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com