Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 调整窗口大小时的中心面板_Java_Swing_Layout Manager_Gridbaglayout_Null Layout Manager - Fatal编程技术网

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

Java 调整窗口大小时的中心面板,java,swing,layout-manager,gridbaglayout,null-layout-manager,Java,Swing,Layout Manager,Gridbaglayout,Null Layout Manager,我想在我的窗口中心保留使用绝对布局创建的面板,即使在调整窗口大小时(如果可能)。我遇到了一些建议和[这里][2],但没有成功!下面是我的示例代码,有什么想法或建议吗?我没有像JLable一样将单个组件居中的问题,但我希望将包含许多组件的面板居中 import java.awt.Color; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JFrame; import ja

我想在我的窗口中心保留使用绝对布局创建的面板,即使在调整窗口大小时(如果可能)。我遇到了一些建议和[这里][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);
            }
        });
    }
}
编辑:关于创建这样的东西的任何提示、链接和指导


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

getContentPane().add(childPanel, new GridBagConstraints());
不要将任何布局设置为内容窗格,只需按如下方式添加它:

getContentPane().add(childPanel);
labell1.setBounds(443, 288, 44, 23);
labell2.setBounds(443 + someXDisplacement, 288, 44, 23);
现在如果你运行它,你将得到中间的两个组件,在这里使用<强> StimeSimulink(…)/强>方法定义它们。

就像几乎每个人都在评论你的问题一样,你不应该使用空布局,而应该使用其他布局。我将使用GridBagLayout来组织图表中的三个按钮和三个文本字段。然后,您可以在子面板上设置背景(…)

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

如果您的第一个标签如下所示:

getContentPane().add(childPanel);
labell1.setBounds(443, 288, 44, 23);
labell2.setBounds(443 + someXDisplacement, 288, 44, 23);
那么您的第二个标签应该是这样的:

getContentPane().add(childPanel);
labell1.setBounds(443, 288, 44, 23);
labell2.setBounds(443 + someXDisplacement, 288, 44, 23);
…第三:

labell3.setBounds(443 + (someXDisplacement x 2), 288, 44, 23);
你明白了。

我要嵌套布局

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);
    }
}

import java.awt.*;
导入javax.swing.*;
导入javax.swing.border.EmptyBorder;
导入javax.swing.border.TitledBorder;
公共类三按钮ExtFieldCombo{
私有JPanel ui=null;
ThreeButtonTextFieldCombo(){
initUI();
}
公共最终无效初始值(){
如果(ui!=null)返回;
ui=新的JPanel(新的GridBagLayout());
ui.订单(新标题边框(“母面板”);
JPanel控件=新的JPanel(新的GridLayout(1,0,10,10));
ui.add(控件);
控件。背景(颜色。红色);
控件。设置顺序(新标题边框(“子面板”);

对于(int ii=1;i不要使用null
LayoutManager
。使用绝对布局,我们会发现您的突发错误,BorderLayout或GrudBagLayout将免费执行此操作。这正是(众多布局之一)我们有布局管理器来处理这些繁琐、繁琐、重复的任务的原因…哦..如果
txtrmagedata=new-JTextArea(“文本区域文本”);
是类似于
txtrmagedata=new-JTextArea(“文本区域文本”,3,20)的东西,这将有助于布局
Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作。因此,它们不利于像素完美的布局。相反,使用布局管理器,或者与布局填充和边框一起使用。此外,您还错误地使用了
GridBagLayout
。查看它,类javadoc有一个使用示例。“如果你真的必须使用绝对布局…”我真的需要绝对布局,当地狱结束的时候。很高兴被否决回答OP想要的内容。明确建议他们不要使用绝对布局,但如果他们确实需要使用它,就给他们一个建议。有时这是一个笑话。“很高兴被否决…”。。“不要看我的否决票。但是…”为了准确回答OP想要的内容。“…我觉得在这种情况下这是一个错误。好吧..按钮/标签,标签/按钮有点混淆,但将标签换成按钮,效果也一样。