gpt4 book ai didi

java - 如何制作一个 6 行 2 列的 Java GridBagLayout

转载 作者:行者123 更新时间:2023-12-01 07:26:09 26 4
gpt4 key购买 nike

我厌倦了 Java GridBagLayout。我想创建一个如下图所示的面板,但我无法得到它。我无法定位左侧面板,也无法使面板变长。当我设置 gridWidth 时,宽度不会增加。我不能为此使用任何 GUI 构建器。我想要一个如下图所示的布局。

enter image description here

这是不成功的代码:

public class gui3 extends Frame{
Panel p1,p2,p3,p4,p5,p6,p7,pmain;
gui3(){

setVisible(true);
setSize(500,500);
setTitle(" Calculator ");


GridBagLayout gb1=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
setLayout(gb1);

p1=new Panel();
p1.setBackground(Color.BLACK);
gbc.gridx=5;
gbc.gridy=0;
gbc.gridwidth=3;
//gbc.weightx =0.5;
gbc.fill=GridBagConstraints.HORIZONTAL;
add(p1,gbc);

p2=new Panel();
p2.setBackground(Color.BLUE);
gbc.gridx=0;
gbc.gridy=1;
gbc.gridwidth=2;
// gbc.weightx = 1;
gbc.fill=GridBagConstraints.HORIZONTAL;
add(p2,gbc);

p3=new Panel();
p3.setBackground(Color.GREEN);
gbc.gridx=0;
gbc.gridy=2;
gbc.gridwidth=2;
// gbc.weightx = 1;
gbc.fill=GridBagConstraints.HORIZONTAL;
add(p3,gbc);

p4=new Panel();
p4.setBackground(Color.cyan);
gbc.gridx=0;
gbc.gridy=3;
gbc.gridwidth=2;
// gbc.weightx = 1;
gbc.fill=GridBagConstraints.HORIZONTAL;
add(p4,gbc);

p5=new Panel();
p5.setBackground(Color.RED);
gbc.gridx=0;
gbc.gridy=4;
gbc.gridwidth=2;
// gbc.weightx = 1;
gbc.fill=GridBagConstraints.HORIZONTAL;
add(p5,gbc);

p6=new Panel();
p6.setBackground(Color.pink);
gbc.gridx=0;
gbc.gridy=5;
gbc.gridwidth=2;
// gbc.weightx = 1;
// gbc.fill=GridBagConstraints.HORIZONTAL;
add(p6,gbc);

p7=new Panel();
p7.setBackground(Color.yellow);
gbc.gridx=6;
gbc.gridy=0;
gbc.gridheight=6;
// gbc.weightx = 1;
gbc.fill=GridBagConstraints.HORIZONTAL;
add(p7,gbc);


}

}

最佳答案

你的意思是......

Layout

基本上,您可以使用GridBagConstraints#gridheight(或gridwidth)来设置组件跨越的网格单元数

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestLayout {

public static void main(String[] args) {
new TestLayout();
}

public TestLayout() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(4, 4, 4, 4);
gbc.gridx = 0;
gbc.weightx = 1;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
frame.add(createPane(Color.RED), gbc);
gbc.gridy++;
frame.add(createPane(Color.GREEN), gbc);
gbc.gridy++;
frame.add(createPane(Color.BLUE), gbc);
gbc.gridy++;
frame.add(createPane(Color.CYAN), gbc);
gbc.gridy++;
frame.add(createPane(Color.MAGENTA), gbc);
gbc.gridy++;
frame.add(createPane(Color.ORANGE), gbc);
gbc.gridy++;
frame.add(createPane(Color.PINK), gbc);

gbc.gridx++;
gbc.weightx = 0;
gbc.gridy = 0;
gbc.weighty = 1;
gbc.gridheight = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.VERTICAL;
frame.add(createPane(Color.YELLOW), gbc);

frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public JPanel createPane(Color color) {
JPanel pane = new JPanel(){

@Override
public Dimension getPreferredSize() {
return new Dimension(50, 50);
}

};
pane.setBackground(color);
return pane;
}

}

看看How to Use GridBagLayout了解更多详情

关于java - 如何制作一个 6 行 2 列的 Java GridBagLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24427092/

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