我对扩展JPanel的类有疑问。在我基于JFrame的游戏中,我有几个JPanels,我在程序运行时交换(登录面板,新的acc表单面板等...都在一个JFrame中)。我想用GBL切换到Panel,但不幸的是它没有很好地显示内容......它只显示ChatBox(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);
}
}
答案 0 :(得分:2)
你对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设置为非零值。