GridBagLayout组件忽略宽度?

时间:2014-11-10 12:44:18

标签: java swing layout-manager gridbaglayout

对不起,如果以前曾经问过这个问题,但是我花了很多时间没有成功。

我有一个面板,我正在尝试使用GridBagLayout添加一些组件。

问题是由于某些原因我无法弄清楚 - 我试图添加的JTextArea似乎忽略了分配给它的宽度 - 它似乎接受高度没有问题。

除文本区域外,所有组件都添加正常 - 文本区域的宽度应为3,但显示宽度为1。

我无法理解为什么会这样,有人可以帮忙吗?

提前感谢您的帮助。

代码:(编辑)

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

1 个答案:

答案 0 :(得分:2)

要计算3列宽度,第2列和第3列应该有一些东西。

因此实际上您将所有组件添加到一列中。 LayoutManager无法确定第2列和第3列的宽​​度,因为列中没有组件。所以他们的宽度是0和your textarea's width =1st column widht + 0 + 0