我的 JPanel 中的 GridBagLayout 不起作用
GridBagLayout in my JPanel doesn't work
我对扩展 JPanel 的 class 有疑问。在我基于 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);
}
}
您对 gridHeight 和 gridWidth 常量的使用似乎已关闭,并且似乎期望 GridBagLayout 不具备的能力。例如,您不会以这种方式定义 JTextArea 的列大小和行大小。相反,通过调用适当的构造函数或方法来设置文本组件的行大小和列大小以及 JList 的原型大小。例如:
import java.awt.*;
import javax.swing.*;
public class InGameMenu extends JPanel {
private static final String[] EXAMPLE_TEXT = {"One", "Two", "Three"};
private static final int COLUMNS = 40;
private static final int ROWS = 20;
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)
public InGameMenu(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");
chatBox = new JTextArea("This is player's chat.\n", ROWS, COLUMNS);
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);
chatBox.setWrapStyleWord(true);
chatBox.setLineWrap(true);
JScrollPane scrollPane = new JScrollPane(chatBox);
this.add(scrollPane, gbc);
// chat text field
chatTextField = new TextField("dsadasda", COLUMNS);
chatTextField.setBackground(Color.GRAY);
// gbc.gridheight = 1;
gbc.gridy = 1;
this.add(chatTextField, gbc);
// lista graczy
listModel = new DefaultListModel();
for (int i = 0; i < EXAMPLE_TEXT.length; i++) {
listModel.addElement(EXAMPLE_TEXT[i]);
}
playersList = new JList(listModel);
playersList.setPrototypeCellValue(String.format("%40s", " "));
// 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);
}
public InGameMenu() {
// TODO Auto-generated constructor stub
}
private static void createAndShowGui() {
JFrame frame = new JFrame("In Game Menu");
InGameMenu mainPanel = new InGameMenu(frame);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
现在,如果您希望组件填充视图,则需要将 weightx 和 weighty GridBagConstraints 设置为非零值。
我对扩展 JPanel 的 class 有疑问。在我基于 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);
}
}
您对 gridHeight 和 gridWidth 常量的使用似乎已关闭,并且似乎期望 GridBagLayout 不具备的能力。例如,您不会以这种方式定义 JTextArea 的列大小和行大小。相反,通过调用适当的构造函数或方法来设置文本组件的行大小和列大小以及 JList 的原型大小。例如:
import java.awt.*;
import javax.swing.*;
public class InGameMenu extends JPanel {
private static final String[] EXAMPLE_TEXT = {"One", "Two", "Three"};
private static final int COLUMNS = 40;
private static final int ROWS = 20;
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)
public InGameMenu(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");
chatBox = new JTextArea("This is player's chat.\n", ROWS, COLUMNS);
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);
chatBox.setWrapStyleWord(true);
chatBox.setLineWrap(true);
JScrollPane scrollPane = new JScrollPane(chatBox);
this.add(scrollPane, gbc);
// chat text field
chatTextField = new TextField("dsadasda", COLUMNS);
chatTextField.setBackground(Color.GRAY);
// gbc.gridheight = 1;
gbc.gridy = 1;
this.add(chatTextField, gbc);
// lista graczy
listModel = new DefaultListModel();
for (int i = 0; i < EXAMPLE_TEXT.length; i++) {
listModel.addElement(EXAMPLE_TEXT[i]);
}
playersList = new JList(listModel);
playersList.setPrototypeCellValue(String.format("%40s", " "));
// 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);
}
public InGameMenu() {
// TODO Auto-generated constructor stub
}
private static void createAndShowGui() {
JFrame frame = new JFrame("In Game Menu");
InGameMenu mainPanel = new InGameMenu(frame);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
现在,如果您希望组件填充视图,则需要将 weightx 和 weighty GridBagConstraints 设置为非零值。