Java 网格布局困难
我只想使用Java 网格布局困难,java,swing,awt,layout-manager,gridbaglayout,Java,Swing,Awt,Layout Manager,Gridbaglayout,我只想使用GridBagLayout 我尝试了几个约束条件,但始终没有得到预期的结果,因此我开始怀疑,仅使用GridBagLayout是否真的可行。难点在于C1、C2和C3组件。 C1和C2是JComponent,将容纳其他组件,如JPanel。我已经设置了他们的最小和首选尺寸。C3是一个JButton C1不应该占用额外的空间,所以我将其权重X设置为0,将gridwidth设置为1(也尝试使用2,因为它跨越C2和C3)。 C2占用所有额外空间,我将其权重X设置为1,将gridwidth设置为3
GridBagLayout
我尝试了几个约束条件,但始终没有得到预期的结果,因此我开始怀疑,仅使用
GridBagLayout
是否真的可行。难点在于C1、C2和C3组件。C1和C2是
JComponent
,将容纳其他组件,如JPanel
。我已经设置了他们的最小和首选尺寸。C3是一个JButton
C1不应该占用额外的空间,所以我将其权重X设置为0,将gridwidth设置为1(也尝试使用2,因为它跨越C2和C3)。
C2占用所有额外空间,我将其权重X设置为1,将gridwidth设置为3。
GUI无法调整大小。
这个
LayoutManager
我已经用了好几次了,但还是不太熟练,谢谢你的帮助
恐怕这是不可能的
GridBagLayout
无法计算C1起点和C3起点之间的正确距离。恐怕这是不可能的GridBagLayout
无法计算C1起点和C3起点之间的正确距离。- 我将只讨论
,甚至这可能是GridBagLayout
(MigLayout具有用于填充列数和行数、调整大小等的附加参数)和/或MigLayout
TableLayout(??)
仅需填写第一行(仅)中所有所需的列数,然后创建矩阵,您可以定义任何GridBagLayout
和/或GBC权重x、权重y、gridx、gridy
锚定值
- 例谈
导入java.awt.Color;
导入java.awt.ComponentOrientation;
导入java.awt.Font;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.Insets;
导入javax.swing.BorderFactory;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JTextField;
导入javax.swing.SwingConstants;
导入javax.swing.SwingUtilities;
公共类GBT输出{
私有JFrame帧=新JFrame(“GbcLayoutGbcLayout”);
private JPanel panel=new JPanel();
私人JLabel hidelLabel;
私人JLabel firstLabel;
私有JTextField firstText;
公共GbcLayout(){
panel.setLayout(新的GridBagLayout());
GridBagConstraints gbc=新的GridBagConstraints();
面板。设置组件方向(组件方向。从左到右);
对于(int k=0;k<50;k++){
hidelLabel=新的JLabel(“”);
设置不透明(真);
挫折背景(颜色:橙色);
setboorder(BorderFactory.createLineBorder(Color.BLACK,1));
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.weightx=0.5;
gbc.weighty=0.5;
gbc.gridx=k;
gbc.gridy=0;
面板。添加(希德拉贝尔,gbc);
}
对于(int k=0;k<5;k++){
firstLabel=新的JLabel(“测试标签:”,SwingConstants.RIGHT);
setFont(新字体(“衬线”,Font.BOLD,20));
firstLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK,1));
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.插图=新插图(0,0,5,0);
gbc.gridx=0;
gbc.gridwidth=8;
gbc.gridy=k+1;
面板。添加(第一个标签,gbc);
}
对于(int k=0;k<5;k++){
firstText=新的JTextField(“测试文本字段”);
setFont(新字体(“衬线”,Font.BOLD,20));
setboorder(BorderFactory.createLineBorder(Color.BLACK,1));
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.插图=新插图(0,0,5,0);
gbc.gridx=9;
gbc.gridwidth=k+8;
gbc.gridy=k+1;
面板。添加(第一个文本,gbc);
}
对于(int k=0;k<5;k++){
firstLabel=新的JLabel(“测试标签:”,SwingConstants.RIGHT);
setFont(新字体(“衬线”,Font.BOLD,20));
firstLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK,1));
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.插图=新插图(0,0,5,0);
gbc.gridx=20+k;
gbc.gridwidth=8;
gbc.gridy=k+1;
面板。添加(第一个标签,gbc);
}
对于(int k=0;k<5;k++){
firstText=新的JTextField(“测试文本字段”);
setFont(新字体(“衬线”,Font.BOLD,20));
setboorder(BorderFactory.createLineBorder(Color.BLACK,1));
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.插图=新插图(0,0,5,0);
gbc.gridx=29+k;
gbc.gridwidth=21-k;
gbc.gridy=k+1;
面板。添加(第一个文本,gbc);
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
框架。添加(面板);
frame.pack();
frame.setVisible(true);
}
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
GbcLayout gbcl=新的GbcLayout();
}
});
}
}
- 我将只讨论
,甚至这可能是GridBagLayout
(MigLayout具有用于填充列数和行数、调整大小等的附加参数)和/或MigLayout
TableLayout(??)
仅需填写第一行(仅)中所有所需的列数,然后创建矩阵,您可以定义任何GridBagLayout
和/或GBC权重x、权重y、gridx、gridy
锚定值
- 例谈 <
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
public class GbcLayout {
private JFrame frame = new JFrame("GbcLayoutGbcLayout");
private JPanel panel = new JPanel();
private JLabel hidelLabel;
private JLabel firstLabel;
private JTextField firstText;
public GbcLayout() {
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
for (int k = 0; k < 50; k++) {
hidelLabel = new JLabel(" ");
hidelLabel.setOpaque(true);
hidelLabel.setBackground(Color.orange);
hidelLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 0.5;
gbc.weighty = 0.5;
gbc.gridx = k;
gbc.gridy = 0;
panel.add(hidelLabel, gbc);
}
for (int k = 0; k < 5; k++) {
firstLabel = new JLabel("Testing Label : ", SwingConstants.RIGHT);
firstLabel.setFont(new Font("Serif", Font.BOLD, 20));
firstLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridx = 0;
gbc.gridwidth = 8;
gbc.gridy = k + 1;
panel.add(firstLabel, gbc);
}
for (int k = 0; k < 5; k++) {
firstText = new JTextField("Testing TextField");
firstText.setFont(new Font("Serif", Font.BOLD, 20));
firstText.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridx = 9;
gbc.gridwidth = k + 8;
gbc.gridy = k + 1;
panel.add(firstText, gbc);
}
for (int k = 0; k < 5; k++) {
firstLabel = new JLabel("Testing Label : ", SwingConstants.RIGHT);
firstLabel.setFont(new Font("Serif", Font.BOLD, 20));
firstLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridx = 20 + k;
gbc.gridwidth = 8;
gbc.gridy = k + 1;
panel.add(firstLabel, gbc);
}
for (int k = 0; k < 5; k++) {
firstText = new JTextField("Testing TextField");
firstText.setFont(new Font("Serif", Font.BOLD, 20));
firstText.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridx = 29 + k;
gbc.gridwidth = 21 - k;
gbc.gridy = k + 1;
panel.add(firstText, gbc);
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
GbcLayout gbcl = new GbcLayout();
}
});
}
}