Java 如何使用GridBagConstraints setRows和setColumns?
如何使用Java 如何使用GridBagConstraints setRows和setColumns?,java,swing,layout-manager,gridbaglayout,Java,Swing,Layout Manager,Gridbaglayout,如何使用setRows和setColumns更改窗口中的行数和列数 这是我的代码副本,我试图在其中实现设置的行和列: import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import java.awt.Dim
setRows
和setColumns更改窗口中的行数和列数
这是我的代码副本,我试图在其中实现设置的行和列:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.*;
@SuppressWarnings("unused")
public class GUI
{
public static void main(String[] args)
{
new GUI();
}
public GUI()
{
JFrame AG = new JFrame("Adventure Game");
AG.setExtendedState(JFrame.MAXIMIZED_BOTH);
AG.setResizable(true);
AG.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p=new JPanel();
p.setLayout (new GridBagLayout());
AG.add(p);
GridBagConstraints GBC = new GridBagConstraints();
GridLayout.setRows(100);//heres the set rows<<<<<<<<<<<<<<<<<<<
GridLayout.setColumns(100);//heres the set columns<<<<<<<<<<<<<<<<<<<
AG.getContentPane().add(p, BorderLayout.NORTH);
JButton saveButton =new JButton("Save");
JButton loadButton =new JButton("Load");
JButton optionsButton = new JButton("Options");
JLabel textBox= new JLabel("Story line will go here.");
JLabel label11 = new JLabel("Test 1");
GBC.gridx = 0;
GBC.gridy = 1;
p.add(label11,GBC);
JLabel label12 = new JLabel("Test 2");
GBC.gridx = 0;
GBC.gridy = 2;
p.add(label12,GBC);
JLabel label13 = new JLabel("Test 3");
GBC.gridx = 0;
GBC.gridy = 3;
p.add(label13,GBC);
JLabel label14 = new JLabel("Test 4");
GBC.gridx = 0;
GBC.gridy = 5;
p.add(label14,GBC);
AG.setVisible(true);
}
}
import javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JScrollPane;
导入javax.swing.JTextArea;
导入java.awt.Dimension;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.Toolkit;
导入java.awt.*;
@抑制警告(“未使用”)
公共类GUI
{
公共静态void main(字符串[]args)
{
新GUI();
}
公共图形用户界面()
{
JFrame AG=新JFrame(“冒险游戏”);
AG.setExtendedState(JFrame.MAXIMIZED_两者);
AG.可设置大小(真);
AG.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p=新的JPanel();
p、 setLayout(新的GridBagLayout());
AG.添加(p);
GridBagConstraints GBC=新的GridBagConstraints();
GridLayout.setRows(100);//这里是设置的行GridBagConstraints
使用GridBagLayout
,而不是GridLayout
。对GridLayout的调用没有任何作用
要使用GridBagConstraints设置行和列,您需要使用GBC.gridx=…;
和GBC.gridy=…;
如果您试图在按钮之间放置间距,空的GridBagConstraints单元格不是这样做的方式;请使用GridBagConstraints的插入部分
要使GridBagLayout中的某个组件消失,而不使其他组件移动,请将按钮放置在带有CardLayout的JPanel中,并确保将该JPanel添加到GridBagLayout中,而不是按钮:
private JButton optionalButton;
private void buildWindow() {
// ...
optionalButton = new JButton("Optional Action");
JPanel optionalButtonPanel = new JPanel(new CardLayout());
optionalButtonPanel.add(optionalButton, "BUTTON");
optionalButtonPanel.add(new JLabel(), "BLANK");
// ...
}
private void setOptionalButtonVisible(boolean visible) {
Container optionalButtonPanel = optionalButton.getParent();
CardLayout layout = (CardLayout) optionalButtonPanel.getLayout();
layout.show(optionalButtonPanel, visible ? "BUTTON" : "BLANK");
}
CardLayout始终显示且仅显示其一个组件,但它会调整其关联容器的大小以适合所有组件,包括不可见的组件。您试图实现什么?您需要借助GridBagConstraints
:gridx
,gridy
,gridwidth
,gridheigh来操作行/列t
是的,我正在尝试解决如何布置按钮,因为我希望例如能够有空白单元格,这样我可以在0,10,20,3处有一个按钮,然后错过一个和0,5。在这种情况下,我建议您添加虚拟标签(新的JLabel(“”;
)到0.4。这就是我所使用的,但是列和行的数量与对象的数量有关,所以我想预定义x轴和y轴上的单元格数量您无法在GridBagLayout中预定义行/列计数…这没有意义!行/列中要么有组件,如果没有,GridBagLayout将不会进行布局(即0高度和宽度)