java 板


LINE

constraints.fill = GridBagConstraints.BOTH .VERTICAL .HORIZONTAL constraints.weightx .weighty 在 addComponent 之前作修改, 会有不同的效果。 例如: constraints.fill = GridBagConstraints.BOTH; constraints.weightx = 1.0; constraints.weighty = 1.0; 加在 addComponent(button1); 之前 且其他 constraints.fill 删掉 就会得到 ┌───────────────────────┐ │gridbaglayout              _□╳│ ├───────────────────────┤ │ │Button1 Button2 option1▼ option1▼ 口filled│ │ │ ├───────────────────────┤ │ │ │ │ │ │ ├───────────────────────┤ │ │ │textfield │ │ │ └───────────────────────┘ ※ 引述《Liszt04 (走路往下看都会怕)》之铭言: : 请问依下依个排版的问题 : 我希望排出这样的排版 : ┌┬┬┬┬┐ : ├┴┴┴┴┤ : ├────┤ : └────┘ : 上面是五个button, 中间是依个Jpanel可以画画, 下面是一个JTextField : code如下: : import javax.swing.*; : import java.awt.*; : public class GridBag extends JFrame : { : private GridBagLayout layout; : private GridBagConstraints constraints; : private JButton button1; : private JButton button2; : private JComboBox combobox1; : private JComboBox combobox2; : private JCheckBox checkbox; : private String list[] = {"option1", "option2", "option3", "option4"}; : private JPanel panel; : private JTextField textarea; : public GridBag() : { : super("gridbaglayout"); : layout = new GridBagLayout(); : setLayout(layout); : constraints = new GridBagConstraints(); : button1 = new JButton("Button1"); : button2 = new JButton("Button2"); : combobox1 = new JComboBox(list); : combobox2 = new JComboBox(list); : checkbox = new JCheckBox("filled"); : textarea = new JTextField("textfield", 50); : constraints.fill = GridBagConstraints.HORIZONTAL; : addComponent(button1, 0, 0, 1, 1); : addComponent(button2, 1, 0, 1, 1); : addComponent(combobox1, 2, 0, 1, 1); : addComponent(combobox2, 3, 0, 1, 1); : addComponent(checkbox, 4, 0, 1, 1); : constraints.fill = GridBagConstraints.BOTH; : addComponent(panel, 0, 1, 5, 1); : constraints.fill = GridBagConstraints.HORIZONTAL; : addComponent(textarea, 0, 2, 5, 1); : } : public void addComponent(Component component, int column, int row, int : width, int height) : { : constraints.gridx = column; : constraints.gridy = row; : constraints.gridwidth = width; : constraints.gridheight = height; : layout.setConstraints(component, constraints); : add(component); : } : } : 他跑出来的结果是: : exceptionx in thread "main" lava.lang.Nullpointerexception : at java.util.Hastable.put(Hashtable.java:399) : at java.awt.GridBagLayout.setConstraints(GridBagLayout.java:482) : at GridBag.addComponent(GridBag.java:61) : at GridBag.(init)(GridBag.java:48) : at GridBagTest.main(GridBagTest.java:8) : : 这是GridBagTest的code: : mport javax.swing.*; : import java.awt.*; : public class GridBagTest : { : public static void main(String args[]) : { : GridBag g = new GridBag(); : g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); : g.setSize(600, 400); : g.setResizable(false); : g.setVisible(true); : } : } : 要是红色部分没有打的话 输出是正确的 : 加上去panel之後整个跑不出来 : add(panel) 换成add(button) 的话 排版竟然也是错的 囧 : 请问一下是我哪边写错了 还是我哪边观念不太正确 : 麻烦好心的大大帮我看一下吧 : 谢谢! --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 211.74.9.2
1F:推 PsMonkey:这 ASCII 示意图也太精美了吧? 07/05 02:30
※ 编辑: csihcs 来自: 211.74.9.2 (07/05 02:36)
2F:推 Liszt04:我试试看 真是大感谢~! 07/05 09:08







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:BuyTogether站内搜寻

TOP