有人告诉我用摇摆来设计这个。谁能告诉我怎么做?

时间:2014-04-08 10:24:34

标签: swing

我是java的新手,所以请帮助我 要求是这样的。

*-----------------------------------------------*
| label A     |   field A                       | 
| label B     |   field B                       |
| label C     |   field C                       |
| label D     |   field D                       |   
|                                               |
| Button1 Button2 Button3 Button4               |
|                                               |
|                                               |
|           JTABLE                              |
|                                               |
*-----------------------------------------------*

我尝试过这段代码

'在这里,我正在创建组布局'

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());         。的getContentPane()的setLayout(布局);

creating horizontal Group

layout.setHorizo​​ntalGroup(

    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

        .addGroup(layout.createSequentialGroup()

        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

        .addGroup(layout.createSequentialGroup()

        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup()
                    .addGap(40, 40, 40)
                    .addComponent(jLabel2)
                    .addComponent(txtitem, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)
                            .addComponent(txtprice, javax.swing.GroupLayout.DEFAULT_SIZE, 100,javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4) 
                            .addComponent(txtquantity, javax.swing.GroupLayout.DEFAULT_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5)
                            .addComponent(txtreorder, javax.swing.GroupLayout.DEFAULT_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnupdate)//,javax.swing.GroupLayout.DEFAULT_SIZE, 100, javax.swing.GroupLayout.DEFAULT_SIZE)//,javax.swing.GroupLayout.DEFAULT_SIZE, 100, javax.swing.GroupLayout.DEFAULT_SIZE)
                            .addComponent(btndelete,javax.swing.GroupLayout.DEFAULT_SIZE, 100, javax.swing.GroupLayout.DEFAULT_SIZE)
                            .addComponent(btnlist,javax.swing.GroupLayout.DEFAULT_SIZE, 100, javax.swing.GroupLayout.DEFAULT_SIZE)
                            .addComponent(btnadd)//,javax.swing.GroupLayout.DEFAULT_SIZE, 100, javax.swing.GroupLayout.DEFAULT_SIZE)

                            .addComponent(txtreorder)
                            .addComponent(jLabel5)  )
                    )
                    )
                    );

'这是一个垂直布局'

    layout.setVerticalGroup(
        layout.createParallelGroup()
        .addGroup(layout.createSequentialGroup()
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(40, 40, 40)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel2)
               .addComponent(txtitem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel3)
               .addComponent(txtprice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel4)
                .addComponent(txtquantity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel5)
                .addComponent(txtreorder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
               // .addComponent(btnadd))
                .addGap(40, 40, 40)
                .addComponent(btnadd,javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(txtreorder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btnupdate,javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(txtreorder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addComponent(btndelete,javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addComponent(txtreorder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addComponent(btnlist,javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addComponent(txtreorder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            //.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
            //.addContainerGap()
            )));



}

1 个答案:

答案 0 :(得分:1)

您上传的代码无法在没有GUI设计器的情况下阅读和使用。在学习阶段尽量避免使用GUI设计器。在学习过程中,最好坚持标准的简单布局。

为了保持代码简单,您可以尝试这种方法:

Frame mockup

生成的JFrame将如下所示:        Preview Design

您可能需要使用JLabel水平对齐进行一些调整才能使事情正确。

此示例代码应该完成工作:

    public class LayoutSample extends JFrame {

        public LayoutSample() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        p1 = new JPanel(new BorderLayout());
        p2 = new JPanel(new BorderLayout());
        p3 = new JPanel(new GridLayout(4, 2));
        p4 = new JPanel(new FlowLayout());
        p5 = new JPanel(new BorderLayout());

        l1 = new JLabel("Label1");
        l2 = new JLabel("Label2");
        l3 = new JLabel("Label3");
        l4 = new JLabel("Label4");
        tf1 = new JTextField();
        tf2 = new JTextField();
        tf3 = new JTextField();
        tf4 = new JTextField();
        p3.add(l1);
        p3.add(tf1);
        p3.add(l2);
        p3.add(tf2);
        p3.add(l3);
        p3.add(tf3);
        p3.add(l4);
        p3.add(tf4);

        b1 = new JButton("Button1");
        b2 = new JButton("Button2");
        b3 = new JButton("Button3");
        b4 = new JButton("Button4");
        p4.add(b1);
        p4.add(b2);
        p4.add(b3);
        p4.add(b4);

        t = new JTable();
        t.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{
                    {null, null, null, null},
                    {null, null, null, null},
                    {null, null, null, null}
                },
                new String[]{
                    "Title 1", "Title 2", "Title 3", "Title 4"
                }
        ));
        sc = new JScrollPane(t);

        p5.add(sc, BorderLayout.CENTER);
        p2.add(p3, BorderLayout.NORTH);
        p2.add(p4, BorderLayout.SOUTH);
        p1.add(p2, BorderLayout.NORTH);
        p1.add(p5, BorderLayout.CENTER);
        add(p1);

        pack();
    }

    public static void main(String[] args) {
        new LayoutSample().setVisible(true);
    }

    private JPanel p1, p2, p3, p4, p5;
    private JLabel l1, l2, l3, l4;
    private JTextField tf1, tf2, tf3, tf4;
    private JButton b1, b2, b3, b4;
    private JScrollPane sc;
    private JTable t;
}