gpt4 book ai didi

java - GridLayout 单元格真的都是相同大小吗?

转载 作者:太空宇宙 更新时间:2023-11-04 07:06:40 26 4
gpt4 key购买 nike

我决定为我的 Java Swing 应用程序使用 GridLayout LayoutManager 因为网格中的每个单元格应该具有完全相同的大小。

来自the Java Tutorials :

A GridLayout object places components in a grid of cells. Each component takes all the available space within its cell, and each cell is exactly the same size.

甚至在 GridLayout 的描述中类:

The GridLayout class is a layout manager that lays out a container's components in a rectangular grid. The container is divided into equal-sized rectangles, and one component is placed in each rectangle.

但是,我的代码似乎使某个单元格大小是其他单元格的两倍。我使用 GridLayout 将 3 个 JPanel 添加到 Container 中,并为每个 JPanel 提供了不同的背景颜色。结果是这样的:

enter image description here

显然,第一个 JPanel(红色背景)是其他 JPanel(绿色和黄色)的两倍大。生成此代码的代码如下:

public void updateListFrameContentPane(Container mainPane) {
mainPane.setLayout(new GridLayout(1,0));
JPanel listPanel = new JPanel();
listPanel.setLayout(new BoxLayout(listPanel, BoxLayout.Y_AXIS));
listPanel.add(friendsLabel);
listPanel.add(listScrollPane);
listPanel.setBackground(Color.RED);
mainPane.add(listPanel);
for(JPanel chatPanel : chatPanels) {
chatPanel.setBackground((Math.random()>0.5 ? Color.YELLOW : Color.GREEN));
mainPane.add(chatPanel);
}
}

我所做的就是将 Container's 布局设置为具有 1 行和任意数量列的 GridLayout,然后向其中添加 3 个 JPanels。那么为什么第一个 JPanel 这么大呢?奇怪的是,只有当添加两个或更多 chatPanel 时才会发生这种情况。当只有一个时,它的格式正确。

最佳答案

基赫鲁是对的。更改容器内容后重新验证/重新绘制。这是一个粗略但有效的示例:

public class GridLayoutExample {

private JFrame frame;
private Map<String,JPanel> chatBoxes = new HashMap<String,JPanel>();
private String lastKey = "0";

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GridLayoutExample window = new GridLayoutExample();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public GridLayoutExample() {
initialize();
}

private void addChatBox() {
/*
* JPanel (border layout)
* - JPanel (Border South, Border layout)
* - - JTextField ( Border center )
* - - JButton ( Border east )
* - JLabel (Border North )
* - JTextArea (Border Center);
*/
int lk = Integer.valueOf(lastKey)+1;
lastKey = Integer.toString(lk);
JPanel np = new JPanel();
np.setLayout(new BorderLayout(0,0));
np.setBackground((lk%2 == 0) ? Color.GREEN : Color.YELLOW);

JPanel south = new JPanel();
south.setLayout(new BorderLayout(0,0));
np.add(south,BorderLayout.SOUTH);

JButton b = new JButton("New Button");
south.add(b,BorderLayout.EAST);

JTextField field = new JTextField();
south.add(field,BorderLayout.CENTER);

JLabel label = new JLabel(lastKey);
label.setHorizontalAlignment(SwingConstants.CENTER);
np.add(label,BorderLayout.NORTH);

JTextArea text = new JTextArea();
np.add(text,BorderLayout.CENTER);

chatBoxes.put(lastKey, np);
frame.getContentPane().add(np);
frame.revalidate(); // CRITICAL MISSING LINES
frame.repaint(); // CRITICAL MISSING LINES
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 923, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridLayout(1, 0, 0, 0));

JPanel panel = new JPanel();
panel.setBackground(Color.RED);
frame.getContentPane().add(panel);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

JLabel lblNewLabel = new JLabel("Online Users");
panel.add(lblNewLabel);

JList list = new JList();
list.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
addChatBox();
}
});
list.setModel(new AbstractListModel() {
String[] values = new String[] {"Alpha", "Beta", "Gamma", "Delta", "Epsilon"};
public int getSize() {
return values.length;
}
public Object getElementAt(int index) {
return values[index];
}
});
panel.add(list);

}
}

我选择重新验证/重新绘制整个框架,但在重新绘制较小的容器时可能会使其工作。当然,如果没有上面标记的关键行,无论您单击列表元素的频率如何,都不会显示任何新内容。通过这些行,每次单击时都会添加一个新的聊天框。

嗯...刚刚注意到这一点。如果红色区域被认为是两个单独的面板,那么它们的尺寸都是完全正确的。您是否可能不小心添加了额外的面板?

关于java - GridLayout 单元格真的都是相同大小吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21269417/

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