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);
}
}
展开
 我来答
紫薇参星
科技发烧友

推荐于2016-02-18 · 有一些普通的科技小锦囊
知道大有可为答主
回答量:5983
采纳率:92%
帮助的人:3458万
展开全部
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);
}
}
张麻子xx
推荐于2018-05-07 · TA获得超过104个赞
知道答主
回答量:64
采纳率:100%
帮助的人:15.1万
展开全部
能,首先给你说嘛下用GridBagLayout各参数的说明
new GridBagConstraints(X轴位置就是从左到右第几个开始,行高就是从上到下第几行开始, 占几个宽度,
占几行高, 1, 0.0, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0);
第四个参数设置为2就能占单元格俩行了
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式