JPanels之间的差距

时间:2013-12-04 14:05:24

标签: java swing jpanel

我有JPanels的问题。我有1个主面板(GridBagLayout)并且有4个面板。我不想再看到中间的这个***差距,扭曲!当我在面板上使用填充它可以工作但是有一个问题左边的更多面板不能水平调整大小。所以我选择左边填充垂直的桌面,右边填充两个。它工作正常,因为左侧没有调整大小但我仍然在中间有这个间隙,当我调整框架大小时,这个间隙会从小到大改变大小; /

屏幕:http://i44.tinypic.com/smr42a.png

enter image description here

编辑: 当所有面板都在某个Jpanel中时(我正在使用netbeans设计模式并强制添加主面板),你能够修复它吗?

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

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

public class Example extends JFrame {

public static void main(String[] args) {
    final JFrame f = new JFrame();
    JPanel p0 = new JPanel();

    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    JPanel p3 = new JPanel();
    JPanel p4 = new JPanel();


    p1.setBorder(BorderFactory.createTitledBorder("panel 1"));
    p2.setBorder(BorderFactory.createTitledBorder("panel 2"));
    p3.setBorder(BorderFactory.createTitledBorder("panel 3"));
    p4.setBorder(BorderFactory.createTitledBorder("panel 4"));
    p1.setBackground(Color.RED);
    p2.setBackground(Color.GREEN);
    p3.setBackground(Color.BLUE);
    p4.setBackground(Color.CYAN);


    f.getContentPane().setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 0;
    c.ipadx = 50;
    p0.add(p1,c);

    c.gridy = 1;
    c.fill = GridBagConstraints.VERTICAL;
    c.weighty = 1;
    p0.add(p2,c);

    c.gridx = 1;
    c.gridy = 0;
    c.ipadx = 0;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weighty = 0;
    c.weightx = 1;
    p0.add(p3,c);

    c.fill = GridBagConstraints.BOTH;
    c.weighty = 1;
    c.gridy = 1;
    p0.add(p4,c);

    f.add(p0);
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    f.pack();
    f.setVisible(true);
}

}

2 个答案:

答案 0 :(得分:1)

尝试下一个例子。它没有任何差距/插图:

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

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

public class Example extends JFrame {

    public static void main(String[] args) {
        final JFrame f = new JFrame();
        JPanel p0 = new JPanel(new GridBagLayout());

        JPanel p1 = new JPanel();
        JPanel p2 = new JPanel();
        JPanel p3 = new JPanel();
        JPanel p4 = new JPanel();

        p1.setBorder(BorderFactory.createTitledBorder("panel 1"));
        p2.setBorder(BorderFactory.createTitledBorder("panel 2"));
        p3.setBorder(BorderFactory.createTitledBorder("panel 3"));
        p4.setBorder(BorderFactory.createTitledBorder("panel 4"));
        p1.setBackground(Color.RED);
        p2.setBackground(Color.GREEN);
        p3.setBackground(Color.BLUE);
        p4.setBackground(Color.CYAN);

        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.ipadx = 50;
        p0.add(p1, c);

        c.gridy = 1;
        c.fill = GridBagConstraints.VERTICAL;
        c.weighty = 1;
        p0.add(p2, c);

        c.gridx = 1;
        c.gridy = 0;
        c.ipadx = 0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weighty = 0;
        c.weightx = 1;
        p0.add(p3, c);

        c.fill = GridBagConstraints.BOTH;
        c.weighty = 1;
        c.gridy = 1;
        p0.add(p4, c);

        f.getContentPane().add(p0,BorderLayout.CENTER);
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }

}

看起来像:

enter image description here

panel1的大小已固定。

panel2仅垂直调整大小。

panel3仅水平调整大小。

panel4垂直和水平调整大小。

编辑:不要将LayoutManager设置为contentPane,使用默认值(BorderLayout)。同时将GridBagLaout设置为p0,一切正常。我建议你阅读LayoutManager's

答案 1 :(得分:0)

将框架布局设置为“绝对”,然后您可以将选项卡式窗格一直移动到顶部,这样,它与框架重叠,因此不再可见。如果您想在顶部放置一个面板,但又讨厌标签,您可以将标签面板的布局设置为“绝对”,并在其上方放置一个面板,以隐藏标签。它对我有用。