3

そのため、GUI に GridBagLayout を使用していますが、サイズが適切に変更されないという問題が発生しています。誰かが別のレイアウトを提案する前に、私は GridBagLayout を使用したいと確信しています。多数のコンポーネントがあり、それらを適切にレイアウトしたいのですが、現在のコード/画像には、現在持っているものだけが表示されます。

とにかく、問題はそれ自体が適切にサイズ変更されないことです。サイズ変更時に現在のアスペクト比を維持したいのですが、そうではありません。特定のパネルには、サイズ変更時に何らかの優先順位が与えられているため、サイズ変更すると縦横比が乱れます。基本的に、いくつかのパネルが別のパネルよりも大きくなっていますが、サイズを変更すると、次のように別のパネルよりも小さくなります。

http://i.imgur.com/LWhHm.png

さて、私が実際に望んでいるのは、彼らがすでに示した比率を維持することです。ご覧のとおり、元のように、GUI のチャット部分を左側のパネルのいずれよりも小さくしたいと考えています。

以下は、これを生成するために現在持っているコードです。

jpPack = new JPanel();
    jpCards = new JPanel();
    jpInfo = new JPanel();
    jpChat = new JPanel();


    jpPack.setBackground(Color.red);
    jpCards.setBackground(Color.blue);
    jpInfo.setBackground(Color.green);

    //create the Constraints and set some that will apply to each panel
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.BOTH;

    //set the number of columns/rows to use
    c.gridwidth = 1;
    c.gridheight = 1;
    //set the x and y position
    c.gridx = 0;
    c.gridy = 0;
    //set the weight properties
    c.weightx = 1.0;
    c.weighty = 1.0;

    getContentPane().add(jpCards, c);

    //set the number of columns/rows to use
    c.gridwidth = 1;
    c.gridheight = 1;
    //set the x and y position
    c.gridx = 0;
    c.gridy = 1;
    //set the weight properties
    c.weightx = 1.0;
    c.weighty = 1.0;

    getContentPane().add(jpPack, c);

    //set the number of columns/rows to use
    c.gridwidth = 1;
    c.gridheight = 1;
    //set the x and y position
    c.gridx = 1;
    c.gridy = 0;
    //set the weight properties
    c.weightx = 0.2;
    c.weighty = 0.2;

    getContentPane().add(jpInfo, c);

    //set the number of columns/rows to use
    c.gridwidth = 1;
    c.gridheight = 1;
    //set the x and y position
    c.gridx = 1;
    c.gridy = 1;
    //set the weight properties
    c.weightx = 0.2;
    c.weighty = 0.2;

    getContentPane().add(jpChat, c);
    jpChat.setLayout(new GridLayout());
    jpChat.add(client.gui.getContentPane(), c);

    setVisible(true);

私がそれに取り組んでいる間、私は私が持っている他の質問をするかもしれません. GUI 全体とチャット パネルの両方に、最小サイズの制約を設定したいと考えています。まず、ユーザーが特定の x と特定の y よりも小さいサイズに変更できないようにしたい。後者の場合は、希望するアスペクト比を維持し、右側の 2 つのパネルの最小値に達した後、左側のパネルだけのサイズを変更します。つまり、あるサイズになるまで各パネルのサイズを変更してから、GUI 全体のサイズ変更を続けますが、左側のパネルのサイズを変更し、右側のパネルは最小サイズのままにします。

私を助けてくれてありがとう。これは本当に私を悩ませています.GridBagLayoutConstraintsをいじり、どこにも行きません. これは私が得た最高のものです。

4

1 に答える 1

3

私はあなたの質問に少し取り組んでみましたが、あなたに懸念を引き起こしているかもしれない簡単な理由がありました. で使用しているJTextFieldChatPanelは、同じものを初期化するときに、その列を指定する必要があり、そのような影響を引き起こす可能性があります。上記のコードを作成した方法以来、このことは、何らかの意味で、私に奇妙な動作を与えていません。2 番目の質問に関しては、@kleopatra がいくつかの洞察を与えてくれました。使用したコードは次のとおりです。

import java.awt.*;
import javax.swing.*;

public class ClientGrid
{
    private void displayGUI()
    {
        JFrame frame = new JFrame("Client Grid");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new GridBagLayout());

        JPanel bluePanel = new JPanel();
        bluePanel.setOpaque(true);
        bluePanel.setBackground(Color.BLUE);

        JPanel greenPanel = new JPanel();
        greenPanel.setOpaque(true);
        greenPanel.setBackground(Color.GREEN);

        JPanel redPanel = new JPanel();
        redPanel.setOpaque(true);
        redPanel.setBackground(Color.RED);

        JPanel chatPanel = new JPanel();
        JTextField chatField = new JTextField();
        chatPanel.setLayout(new BorderLayout(5, 5));
        chatPanel.add(chatField, BorderLayout.PAGE_START);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0.7;
        gbc.weighty = 0.3;

        contentPane.add(bluePanel, gbc);

        gbc.gridx = 1;
        gbc.weightx = 0.3;

        contentPane.add(greenPanel, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 0.7;
        gbc.weighty = 0.7;

        contentPane.add(redPanel, gbc);

        gbc.gridx = 1;
        gbc.weightx = 0.3;

        contentPane.add(chatPanel, gbc);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new ClientGrid().displayGUI();
            }
        });
    }
}
于 2012-10-06T12:28:26.253 に答える