如题,swing开发中,一个界面包括一个列表和下面一个panel,根据列表的变化修改panel(主要是一些文本框等给用户填写数据)的表现,还要收集panel的数据,请问什么方式比较好?
不想把界面定制和逻辑代码写到一起。GridBagLayout中,每个单元格的宽度都是等同的,怎么实现变化?
比如一个Jframe中,采用GridBagLayout布局,我希望左边那个单元格占JFrame的宽度的80%,怎么实现?

解决方案 »

  1.   

    panel.add(button,
                               new GridBagConstraints(1, 0,(行列) 1, 1,(行列占格子数) 0.8(占80%的剩余宽度), 0.0, GridBagConstraints.WEST(靠西边放),
                                                      GridBagConstraints.NONE,(不放大)
                                                      new Insets(0, 35, 2, 0)(边框,上左下右), 0, 0));详细请看api
      

  2.   

    frequencyPanel.add(sameFreBtn,
                               new GridBagConstraints(0, 0, 4, 1, 1.0, 0.0
                                                      , GridBagConstraints.WEST,
                                                      GridBagConstraints.NONE,
                                                      new Insets(0, 35, 2, 0), 0, 0));
    frequencyPanel.add(sameFreBtn2,
                               new GridBagConstraints(4, 0, 1, 1, 1.0, 0.0
                                                      , GridBagConstraints.WEST,
                                                      GridBagConstraints.NONE,
                                                      new Insets(0, 35, 2, 0), 0, 0));
    这样第一个sameFreBtn按钮就是第二个sameFreBtn2的4倍。
      

  3.   

    大概的思路如下:
    class Student{...}
    class Professor {...}class StudentInfoForm extends JPanel{...} // 收集学生信息
    class ProfessorInfoForm extends JPanel {...} // 收集教授信息String labels = {"Student","Professor"};
    JList list = new JList(lables);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);CardLayout layout = new CardLayout();
    JPanel mainPanel = new JPanel(layout);
    mainPanel.add(new StudentInfoForm(),"Student");
    mainPanel.add(new ProfessorInfoForm(),"Professor");list.addListSelectionListener(new ListSelectionListener(){
        public void valueChanged(ListSelectionEvent e) {
            layout.show(mainPanel,(String)list.getSelectedValue());
        }
    });