gpt4 book ai didi

java - GridBagLayout 不满足椭圆形形状请求

转载 作者:行者123 更新时间:2023-12-03 06:53:47 26 4
gpt4 key购买 nike

我正在使用 swing 尝试制作一个垄断板。我试图将板上的属性/空间制作为单独的 JPanel,它们可以有自己的实现、信息等,但我在空间的形状方面遇到了一些问题。我正在使用 GridBagLayout (我对这个布局管理器相当陌生),并且我正在尝试布置面板,以便对于侧面 JPanels,gridwidth = 4,gridheight = 3(请参阅随附的代码)。然而,当我运行它并查看它时,面板是方形的。我确认当我将 gridheight 更改为 2 或 1 时这不起作用,它仍然显示为正方形(我期望 JPanel 的形状更加椭圆形)。

请原谅命名方案,在我的主垄断程序中实现 gui 之前,我只是在 Eclipse 中的一个单独的临时项目文件中尝试此布局。

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();
}
}

最佳答案

我对你的c.fill = VERTICAL有点困惑,我假设BOTH。除此之外,如果窗口是方形的,则棋盘将具有方形区域,如果窗口是椭圆形的,则棋盘将具有椭圆形区域。 Gridwidth 和 gighheight 在这里完全无关。相反,如果您想要其区域与其中一个轴重叠的对象,则可以使用这些。最好将网格包布局视为一个大网格。然后 gridwidth 和 gridheight 告诉布局给定对象应占据多少列或行。但没有什么可说的,列应该和行一样宽。事实上,没有什么可以说行或列具有相同的大小。所以我想说你对 GridBagLayout 的运作方式有一些不正确的想法。

如果您想固定宽高比,您应该为各个 Pane 设置首选尺寸,然后将整个板打包到 ScrollView 中。这将确保所有内容都以首选尺寸显示。不过,不确定滚动对于您的情况是否是一个好主意。如果没有,您可以尝试使用具有某种合适布局的外部容器,这将允许内部容器增长到其首选大小并且不会更大。

关于java - GridBagLayout 不满足椭圆形形状请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24665104/

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