Java GridBagLayout列大小

时间:2018-01-13 12:58:08

标签: java jpanel

使用此代码,我可以轻松地将一些组件添加到JPanel布局中。但是现在,正如你从图像中看到的那样,我希望JTextField超越它自己的一半。这是我的代码的一部分,只需单击按钮即可插入组件。我能怎么做?任何帮助,将不胜感激,谢谢

EDITED

 public class Nuova_password extends JPanel {

    private static final int PREF_W = 550;
    private static final int PREF_H = 650;
    private JPanel gridPanel = new JPanel(new GridLayout(0, 1));

    public Nuova_password() {
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(new JButton(new AbstractAction("Add Panel") {

            @Override
            public void actionPerformed(ActionEvent e) {
                gridPanel.add(createButtonPanel());
                gridPanel.revalidate();
                gridPanel.repaint();
            }
        }));

        gridPanel.setBorder(new EmptyBorder(0, 15, 0, 15));

        JPanel borderLayoutPanel = new JPanel(new BorderLayout());
        borderLayoutPanel.add(gridPanel, BorderLayout.PAGE_START);
        JScrollPane scrollPane = new JScrollPane(borderLayoutPanel);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

        setPreferredSize(new Dimension(PREF_W, PREF_H));
        setLayout(new BorderLayout());
        add(scrollPane);
        add(buttonPanel, BorderLayout.PAGE_END);

    }

    private JPanel createButtonPanel() {
        JPanel pane = new JPanel(new GridBagLayout());

        JButton button;
        JLabel lblTitolo;
        JTextField ed_dato_campo;

        pane.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.PAGE_START;

        lblTitolo = new JLabel("Button 1");
        lblTitolo.setFont(new Font("Arial", Font.PLAIN, 16));

        c.weightx = 0.5;
        c.gridx = 0;
        c.gridy = 0;
        pane.add(lblTitolo, c);

        ed_dato_campo = new JTextField("Button 2");
        ed_dato_campo.setFont(new Font("Arial", Font.PLAIN, 20));
        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 3;
        c.fill = GridBagConstraints.HORIZONTAL;
        pane.add(ed_dato_campo, c);

        button = new JButton("Button 3");
        c.gridx = 3;
        c.gridy = 1;
        c.gridwidth = 1;
        pane.add(button, c);

        button = new JButton("Long-Named Button 4");
        c.ipady = 40;
        c.weightx = 0.0;
        c.gridwidth = 4;
        c.gridx = 0;
        c.gridy = 2;
        pane.add(button, c);
        return pane;
    }

enter image description here

1 个答案:

答案 0 :(得分:0)

你应该让4列,使ed_dato_campo填充3列,button3将在gridx = 3,button4将填充4列。你应该用不同的JButton对象定义每个按钮,对不起,如果我有任何不好的语法:)

ed_dato_campo = new JTextField("Button 2");
    ed_dato_campo.setFont(new Font("Arial", Font.PLAIN, 20));
    c.gridx = 0;
    c.gridy = 1;
    c.gridwidth = 3;
    c.fill = GridBagConstraints.HORIZONTAL;
    pane.add(ed_dato_campo, c);

    button = new JButton("Button 3");
    c.gridx = 3;
    c.gridy = 1;
    c.gridwidth = 1;
    pane.add(button, c);

    button = new JButton("Long-Named Button 4");
    c.ipady = 40;
    c.weightx = 0.0;
    c.gridwidth = 4;
    c.gridx = 0;
    c.gridy = 2;
    pane.add(button, c);
    return pane;