防止自定义JPanel扩展

时间:2014-01-27 21:00:55

标签: java swing layout-manager gridbaglayout

我想创建一个自定义JPanel,以允许我的用户选择4个角中的一个。我通过使用4 JRadioButton并在面板上放置TitledBorder来完成此操作。

我希望面板始终尽可能小,以使面板整洁干净。我认为这是可能的,因为布局应该遵循最大和最小尺寸属性。

首先,我尝试使用setMaximumSize()setPreferredSize()将其设置为最小尺寸,但这不起作用。然后我尝试重载getMaximumSize()getPreferredSize(),但这似乎也没有效果。在我的所有布局中,我的面板都会增长,以填充放置它的布局所决定的空间。我已经在我要放置的GridBagLayout上进行了测试,并在BorderLayout上测试了为测试而创建的空JFrame

这是我的代码:

public class OverlayAnchorPanel extends JPanel implements ItemListener{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private AnchorChangeListener anchorListener;
    private JRadioButton rdbtnSouthEast;
    private JRadioButton rdbtnSouthWest;
    private JRadioButton rdbtnNorthWest;
    private JRadioButton rdbtnNorthEast;
    private ButtonGroup radioGroup;

    /**
     * Create the panel.
     */
    public OverlayAnchorPanel() {
        this.radioGroup = new ButtonGroup();
        setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Anchor", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWidths = new int[]{0, 0};
        gridBagLayout.rowHeights = new int[]{0, 0};
        gridBagLayout.columnWeights = new double[]{0.0, 0.0};
        gridBagLayout.rowWeights = new double[]{0.0, 0.0};
        setLayout(gridBagLayout);

        rdbtnNorthEast = new JRadioButton("");
        GridBagConstraints gbc_rdbtnNorthEast = new GridBagConstraints();
        gbc_rdbtnNorthEast.insets = new Insets(0, 0, 5, 0);
        gbc_rdbtnNorthEast.gridx = 1;
        gbc_rdbtnNorthEast.gridy = 0;
        add(rdbtnNorthEast, gbc_rdbtnNorthEast);
        this.radioGroup.add(rdbtnNorthEast);

        rdbtnNorthWest = new JRadioButton("");
        GridBagConstraints gbc_rdbtnNorthWest = new GridBagConstraints();
        gbc_rdbtnNorthWest.insets = new Insets(0, 0, 5, 5);
        gbc_rdbtnNorthWest.gridx = 0;
        gbc_rdbtnNorthWest.gridy = 0;
        add(rdbtnNorthWest, gbc_rdbtnNorthWest);
        this.radioGroup.add(rdbtnNorthWest);

        rdbtnSouthWest = new JRadioButton("");
        GridBagConstraints gbc_rdbtnSouthWest = new GridBagConstraints();
        gbc_rdbtnSouthWest.insets = new Insets(0, 0, 0, 5);
        gbc_rdbtnSouthWest.gridx = 0;
        gbc_rdbtnSouthWest.gridy = 1;
        add(rdbtnSouthWest, gbc_rdbtnSouthWest);
        this.radioGroup.add(rdbtnSouthWest);

        rdbtnSouthEast = new JRadioButton("");
        rdbtnSouthEast.setSelected(true);
        GridBagConstraints gbc_rdbtnSouthEast = new GridBagConstraints();
        gbc_rdbtnSouthEast.gridx = 1;
        gbc_rdbtnSouthEast.gridy = 1;
        add(rdbtnSouthEast, gbc_rdbtnSouthEast);;
        this.radioGroup.add(rdbtnSouthEast);

        // This was my first attempt and did not work
        //this.setMaximumSize(this.getMinimumSize());
        //this.setPreferredSize(this.getMinimumSize());
    }

    public AnchorChangeListener getAnchorListener() {
        return anchorListener;
    }

    public void setAnchorListener(AnchorChangeListener anchorListener) {
        this.anchorListener = anchorListener;
    }


    @Override
    public void itemStateChanged(ItemEvent arg0) {
        OverlayAnchor newAnchor;
        if(arg0.getSource() == this.rdbtnNorthEast){
            newAnchor = OverlayAnchor.NORTHEAST;
        } else if(arg0.getSource() == this.rdbtnNorthWest){
            newAnchor = OverlayAnchor.NORTHWEST;
        } else if(arg0.getSource() == this.rdbtnSouthEast){
            newAnchor = OverlayAnchor.SOUTHEAST;
        } else{
            newAnchor = OverlayAnchor.SOUTHWEST;
        }
        if(this.anchorListener !=null){
            this.anchorListener.anchorChanged(new AnchorChangeEvent(this, newAnchor));
        }
    }

    public Dimension getPreferredSize(){
        return this.getMinimumSize();
    }

    public Dimension getMaximumSize(){
        return this.getMinimumSize();
    }
}

2 个答案:

答案 0 :(得分:1)

如何布置组件可归结为许多事情。第一个显然是选择父容器的布局管理器。

布局管理员可以免费使用getPreferred/Miniumum/MaximumSize中的信息,也可以随意忽略它们,或者根据需要使用它们的组合。

例如,BorderLayout倾向于使用组件的首选大小,但允许父容器根据需要进行扩展和缩小,通常根据需要扩展和缩小CENTER组件。 / p>

在我的测试中,我只是将框架的布局管理器更改为GridBagLayout并得到了这个结果......

Anchor

基本上,GridBagLayout会尊重大小提示,只要它们不与您提供的约束相矛盾。

答案 1 :(得分:0)

为了达到不改变尺寸的自包含自定义组件所需的结果,我不得不将它包装在另一个面板中,该面板将改变尺寸,但在一个将尊重我的首选尺寸的庄园中展示自己面板。

这是我的最终代码:

public class OverlayAnchorPanel extends JPanel implements ItemListener {
    private static final long serialVersionUID = 1L;

    private AnchorChangeListener anchorListener;
    private JRadioButton rdbtnSouthEast;
    private JRadioButton rdbtnSouthWest;
    private JRadioButton rdbtnNorthWest;
    private JRadioButton rdbtnNorthEast;
    private ButtonGroup radioGroup;
    private JPanel panel;

    /**
     * Create the panel.
     */
    public OverlayAnchorPanel() {
        this.radioGroup = new ButtonGroup();;
        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWidths = new int[] {0};
        gridBagLayout.rowHeights = new int[] {0};
        gridBagLayout.columnWeights = new double[]{0.0};
        gridBagLayout.rowWeights = new double[]{0.0};
        setLayout(gridBagLayout);

        panel = new JPanel();
        panel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Anchor", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        GridBagConstraints gbc_panel = new GridBagConstraints();
        gbc_panel.anchor = GridBagConstraints.NORTHWEST;
        gbc_panel.gridx = 0;
        gbc_panel.gridy = 0;
        add(panel, gbc_panel);
        GridBagLayout gbl_panel = new GridBagLayout();
        gbl_panel.columnWidths = new int[] {0, 0};
        gbl_panel.rowHeights = new int[] {0, 0};
        gbl_panel.columnWeights = new double[]{0.0, 0.0};
        gbl_panel.rowWeights = new double[]{0.0, 0.0};
        panel.setLayout(gbl_panel);

        rdbtnNorthEast = new JRadioButton("");
        GridBagConstraints gbc_rdbtnNorthEast = new GridBagConstraints();
        gbc_rdbtnNorthEast.fill = GridBagConstraints.BOTH;
        gbc_rdbtnNorthEast.insets = new Insets(0, 0, 5, 5);
        gbc_rdbtnNorthEast.gridx = 0;
        gbc_rdbtnNorthEast.gridy = 0;
        panel.add(rdbtnNorthEast, gbc_rdbtnNorthEast);
        this.radioGroup.add(rdbtnNorthEast);

        rdbtnNorthWest = new JRadioButton("");
        GridBagConstraints gbc_rdbtnNorthWest = new GridBagConstraints();
        gbc_rdbtnNorthWest.fill = GridBagConstraints.BOTH;
        gbc_rdbtnNorthWest.insets = new Insets(0, 0, 5, 0);
        gbc_rdbtnNorthWest.gridx = 1;
        gbc_rdbtnNorthWest.gridy = 0;
        panel.add(rdbtnNorthWest, gbc_rdbtnNorthWest);
        this.radioGroup.add(rdbtnNorthWest);

        rdbtnSouthWest = new JRadioButton("");
        GridBagConstraints gbc_rdbtnSouthWest = new GridBagConstraints();
        gbc_rdbtnSouthWest.fill = GridBagConstraints.BOTH;
        gbc_rdbtnSouthWest.insets = new Insets(0, 0, 0, 5);
        gbc_rdbtnSouthWest.gridx = 0;
        gbc_rdbtnSouthWest.gridy = 1;
        panel.add(rdbtnSouthWest, gbc_rdbtnSouthWest);
        this.radioGroup.add(rdbtnSouthWest);

        rdbtnSouthEast = new JRadioButton("");
        GridBagConstraints gbc_rdbtnSouthEast = new GridBagConstraints();
        gbc_rdbtnSouthEast.fill = GridBagConstraints.BOTH;
        gbc_rdbtnSouthEast.gridx = 1;
        gbc_rdbtnSouthEast.gridy = 1;
        panel.add(rdbtnSouthEast, gbc_rdbtnSouthEast);
        rdbtnSouthEast.setSelected(true);
        this.radioGroup.add(rdbtnSouthEast);
    }

    public AnchorChangeListener getAnchorListener() {
        return anchorListener;
    }

    public void setAnchorListener(AnchorChangeListener anchorListener) {
        this.anchorListener = anchorListener;
    }


    @Override
    public void itemStateChanged(ItemEvent arg0) {
        OverlayAnchor newAnchor;
        if(arg0.getSource() == this.rdbtnNorthEast){
            newAnchor = OverlayAnchor.NORTHEAST;
        } else if(arg0.getSource() == this.rdbtnNorthWest) {
            newAnchor = OverlayAnchor.NORTHWEST;
        } else if(arg0.getSource() == this.rdbtnSouthEast) {
            newAnchor = OverlayAnchor.SOUTHEAST;
        } else {
            newAnchor = OverlayAnchor.SOUTHWEST;
        }
        if(this.anchorListener !=null) {
            this.anchorListener.anchorChanged(new AnchorChangeEvent(this, newAnchor));
        }
    }

    public Dimension getPreferredSize() {
        return this.getMinimumSize();
    }

    public Dimension getMaximumSize() {
        return this.getMinimumSize();
    }
}