作者csihcs (非天夜翔)
看板java
标题Re: [问题] 一个 GridBagLayout 的问题
时间Sun Jul 5 02:13:02 2009
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