Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.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 GridBagLayout组件是否忽略宽度?_Java_Swing_Layout Manager_Gridbaglayout - Fatal编程技术网

Java GridBagLayout组件是否忽略宽度?

Java GridBagLayout组件是否忽略宽度?,java,swing,layout-manager,gridbaglayout,Java,Swing,Layout Manager,Gridbaglayout,对不起,如果以前有人问过这个问题,我已经花了很长时间搜索,但没有成功 我有一个面板,我试图使用GridBagLayout添加一些组件 问题是,出于某种原因,我无法理解-我试图添加的JTextArea似乎忽略了分配给它的宽度-它似乎接受高度没有问题 除了文本区域外,所有组件都添加得很好,文本区域的宽度应为3,但显示的宽度为1 我不明白这是为什么,有人能帮忙吗 提前感谢您的帮助 代码:(编辑) 公共类主{ 公共静态void main(字符串[]args){ 内容主机=新内容(); master.cr

对不起,如果以前有人问过这个问题,我已经花了很长时间搜索,但没有成功

我有一个面板,我试图使用GridBagLayout添加一些组件

问题是,出于某种原因,我无法理解-我试图添加的JTextArea似乎忽略了分配给它的宽度-它似乎接受高度没有问题

除了文本区域外,所有组件都添加得很好,文本区域的宽度应为3,但显示的宽度为1

我不明白这是为什么,有人能帮忙吗

提前感谢您的帮助

代码:(编辑)

公共类主{
公共静态void main(字符串[]args){
内容主机=新内容();
master.createAndShowGui();
}
}
包装试验;
//进口报关单
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.Insets;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.ArrayList;
导入java.util.List;
导入javax.swing.JButton;
导入javax.swing.JComponent;
导入javax.swing.JDialog;
导入javax.swing.JLabel;
导入javax.swing.JOptionPane;
导入javax.swing.JPanel;
导入javax.swing.JTextArea;
导入javax.swing.JTextField;
导入javax.swing.SwingConstants;
公开课内容
{   
//声明类变量
private JDialog schoolFrame=new JDialog();
private JPanel schoolPanel=new JPanel(new GridBagLayout());
私有JTextArea text=新JTextArea();
私有JButton exit=新JButton(“退出”);
私有JButton portView=新JButton(“视图组合”);
private JButton payView=新JButton(“查看付款信息”);
私有GridBagConstraints c=新的GridBagConstraints();
私人JLabel payAmount=新JLabel(“”,SwingConstants.CENTER);
私人JButton子付款=新JButton(“提交付款”);
私有JLabel子标签[]=新JLabel[5];
私有JTextField子字段[]=新JTextField[5];
私有JButton subGradeChange=新JButton(“提交更改”);
JLabel测试仪=新的JLabel();
JLabel tester2=新的JLabel(“jjjjj”);
公共内容(){
}
私有void setOptions(JComponent b、int-weightx、int-weighty、int-x、int-y、int-width、int-height)
{
c、 fill=GridBagConstraints.BOTH;
c、 权重x=权重x;
c、 重=重;
c、 gridy=y;
c、 gridx=x;
c、 网格宽度=宽度;
c、 网格高度=高度;
添加(b,c);
}
//方法来创建和显示GUI
公共JDialog createAndShowGui()
{   
c、 fill=GridBagConstraints.BOTH;
c、 插图=新插图(0,0,0,0);
schoolFrame.setDefaultCloseOperation(JDialog.DISPOSE\u ON\u CLOSE);
schoolFrame.setContentPane(学校面板);
校框。设置最小尺寸(新尺寸(600600));
校框。设置最大尺寸(新尺寸(600600));
text.setEditable(false);
schoolFrame.setModal(true);
学校框架。设置标题(“简单学校系统:校长”);
设置选项(测试仪,1,1,2,0,1,1);
设置选项(portView,1,1,0,0,2,1);
设置选项(文本,1,1,2,0,3,7);

对于(int i=0;i要计算3列的宽度,第2列和第3列中应该有一些内容


因此,实际上您将所有组件添加到一列中。LayoutManager无法计算第2列和第3列的宽度,因为这两列中没有组件。因此,它们的宽度为0,并且
您的textarea的宽度=第一列宽度+0+0

要更快地获得更好的帮助,请发布一个(最小完整的可验证示例)。抱歉-我已经更新了代码。不确定您希望它看起来是什么样子。按钮应该在文本区域的上方和下方?此时文本区域就在左侧。奥利弗,这就是您运行代码得到的结果吗?当我运行代码时-上面的确切代码,我得到3列,左上方的按钮跨越2列,然后是标签这个大按钮下面的s/textfields/buttons分为两列。实际上是两列,顶部的按钮占据了这两列。文本区域应该占据第3列的全部内容-其宽度应该大于前两列的宽度,但显示的宽度约为1列。希望这是有意义的。说得清楚一点,我只是想知道这里最重要的是如何确保JTextArea占据更多的面板比例空间-按钮~30%宽度,文本~70%宽度。似乎没有什么可以解决这个问题,设置(首选)大小()的任何变化我试过了,但什么也没用。我只希望面板有5列-文本区域占3,4和5,但现在似乎不可能。感谢您的回复-portView组件位于0,0,但跨越2列,因此占据第2列?然后在循环中创建的按钮占据0,y和1,y因此,所有这些组件都应该占据前两列?然后将textarea添加到第三列。如果我不理解您所说的话,很抱歉。请尝试在第2列和第3列中添加例如JLabel(宽度=1)看到结果了吗?恐怕这似乎没有什么帮助-向第2列添加标签似乎不会影响任何事情,但当我向第3列添加标签(添加随机文本)时,它只会略微增加列的大小(我假设是基于标签中字符串的长度)。
public class Main {


public static void main(String[] args) {

    Content master = new Content();
    master.createAndShowGui();
}

}
package test;

//import declarations

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class Content

{   
//declare class variables

private JDialog schoolFrame = new JDialog();
private JPanel schoolPanel = new JPanel(new GridBagLayout());
private JTextArea text = new JTextArea();
private JButton exit = new JButton("Quit");
private JButton portView = new JButton("View Portfolio");
private JButton payView = new JButton("View Payment Info");
private GridBagConstraints c = new GridBagConstraints();
private JLabel payAmount = new JLabel("",SwingConstants.CENTER);
private JButton subPayment = new JButton("Submit Payment");
private JLabel subLabels[] = new JLabel[5];
private JTextField subFields[] = new JTextField[5];
private JButton subGradeChange = new JButton("Submit Changes");
JLabel tester = new JLabel();
JLabel tester2 = new JLabel("jjjjjjj");


public Content(){

}

private void setOptions(JComponent b,int weightx,int weighty,int x,int y,int width,int height)
{
    c.fill = GridBagConstraints.BOTH;    
    c.weightx = weightx;
    c.weighty = weighty;
    c.gridy = y;
    c.gridx = x;
    c.gridwidth = width;
    c.gridheight = height;
    schoolPanel.add(b, c);
}

//method to create and display the GUI

public JDialog createAndShowGui()
{   
    c.fill = GridBagConstraints.BOTH;
    c.insets = new Insets(0,0,0,0);
    schoolFrame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    schoolFrame.setContentPane(schoolPanel);
    schoolFrame.setMinimumSize(new Dimension(600,600)); 
    schoolFrame.setMaximumSize(new Dimension(600,600));
    text.setEditable(false);
    schoolFrame.setModal(true);
    schoolFrame.setTitle("Simple School System: House Head");
    setOptions(tester, 1, 1, 2, 0, 1, 1);
    setOptions(portView,1,1,0,0,2,1);
    setOptions(text,1,1,2,0,3,7);
    for(int i=0;i<subLabels.length;i++){
        subLabels[i] = new JLabel();
        subFields[i] = new JTextField();
        subLabels[i].setText("Subject: ");
        subFields[i].setText("Enter Grade");
        setOptions(subLabels[i],1,1,0,i+1,1,1);
        setOptions(subFields[i],1,1,1,i+1,1,1);
    }
    setOptions(subGradeChange,1,1,1,6,1,1);
    setOptions(exit,1,1,0,6,1,1);
    exit.addActionListener
  (
    new ActionListener()
    {
        @Override

    public void actionPerformed(ActionEvent e)
    {
            int quit = JOptionPane.showConfirmDialog(null,"Exit","Are you sure",JOptionPane.YES_NO_OPTION);
            if(quit==0)
            {
                System.exit(0);
            }
            else
            {
                schoolFrame.dispose();
                hideUpdate();
            }
    }
  });

    //pack components before displaying frame

    schoolFrame.pack();
    text.setPreferredSize(text.getPreferredSize());
    schoolFrame.pack();
    schoolFrame.setLocationRelativeTo(null);
    schoolFrame.setVisible(true);
    return schoolFrame;
}



//method to update the contents of the frame

private void hideUpdate()
{
    schoolFrame.setVisible(false);      
    schoolFrame.pack();
    schoolFrame.repaint();
    schoolFrame.pack();
    schoolFrame.setVisible(true);       
}
}