Wil*_*iam 2 java swing layout-manager gridbaglayout
我正在使用swing试图建立一个垄断板.我试图将板上的属性/空间作为单独的JPanels,可以有自己的实现,信息等,但我在空间的形状有一些麻烦.我正在使用GridBagLayout(我对这个布局管理器相当新),我正在尝试将面板放置出来,以便对于JPanels,gridwidth = 4,gridheight = 3(参见附加代码).但是,当我运行它并查看它时,面板是方形的.当我将gridheight更改为2或1时,我确认这不起作用,它仍然显示正方形(我期望JPanels更加椭圆形).
原谅命名方案,我只是在Eclipse中的一个单独的临时项目文件中尝试这种布局,然后才在我的主要垄断程序中实现gui.
import static java.awt.GridBagConstraints.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class MainFrame extends JFrame {
private static final long serialVersionUID = 1L;
public MainFrame() {
super();
setDefaultCloseOperation(EXIT_ON_CLOSE);
Dimension screenSize = getToolkit().getScreenSize();
setSize(screenSize.height, screenSize.height);
setLocationRelativeTo(null);
setLayout(new GridBagLayout());
addCorners();
addEdges();
setVisible(true);
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
System.out.println("Component resized");
int width = getWidth();
int height = getHeight();
System.out.println(width + " " + height);
super.componentResized(e);
}
});
}
private int startingColRow = 4;
private int endingColRow = 28;
private int bigWidth = 4;
private int smallWidth = 3;
private int startingEdge = 0;
private int endingEdge = 31;
private void addEdges() {
addLeftEdge();
addTopEdge();
addBottomEdge();
addRightEdge();
}
private void addCorners() {
addTopLeftCorner();
addBottomLeftCorner();
addTopRightCorner();
addBottomRightCorner();
}
private void addCorner(int anchor, int gridx, int gridy) {
add(getWhiteJPanel(), getGridBagConstraints(anchor, gridx, gridy, bigWidth, bigWidth));
}
private void addBottomRightCorner() {
addCorner(SOUTHEAST, endingEdge, endingEdge);
}
private void addTopRightCorner() {
addCorner(NORTHWEST, endingEdge, startingEdge);
}
private void addBottomLeftCorner() {
addCorner(SOUTHWEST, startingEdge, endingEdge);
}
private void addTopLeftCorner() {
addCorner(NORTHEAST, startingEdge, startingEdge);
}
public void addLeftEdge() {
for (int row = startingColRow; row <= endingColRow; row += smallWidth) {
add(getWhiteJPanel(), getGridBagConstraints(WEST, startingEdge, row, bigWidth, smallWidth));
}
}
public void addRightEdge() {
for (int row = startingColRow; row <= endingColRow; row += smallWidth) {
add(getWhiteJPanel(), getGridBagConstraints(EAST, endingEdge, row, bigWidth, smallWidth));
}
}
public void addBottomEdge() {
for (int col = startingColRow; col <= endingColRow; col += smallWidth) {
add(getWhiteJPanel(), getGridBagConstraints(SOUTH, col, endingEdge, smallWidth, bigWidth));
}
}
public void addTopEdge() {
for (int col = startingColRow; col <= endingColRow; col += smallWidth) {
add(getWhiteJPanel(), getGridBagConstraints(NORTH, col, startingEdge, smallWidth, bigWidth));
}
}
private static JPanel getWhiteJPanel() {
JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
return panel;
}
private GridBagConstraints getGridBagConstraints(int anchor, int gridx, int gridy, int gridwidth, int gridheight) {
GridBagConstraints c = new GridBagConstraints();
c.fill = VERTICAL;
c.weightx = c.weighty = 1;
c.anchor = anchor;
c.gridx = gridx;
c.gridy = gridy;
c.gridwidth = gridwidth;
c.gridheight = gridheight;
return c;
}
public static void main(String[] args) {
new MainFrame();
}
}
Run Code Online (Sandbox Code Playgroud)
我对你有点困惑c.fill = VERTICAL,我已经假设了BOTH.除此之外,如果窗口是方形的,则棋盘将具有方形字段;如果窗口是椭圆形的,则将具有长方形字段.网格宽度和grigheight在这里完全无关紧要.相反,如果您想要在其中一个轴上重叠区域的对象,则可以使用它们.最好将网格袋布局视为大网格.然后gridwidth和gridheight告诉布局给定对象应占用多少列或行.但没有什么可说的,列应该与行高一样宽.事实上,没有什么可以说行或列具有相同的大小.所以我说你对GridBagLayout的运作方式有一些错误的想法.
如果要修复纵横比,则应为各个窗格设置首选尺寸,然后将整个板包装到滚动视图中.这将确保所有内容都以首选尺寸显示.不过,不确定滚动是否适合您的情况.如果没有,你可以尝试一个具有一些合适布局的外部容器,这将允许内部容器增长到其首选大小而不是更大.