jtextarea 不会用 GridBagConstraints 填充面板

jtextarea wont fill panel with GridBagConstraints

根据我的代码,我希望我的 JTextArea 填充左上边框,如下所示:

但是你可以看到它在中间占据了一小部分。 我在包含组件的面板上使用 GridBagConstraints。

有一个主 class 调用了一个 class 框架。此 class 创建 JFrame 并设置大小以及其他内容。然后从扩展了 jframe 的 MainFrame.java 调用它,它创建了 3 个面板并设置了它们的布局。如下所示

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

public class MainFrame extends JFrame
{
    private Panel1 storyPanel;
    private Panel2 statsPanel;
    private Panel3 commandsPanel;

    public MainFrame(String title)
    {
        super(title);

        // Setting Layout
        GridBagConstraints gbc = new GridBagConstraints();

        storyPanel = new Panel1();
        storyPanel.setLayout(new GridBagLayout());
        statsPanel = new Panel2();
        commandsPanel = new Panel3();

        Container p = getContentPane();

        p.add(storyPanel, BorderLayout.WEST);
        p.add(statsPanel, BorderLayout.EAST);
        p.add(commandsPanel, BorderLayout.SOUTH);

    }
}

问题中的面板是 Panel1 或 storyPanel。我已经设置了布局,代码调用了 Panel1.java,如下所示:

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

public class Panel1 extends JPanel
{
    public Panel1()
    {
        GridBagConstraints gbc = new GridBagConstraints();

        //Set size of Panel1
        int xsizeP1 = (Frame.xsize() / 2);
        int ysizeP1 = (Frame.ysize() / 3 * 2);
        setPreferredSize(new Dimension(xsizeP1, ysizeP1));
        setBorder(BorderFactory.createLineBorder(Color.black));

        //Adding JTextArea and adding settings to it
        JTextArea storyLine = new JTextArea("       test        ");
        storyLine.setLineWrap(true);
        storyLine.setWrapStyleWord(true);
        storyLine.setEditable(false);


        //Adding JScrollPane to the JTextArea and making it have a vertical scrollbar
        JScrollPane scroll = new JScrollPane(storyLine);
        scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        //GridBagConstraints setup for components
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.fill = GridBagConstraints.VERTICAL;
        add(scroll, gbc);
    }
}

我不明白为什么我的 gbc.fill 没有让 JTextArea 填充屏幕截图的左上边框。

提前感谢您的任何回复

-汤姆 T


将布局更改为边框布局

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

public class Panel1 extends JPanel
{
    public Panel1()
    {
        //GridBagConstraints gbc = new GridBagConstraints();
        //gbc.weightx = 0.1;
        //gbc.weighty = 0.1;
        BorderLayout b = new BorderLayout();

        //Set size of Panel1
        int xsizeP1 = (Frame.xsize() / 2);
        int ysizeP1 = (Frame.ysize() / 3 * 2);
        setPreferredSize(new Dimension(xsizeP1, ysizeP1));
        setBorder(BorderFactory.createLineBorder(Color.black));

        //Adding JTextArea and adding settings to it
        JTextArea storyLine = new JTextArea("       test        ");
        storyLine.setLineWrap(true);
        storyLine.setWrapStyleWord(true);
        storyLine.setEditable(false);


        //Adding JScrollPane to the JTextArea and making it have a vertical scrollbar
        JScrollPane scroll = new JScrollPane(storyLine);
        scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

    //gbc.gridx = 0;
    //gbc.gridy = 0;
        //gbc.weightx = 1;
        //gbc.weighty = 1;
        //gbc.fill = GridBagConstraints.BOTH;
        add(scroll, b.CENTER);
    }
}
  1. 不要设置 storyPanel 的布局,因为它的内容已经被添加和布局,所以在这里更改布局管理器将丢弃您应用的任何属性。相反,在添加任何组件之前在 Panel1 的构造函数中设置布局。
  2. fill 属性 使用 GridBagConstraints.BOTHfill 属性 只能有一个指定值
  3. 使用 weightxweighty 指定组件应使用多少可用 space

例如...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class MainFrame extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new MainFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    private Panel1 storyPanel;
//  private Panel2 statsPanel;
//  private Panel3 commandsPanel;

    public MainFrame(String title) {
        super(title);

        storyPanel = new Panel1();

        Container p = getContentPane();

        p.add(storyPanel, BorderLayout.WEST);
        p.add(new JLabel("East"), BorderLayout.EAST);
        p.add(new JLabel("South"), BorderLayout.SOUTH);

    }

    public class Panel1 extends JPanel {

        public Panel1() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();

            //Set size of Panel1
            setBorder(BorderFactory.createLineBorder(Color.black));

            //Adding JTextArea and adding settings to it
            JTextArea storyLine = new JTextArea(20, 20);
            storyLine.setLineWrap(true);
            storyLine.setWrapStyleWord(true);
            storyLine.setEditable(false);

            //Adding JScrollPane to the JTextArea and making it have a vertical scrollbar
            JScrollPane scroll = new JScrollPane(storyLine);
            scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
            scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

            //GridBagConstraints setup for components
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.weightx = 1;
            gbc.weighty = 1;
            add(scroll, gbc);
        }
    }
}

话虽如此,BorderLayout 会更简单

public class Panel1 extends JPanel {

    public Panel1() {
        setLayout(new BorderLayout());

        //Set size of Panel1
        setBorder(BorderFactory.createLineBorder(Color.black));

        //Adding JTextArea and adding settings to it
        JTextArea storyLine = new JTextArea(20, 20);
        storyLine.setLineWrap(true);
        storyLine.setWrapStyleWord(true);
        storyLine.setEditable(false);

        //Adding JScrollPane to the JTextArea and making it have a vertical scrollbar
        JScrollPane scroll = new JScrollPane(storyLine);
        scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        add(scroll);
    }
}

i tried a border layout and set it to center expecting it to resize but that failed as well

建议您在某个地方犯了一个根本性错误,因为它对我来说很好用。请记住,在将任何组件添加到容器之前设置布局