为什么这个GridBagLayout代码不起作用?

时间:2014-04-10 00:41:18

标签: java swing layout gridbaglayout

我有以下代码。

类似的代码有效。朋友也说它应该工作。但它没有。

任何人都有线索?

谢谢!

问题描述:

而不是绘制所需的布局,一切都很小......(

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class ChatGUI extends JPanel {

    private static final long serialVersionUID = -3069354983197916315L;
    private Image imgBackground;

    private GridBagLayout gblChat;
    private GridBagConstraints constraintsChat;

    private JPanel jPanelChat;
    private JList clientList;

    private JColorChooser font_chooser;

    private JButton privateMessage_button;
    private JButton sendMessage_button;

    private JScrollPane OutputOfChatMessages;
    private JScrollPane ShowConnectedClients;
    private JScrollPane InputOfChatMessage;

    public static void main(String[] args) {

        JFrame jf = new JFrame();

        ChatGUI cg1 = new ChatGUI(500, 500);

        jf.add(cg1);

        jf.setExtendedState(Frame.MAXIMIZED_BOTH);
        jf.setVisible(true);
    }

    /**
     * Layout
     */
    public ChatGUI(int width, int height) {

        // used for scaling
        this.setMinimumSize(new Dimension(width / 2,
                height / 4));
        this.setPreferredSize(new Dimension(width / 2,
                height / 4));

        jPanelChat = new JPanel();
        gblChat = new GridBagLayout();
        constraintsChat = new GridBagConstraints();

        this.setLayout(gblChat);
        this.setBackground(Color.LIGHT_GRAY);
        constraintsChat.fill = GridBagConstraints.BOTH;

        /**
         * Panel 1 = OutputOfChatMessages
         */
        OutputOfChatMessages = new JScrollPane();

        constraintsChat.gridx = 0;
        constraintsChat.gridy = 0;
        constraintsChat.gridwidth = 1;
        constraintsChat.gridheight = 1;
        gblChat.setConstraints(OutputOfChatMessages, constraintsChat);

        this.add(OutputOfChatMessages);

        /**
         * Panel 2 = ShowConnectedClients
         */
        ShowConnectedClients = new JScrollPane();

        constraintsChat.gridx = 1;
        constraintsChat.gridy = 0;
        constraintsChat.gridwidth = 4;
        constraintsChat.gridheight = 4;
        gblChat.setConstraints(ShowConnectedClients, constraintsChat);

        this.add(ShowConnectedClients);

        /**
         * Panel 3 = InputOfChatMessage
         */
        InputOfChatMessage = new JScrollPane();

        constraintsChat.gridx = 0;
        constraintsChat.gridy = 5;
        constraintsChat.gridwidth = 2;
        constraintsChat.gridheight = 2;
        gblChat.setConstraints(InputOfChatMessage, constraintsChat);

        this.add(ShowConnectedClients);

        /**
         * Panel 4 = privateMessage_button
         */
        privateMessage_button = new JButton();

        constraintsChat.gridx = 1;
        constraintsChat.gridy = 6;
        constraintsChat.gridwidth = 1;
        constraintsChat.gridheight = 1;
        gblChat.setConstraints(privateMessage_button, constraintsChat);

        this.add(privateMessage_button);

        /**
         * Panel 5 = sendMessage_button
         */
        sendMessage_button = new JButton();

        constraintsChat.gridx = 1;
        constraintsChat.gridy = 7;
        constraintsChat.gridwidth = 1;
        constraintsChat.gridheight = 1;
        gblChat.setConstraints(sendMessage_button, constraintsChat);

        this.add(sendMessage_button);

        this.validate();
//this.revalidate();

    }
}

1 个答案:

答案 0 :(得分:2)

  1. 您没有为滚动窗格设置任何视图,因此无法确定其首选大小。
  2. 您在按钮上没有设置任何文字,因此它只根据边框要求计算按钮的大小
  3. 不要混淆setXxxSize,您的ChatGUI应该没有可能需要占用的可见尺寸的概念。