向JPanel添加边框会影响添加到面板的组件的布局

时间:2016-03-16 16:14:32

标签: java swing layout-manager gridbaglayout

我有一个添加到JFrame的JPanel。面板上有许多垂直添加的按钮。当我为面板设置边框时,按钮的大小越来越小,布局就越来越好。

有人可以建议为什么布局因边框设置而受到影响吗?

添加边框之前 Image before border

添加边框后 enter image description here

以下是上述设计的代码

frame = new JFrame();
    //frame.setBounds(100, 100, 450, 300);
    frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new BorderLayout(0, 0));

    JPanel actionPanel = new JPanel();
    actionPanel.setPreferredSize(new Dimension(200, 450));
    actionPanel.setBackground(Color.BLUE);
    frame.getContentPane().add(actionPanel, BorderLayout.WEST);
    actionPanel.setLayout(new GridLayout(2, 1, 0, 0));

    JPanel panel = new JPanel();
    panel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
    //panel.setBackground(Color.GRAY);
    actionPanel.add(panel);
    panel.setLayout(new GridBagLayout());

    //GridbagLayout constraints
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.weighty = 0;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.anchor = GridBagConstraints.FIRST_LINE_START;

    JButton btnPos = new JButton("Point of Sale");
    Dimension btnPosDimension = new Dimension(actionPanel.getPreferredSize().width, 40);
    btnPos.setPreferredSize(btnPosDimension);
    panel.add(btnPos, gbc);

    gbc.gridy++;        
    JButton btnCategory = new JButton("Category");
    btnCategory.setPreferredSize(btnPosDimension);
    panel.add(btnCategory, gbc);

    gbc.gridy++;        
    JButton btnProduct = new JButton("Product");
    btnProduct.setPreferredSize(btnPosDimension);
    panel.add(btnProduct, gbc);

    gbc.gridy++;        
    JButton btnVendor = new JButton("Vendor");
    btnVendor.setPreferredSize(btnPosDimension);
    panel.add(btnVendor, gbc);

    gbc.weighty = 1;
    gbc.gridy++;
    panel.add(Box.createGlue(), gbc); //Adding a component to feel the area.

    JPanel panel_1 = new JPanel();
    actionPanel.add(panel_1);


    JPanel contentPanel = new JPanel();
    frame.getContentPane().add(contentPanel, BorderLayout.CENTER);

2 个答案:

答案 0 :(得分:2)

边框被添加到JPanel的内部,因此内部可用空间更少。您已指定容纳面板的容器的大小,因此在添加边框时,内容没有足够的空间。

答案 1 :(得分:1)

我只是缩小了按钮的宽度2.因为2使用了行边框。

这是工作代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Example {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        //frame.setBounds(100, 100, 450, 300);
        frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new BorderLayout(0, 0));

        JPanel actionPanel = new JPanel();
        actionPanel.setPreferredSize(new Dimension(200, 450));
        actionPanel.setBackground(Color.BLUE);
        frame.getContentPane().add(actionPanel, BorderLayout.WEST);
        actionPanel.setLayout(new GridLayout(2, 1, 0, 0));

        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
        //panel.setBackground(Color.GRAY);
        actionPanel.add(panel);
        panel.setLayout(new GridBagLayout());

        //GridbagLayout constraints
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.weighty = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;

        JButton btnPos = new JButton("Point of Sale");
        Dimension btnPosDimension = new Dimension(actionPanel.getPreferredSize().width - 2, 40);
        btnPos.setPreferredSize(btnPosDimension);
        panel.add(btnPos, gbc);

        gbc.gridy++;        
        JButton btnCategory = new JButton("Category");
        btnCategory.setPreferredSize(btnPosDimension);
        panel.add(btnCategory, gbc);

        gbc.gridy++;        
        JButton btnProduct = new JButton("Product");
        btnProduct.setPreferredSize(btnPosDimension);
        panel.add(btnProduct, gbc);

        gbc.gridy++;        
        JButton btnVendor = new JButton("Vendor");
        btnVendor.setPreferredSize(btnPosDimension);
        panel.add(btnVendor, gbc);

        gbc.weighty = 1;
        gbc.gridy++;
        panel.add(Box.createGlue(), gbc); //Adding a component to feel the area.

        JPanel panel_1 = new JPanel();
        actionPanel.add(panel_1);


        JPanel contentPanel = new JPanel();
        frame.getContentPane().add(contentPanel, BorderLayout.CENTER);

        frame.setVisible(true);
    }


}