最近在设计一个围棋的界面框架,分成了几个轻量级的jpanel类(QiPang,Player,PlayerPane,Chatroom)和一个主框架GameFrame类,但是一组合起来左边的QiPang的界面却不能完全显示,只显示了一部分,布局管理器是使用了GroupLayout,想了很久,还是不知道问题的所在,由于本人才刚接触布局管理器,不是很清楚,也有可能是其它一些常识的问题,请高手指点指点。贴出下面的源码,粘贴在eclipse上直接运行就行,代码是多了点,但已经删除了很多了。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Vector;
import javax.swing.GroupLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class GameFrame extends JFrame {
public final int GAMEWIDTH = 800;
public final int GAMEHEIGHT = 800;
public GameFrame() {
this.init();
this.setBounds(new Rectangle(GAMEWIDTH, GAMEHEIGHT));
this.setTitle("围棋天地");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
public void init() {
// 创建各个组件
PlayerPane player1 = new PlayerPane(new Player());
PlayerPane player2 = new PlayerPane(new Player());
Chatroom chat = new Chatroom();
QiPang qipang = new QiPang();
// 布置各个组件
GroupLayout layout = new GroupLayout(this.getContentPane());
this.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
// 水平布置
GroupLayout.SequentialGroup sga = layout.createSequentialGroup()
.addComponent(player1)
.addComponent(player2);
GroupLayout.ParallelGroup hp1 = layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(sga)
.addComponent(chat);
GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup()
.addComponent(qipang)
.addGroup(hp1);
layout.setHorizontalGroup(hGroup);
// 垂直布置
GroupLayout.ParallelGroup vpa = layout.createParallelGroup()
.addComponent(player1)
.addComponent(player2);
GroupLayout.SequentialGroup sgb = layout.createSequentialGroup()
.addGroup(vpa)
.addComponent(chat);
GroupLayout.ParallelGroup vp1 = layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(qipang)
.addGap(5)
.addGroup(sgb);
layout.setVerticalGroup(vp1);
}
public class QiPang extends JPanel {
public static final int SPACE = 26;
public static final int QIPANGSPACE = 20;
public static final int QIPANGWIDTH = SPACE * 18 + QIPANGSPACE * 2;
public static final int QIPANGHEIGHT = SPACE * 18 + QIPANGSPACE * 2;
public static final int MOUSESPACE = 10;
public static final int POINTCOUNT = 19 * 19;
public QiPang() {
super();
this.setBounds(new Rectangle(QIPANGWIDTH, QIPANGHEIGHT));