添加按钮到弹簧布局

时间:2013-10-04 12:31:24

标签: java forms button user-interface springlayout

我一直在尝试在我使用SpringLayout构建的表单中添加2个按钮。

我似乎无法弄清楚如何在表单底部添加按钮。我尝试了几种方法,但它们似乎没有用。

主要方法如下:

private static void createAndShowGUI() {
    String[] labels = {"Name: ", "Fax: ", "Email: ", "Address: ", "Test: "};
    int numPairs = labels.length;

    JFrame frame = new JFrame("SpringForm");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and populate the panel.
    JPanel p = new JPanel(new SpringLayout());

    for (int i = 0; i < numPairs; i++)  {
        JLabel l = new JLabel(labels[i], JLabel.TRAILING);
        p.add(l);
        JTextField textField = new JTextField(10);
        l.setLabelFor(textField);
        p.add(textField);
    }

    //Set up the content pane.
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new SpringLayout());

    //Add the buttons.
    contentPane.add(new JButton("Button 1"));
    contentPane.add(new JButton("Button 2"));

    //Lay out the panel.
    SpringUtilities.makeCompactGrid(p,
                                    numPairs, 2, //rows, cols
                                    6, 6,        //initX, initY
                                    6, 6);   

    //Set up the content pane.
    p.setOpaque(true);  //content panes must be opaque
    frame.setContentPane(p);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

感谢任何帮助。

0 个答案:

没有答案