GridBagLayout的使用问题,急!!!! 请教大家,我想生成类似于计算器的界面,上面的“=”是占用两个格的大小。这里我想用网格包布局GridBagLayout.但是不会用。请问怎么设置组件占2个格子.GridBagConstraints类好像只有设置组件在整个网格中的起始位置(gridx,gridy),但没有指定结束位置啊急!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 刚又看了下api,好像gridheight和gridewidth是用来设置组件占据的垂直和水平单元格个数,但是整个Frame是分成几行几列呢,这个怎么设置啊 import java.awt.*; import java.util.*; import java.applet.Applet; public class GridBagEx1 extends Applet { protected void makebutton(String name, GridBagLayout gridbag, GridBagConstraints c) { Button button = new Button(name); gridbag.setConstraints(button, c); add(button); } public void init() { GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); setFont(new Font("SansSerif", Font.PLAIN, 14)); setLayout(gridbag); c.fill = GridBagConstraints.BOTH; c.weightx = 1.0; makebutton("Button1", gridbag, c); makebutton("Button2", gridbag, c); makebutton("Button3", gridbag, c); c.gridwidth = GridBagConstraints.REMAINDER; //end row makebutton("Button4", gridbag, c); c.weightx = 0.0; //reset to the default makebutton("Button5", gridbag, c); //another row c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row makebutton("Button6", gridbag, c); c.gridwidth = GridBagConstraints.REMAINDER; //end row makebutton("Button7", gridbag, c); c.gridwidth = 1; //reset to the default c.gridheight = 2; c.weighty = 1.0; makebutton("Button8", gridbag, c); c.weighty = 0.0; //reset to the default c.gridwidth = GridBagConstraints.REMAINDER; //end row c.gridheight = 1; //reset to the default makebutton("Button9", gridbag, c); makebutton("Button10", gridbag, c); setSize(300, 100); } public static void main(String args[]) { Frame f = new Frame("GridBag Layout Example"); GridBagEx1 ex1 = new GridBagEx1(); ex1.init(); f.add("Center", ex1); f.pack(); f.setSize(f.getPreferredSize()); f.show(); } } 不行就setlayout(null) 然后每个按钮都可以 setBounds(int x,int y,int width,int heigth)随便放了 GridBagConstraints类的weightx,weighty表示权重,到底是什么权重啊 Panel的分行处理 jar打包后getResource(...)不能获取外部的资源 新手求救..请问怎么判断键盘有两个键被按下?? 自己做了个俄罗斯方块,但有几个细节有点问题 一台Linux的机器如何通过一台Windows的机器访问局域网内的其他机器。 java编程难题 请教一下我如何设置JTextArea对象和我的JPanel对象的大小保持一致呢?大大们帮帮我啊 关于字符串的处理 jsp测试连接Oracle时的问题. jdk1.2 下做的小东西,在 j2sdk 下报错 这个方法有什么功能 希望能有工作经验的人来模拟写个标书来让我练习。。谢谢。
import java.util.*;
import java.applet.Applet; public class GridBagEx1 extends Applet { protected void makebutton(String name,
GridBagLayout gridbag,
GridBagConstraints c) {
Button button = new Button(name);
gridbag.setConstraints(button, c);
add(button);
} public void init() {
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints(); setFont(new Font("SansSerif", Font.PLAIN, 14));
setLayout(gridbag); c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
makebutton("Button1", gridbag, c);
makebutton("Button2", gridbag, c);
makebutton("Button3", gridbag, c); c.gridwidth = GridBagConstraints.REMAINDER; //end row
makebutton("Button4", gridbag, c); c.weightx = 0.0; //reset to the default
makebutton("Button5", gridbag, c); //another row c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row
makebutton("Button6", gridbag, c); c.gridwidth = GridBagConstraints.REMAINDER; //end row
makebutton("Button7", gridbag, c); c.gridwidth = 1; //reset to the default
c.gridheight = 2;
c.weighty = 1.0;
makebutton("Button8", gridbag, c); c.weighty = 0.0; //reset to the default
c.gridwidth = GridBagConstraints.REMAINDER; //end row
c.gridheight = 1; //reset to the default
makebutton("Button9", gridbag, c);
makebutton("Button10", gridbag, c); setSize(300, 100);
} public static void main(String args[]) {
Frame f = new Frame("GridBag Layout Example");
GridBagEx1 ex1 = new GridBagEx1(); ex1.init(); f.add("Center", ex1);
f.pack();
f.setSize(f.getPreferredSize());
f.show();
}
}