为什么我的JPanel会占用JFrame?

时间:2014-08-05 02:18:45

标签: java swing layout-manager boxlayout

我对编程比较陌生,所以如果这个问题很愚蠢,我很抱歉。我正在创建一个Java程序,它在JPanel中包含一个JButton,而JPanel在JFrame中。另一个按钮位于JPanel外部,但仍在JFrame中。我将布局设置为BoxLayout。我的问题是,我制作黑色的面板占据了整个JFrame,除了第二个按钮的位置。如何制作JPanel,只占用第一个按钮周围的区域?

public class alt {
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    JButton button1 = new JButton("button 1");
    JButton button2 = new JButton("button 2");
    public alt(){
        frame.setVisible(true);
        frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
        panel.setBackground(Color.black);
        frame.setTitle("test");
        frame.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
        panel.add(button1);
        frame.add(panel);
        frame.add(button2);
        button2.setAlignmentX(Component.CENTER_ALIGNMENT);
    }
}

2 个答案:

答案 0 :(得分:2)

您可以使用不同的布局管理器,它可以让您更好地控制决定如何分配空间并处理填充,例如GridBagLayout ......

Example

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class SampleLayout {

    public static void main(String[] args) {
        new SampleLayout();
    }

    public SampleLayout() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JPanel panel = new JPanel();
                JButton button1 = new JButton("button 1");
                JButton button2 = new JButton("button 2");

                panel.setBackground(Color.black);
                panel.add(button1);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                frame.add(panel, gbc);
                frame.add(button2, gbc);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

请查看Laying Out Components Within a Container了解详情

答案 1 :(得分:1)

您的面板占据框架内容窗格的大部分原因在于 在BoxLayout经理的工作方式中,最小的,首选的, 和组件的最大值。它占用了组件的最大值 考虑到了。由于JPanel的最大值很大,所以需要 所有可用空间。解决方案是更改最大值 小组。但是,这是不好的做法。我不建议使用 BoxLayout经理 - 它非常弱,导致代码不佳。

我建议使用MigLayout经理或GroupLayout经理。

我提供了三种解决方案:纠正的BoxLayout解决方案,MigLayout解决方案, 和GroupLayout解决方案。

BoxLayout解决方案

我们确定按钮的最大尺寸并更改面板的尺寸 比按钮的大一点。

package com.zetcode;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class BoxLayoutPanel extends JFrame {

    public BoxLayoutPanel() {

        initUI();
    }

    private void initUI() {

        JPanel cpane = (JPanel) getContentPane();
        cpane.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
        cpane.setLayout(new BoxLayout(cpane, 
                BoxLayout.Y_AXIS));        

        JPanel pnl = new JPanel();

        JButton btn1 = new JButton("Button 1");
        JButton btn2 = new JButton("Button 2");

        Dimension dm = btn1.getMaximumSize();
        dm.height += 15;
        dm.width += 15;

        pnl.setMaximumSize(dm);

        pnl.setBackground(Color.black);
        add(pnl);
        add(Box.createVerticalStrut(10));
        pnl.add(btn1);
        btn2.setAlignmentX(Component.CENTER_ALIGNMENT);
        add(btn2);

        setTitle("BoxLayout solution");
        pack();
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                BoxLayoutPanel ex = new BoxLayoutPanel();
                ex.setVisible(true);
            }
        });   
    }
}

这不是一个干净的解决方案。通常,我们应该避免调用getMaximumSize()和。{ 应用程序代码中的setMaximumSize() - 这是布局管理器的工作。同样在三种情况下,我们使用固定像素宽度:当我们定义空边框,垂直支柱和最大面板尺寸时。但是,此代码不可移植。 当屏幕分辨率发生变化时,像素宽度会发生变化。这是一个 BoxLayout经理的缺点。

MigLayout解决方案

此解决方案更清洁,更便携。 MigLayout是第三方 经理,所以我们需要下载额外的库。

package com.zetcode;

import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;


public class MigLayoutPanel extends JFrame {

    public MigLayoutPanel(){

        initUI();

        setTitle("MigLayout solution");
        setLocationRelativeTo(null);       
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void initUI() {

        JPanel main = new JPanel(new MigLayout("center"));

        JPanel pnl2 = new JPanel();

        JButton btn1 = new JButton("Button 1");
        JButton btn2 = new JButton("Button 2");

        pnl2.setBackground(Color.black);

        pnl2.add(btn1);
        main.add(pnl2, "wrap");
        main.add(btn2, "alignx center");

        add(main);

        pack();
    }


    public static void main(String[] args){

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                MigLayoutPanel ex = new MigLayoutPanel();
                ex.setVisible(true);
            }
        });
    }
}

MigLayout solution

GroupLayout解决方案

GroupLayout是一个内置的布局管理器。使用MigLayout,他们是最多的 便携而灵活的布局管理器。

package com.zetcode;

import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import javax.swing.GroupLayout;
import static javax.swing.GroupLayout.Alignment.CENTER;
import static javax.swing.GroupLayout.DEFAULT_SIZE;
import static javax.swing.GroupLayout.PREFERRED_SIZE;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import static javax.swing.LayoutStyle.ComponentPlacement.RELATED;


public class GroupLayoutPanel extends JFrame {

    public GroupLayoutPanel(){

        initUI();

        setTitle("GroupLayout solution");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);        
    }

    private void initUI() {

        Container pane = getContentPane();
        GroupLayout gl = new GroupLayout(pane);
        pane.setLayout(gl); 

        JPanel pnl = new JPanel();

        JButton btn1 = new JButton("Button 1");
        pnl.add(btn1);

        JButton btn2 = new JButton("Button 2");

        pnl.setBackground(Color.black);

        gl.setAutoCreateGaps(true);

        gl.setHorizontalGroup(gl.createSequentialGroup()
                .addContainerGap(DEFAULT_SIZE, Integer.MAX_VALUE)
                .addGroup(gl.createParallelGroup(CENTER)
                        .addComponent(pnl, DEFAULT_SIZE, DEFAULT_SIZE, 
                                PREFERRED_SIZE)
                        .addComponent(btn2))
                .addContainerGap(DEFAULT_SIZE, Integer.MAX_VALUE)
        );

        gl.setVerticalGroup(gl.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnl, DEFAULT_SIZE, DEFAULT_SIZE, 
                        PREFERRED_SIZE)
                .addPreferredGap(RELATED)
                .addComponent(btn2)
                .addContainerGap()
        );

        pack();
    }


    public static void main(String[] args){

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                GroupLayoutPanel ex = new GroupLayoutPanel();
                ex.setVisible(true);
            }
        });
    }
}

GroupLayout solution