窗口大小调整时的中心面板

时间:2014-11-06 09:17:15

标签: java swing layout-manager gridbaglayout null-layout-manager

我想保留一个我在窗口中心使用绝对布局创建的面板,即使调整窗口大小(如果可能)。我遇到了一些建议here和[这里] [2],但没有骰子!以下是我的示例代码,任何想法或建议?我没有像JLable这样的单个组件集中的问题,但我希望将面板集中在许多组件中!

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

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.JLabel;


public class TestPanel extends JFrame {

    private JLabel lblSetupTitle;
    private Border compoundBorder, outlineColorBorder, outlineBorder;
    private JTextArea txtrManageData;
    private JPanel childPanel;

    public TestPanel() 
    {
        setBackground(Color.white);
        outlineColorBorder = BorderFactory.createLineBorder(Color.gray);
        outlineBorder = BorderFactory.createEmptyBorder(20, 20, 20, 20);
        compoundBorder = BorderFactory.createCompoundBorder(outlineColorBorder, outlineBorder);

        lblSetupTitle = new JLabel("Setup");
        lblSetupTitle.setBounds(443, 288, 44, 23);
        txtrManageData = new JTextArea("Text Area Text");
        txtrManageData.setBounds(393, 322, 142, 61);

        childPanel = new JPanel();
        childPanel.setLocation(89, 38);
        childPanel.setSize(921, 452);
        childPanel.setBorder(compoundBorder);

        setupGUIElements();
        setupPanel();
    }

    private void setupGUIElements()
    {
        txtrManageData.setBackground(null);
        txtrManageData.setLineWrap(true);
        txtrManageData.setWrapStyleWord(true);
    }

    private void setupPanel()
    {
        getContentPane().setLayout(new GridBagLayout()); // set layout of parent panel to GridBagLayout
        childPanel.setLayout(null); // set layout of child panel to AbsoluteLayout
        childPanel.add(lblSetupTitle);
        childPanel.add(txtrManageData);

        getContentPane().add(childPanel, new GridBagConstraints());

        this.setSize(1020, 500);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                TestPanel ex = new TestPanel();
                ex.setVisible(true);
            }
        });
    }
}

编辑:有关创建此类内容的任何提示,链接和指导

example gui i would like to create

2 个答案:

答案 0 :(得分:2)

我嵌套布局。

enter image description here

enter image description here

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

public class ThreeButtonTextFieldCombo {

    private JPanel ui = null;

    ThreeButtonTextFieldCombo() {
        initUI();
    }

    public final void initUI() {
        if (ui!=null) return;
        ui = new JPanel(new GridBagLayout());
        ui.setBorder(new TitledBorder("Parent Panel"));

        JPanel controls = new JPanel(new GridLayout(1,0,10,10));
        ui.add(controls);
        controls.setBackground(Color.RED);
        controls.setBorder(new TitledBorder("Child Panel"));

        for (int ii=1; ii<4; ii++) {
            addLabelAndField(controls, "String " + ii);
        }
    } 

    public JComponent getUI() {
        return ui;
    }

    private void addLabelAndField(JPanel panel, String text) {
        JPanel controls = new JPanel(new BorderLayout(3, 3));
        controls.setBorder(new EmptyBorder(20,20,20,20));
        JLabel l = new JLabel(text);
        controls.add(l, BorderLayout.PAGE_START);

        JTextArea ta = new JTextArea(text, 2, 8);
        controls.add(new JScrollPane(ta));

        panel.add(controls);
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame("Three Button/Text Field Combo");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);
                ThreeButtonTextFieldCombo tbtfc = 
                        new ThreeButtonTextFieldCombo();
                f.setContentPane(tbtfc.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());
                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

答案 1 :(得分:0)

您的代码的第一个问题是您使用GridBagConstraints的空实例化添加子面板。我以前从未见过这样的用途。

getContentPane().add(childPanel, new GridBagConstraints());

不要将任何布局设置为内容窗格,只需添加如下:

getContentPane().add(childPanel);

现在,如果您运行它,您将获得中间的两个组件,您可以使用 setBounds(..)方法定义它们。

几乎每个人都在评论你的问题,你不应该使用null布局,而是使用其他布局。我会使用 GridBagLayout 来组织图表中的三个按钮和三个文本字段。然后,您可以在子面板上设置边框(..)。

如果你真的必须使用绝对布局,那么你将不得不做一些数学运算。

如果您的第一个标签是这样的:

labell1.setBounds(443, 288, 44, 23);

然后你的第二个标签应该是这样的:

labell2.setBounds(443 + someXDisplacement, 288, 44, 23);

..和第三:

labell3.setBounds(443 + (someXDisplacement x 2), 288, 44, 23);

你明白了。