java swing中GridBagLayout布局如何让最后一个单元格跨两行?
-----------------------------packagetest;importjava.awt.GridBagConstraints;importjava...
-----------------------------
package test;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestGridBagLayout {
public static void main(String[] args) {
JFrame jf = new JFrame();
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
jf.setLayout(gbl);
JButton jb1 = new JButton("BUTTON1");
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbl.setConstraints(jb1, gbc);
jf.add(jb1);
JButton jb2 = new JButton("BUTTON2");
gbc.gridheight = 2;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbl.setConstraints(jb2, gbc);
jf.add(jb2);
JButton jb3 = new JButton("BUTTON3");
gbc.gridwidth = 1;
gbc.gridheight = 2;
gbl.setConstraints(jb3, gbc);
jf.add(jb3);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(500, 400);
jf.setVisible(true);
}
} 展开
package test;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestGridBagLayout {
public static void main(String[] args) {
JFrame jf = new JFrame();
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
jf.setLayout(gbl);
JButton jb1 = new JButton("BUTTON1");
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbl.setConstraints(jb1, gbc);
jf.add(jb1);
JButton jb2 = new JButton("BUTTON2");
gbc.gridheight = 2;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbl.setConstraints(jb2, gbc);
jf.add(jb2);
JButton jb3 = new JButton("BUTTON3");
gbc.gridwidth = 1;
gbc.gridheight = 2;
gbl.setConstraints(jb3, gbc);
jf.add(jb3);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(500, 400);
jf.setVisible(true);
}
} 展开
2个回答
展开全部
GridBagLayout布局中让最后一个单元格跨两行的Java程序如下:
package test;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestGridBagLayout {
public static void main(String[] args) {
JFrame jf = new JFrame();
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
jf.setLayout(gbl);
JButton jb1 = new JButton("BUTTON1");
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 10;
gbc.weighty = 10;
gbl.setConstraints(jb1, gbc);
jf.add(jb1);
JButton jb2 = new JButton("BUTTON2");
GridBagConstraints gbc1 = new GridBagConstraints();
gbc1.fill = GridBagConstraints.BOTH;
gbc1.gridx = 1;
gbc1.gridy = 0;
gbc1.gridheight = 2;
gbc1.weightx = 20;
gbc1.weighty = 20;
gbl.setConstraints(jb2, gbc1);
jf.add(jb2);
JButton jb3 = new JButton("BUTTON3");
GridBagConstraints gbc2 = new GridBagConstraints();
gbc2.fill = GridBagConstraints.BOTH;
gbc2.gridx = 0;
gbc2.gridy = 1;
gbc2.gridwidth = 1;
gbc2.weightx = 10;
gbc2.weighty = 10;
gbl.setConstraints(jb3, gbc2);
jf.add(jb3);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(500, 400);
jf.setVisible(true);
}
}
package test;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestGridBagLayout {
public static void main(String[] args) {
JFrame jf = new JFrame();
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
jf.setLayout(gbl);
JButton jb1 = new JButton("BUTTON1");
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 10;
gbc.weighty = 10;
gbl.setConstraints(jb1, gbc);
jf.add(jb1);
JButton jb2 = new JButton("BUTTON2");
GridBagConstraints gbc1 = new GridBagConstraints();
gbc1.fill = GridBagConstraints.BOTH;
gbc1.gridx = 1;
gbc1.gridy = 0;
gbc1.gridheight = 2;
gbc1.weightx = 20;
gbc1.weighty = 20;
gbl.setConstraints(jb2, gbc1);
jf.add(jb2);
JButton jb3 = new JButton("BUTTON3");
GridBagConstraints gbc2 = new GridBagConstraints();
gbc2.fill = GridBagConstraints.BOTH;
gbc2.gridx = 0;
gbc2.gridy = 1;
gbc2.gridwidth = 1;
gbc2.weightx = 10;
gbc2.weighty = 10;
gbl.setConstraints(jb3, gbc2);
jf.add(jb3);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(500, 400);
jf.setVisible(true);
}
}
展开全部
能,首先给你说嘛下用GridBagLayout各参数的说明
new GridBagConstraints(X轴位置就是从左到右第几个开始,行高就是从上到下第几行开始, 占几个宽度,
占几行高, 1, 0.0, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0);
第四个参数设置为2就能占单元格俩行了
new GridBagConstraints(X轴位置就是从左到右第几个开始,行高就是从上到下第几行开始, 占几个宽度,
占几行高, 1, 0.0, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0);
第四个参数设置为2就能占单元格俩行了
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询