【问题标题】:jtextarea wont fill panel with GridBagConstraintsjtextarea 不会用 GridBagConstraints 填充面板
【发布时间】:2015-11-02 21:07:31
【问题描述】:

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

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

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

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

【问题讨论】:

  • 不要担心设置 xsize 和 ysize,thoes 的代码在 Frame.java 中,问题不需要。如果需要任何其他信息,请询问。
  • 不要再设置storyPanel的布局。使用 GridBagConstaints.BOTH 作为填充约束,并使用 weightx 和 weighty 指定组件应占用的剩余空间量。根据您的代码,改用 BorderLayout 会更简单
  • 我尝试了边框布局并将其设置为中心,希望它可以调整大小,但也失败了,不过我会尝试您的建议,谢谢
  • 我删除了 Mainframe 面板上的 Gridbagconstraints 并将填充设置为 both 和 weighty,但除了将组件移动到左侧面板的顶部之外,它没有改变任何东西。

标签: java swing layout-manager jtextarea gridbaglayout


【解决方案1】:
  1. 不要设置storyPanel的布局,因为它的内容已经被添加和布局,所以在这里改变布局管理器会丢弃你应用的任何属性。相反,在添加任何组件之前,请在 Panel1 的构造函数中设置布局。
  2. GridBagConstraints.BOTH 用于fill 属性。 fill 属性只能有一个指定值
  3. 使用weightxweighty 指定组件应该使用多少可用空间

例如...

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

我尝试了一个边框布局并将其设置为中心,希望它可以调整大小,但也失败了

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

【讨论】:

  • 非常感谢,它运行良好,您的回答结构合理且很有帮助,因为它帮助我理解并解决了问题。我也更改了边框布局,问题是我忘记设置布局,只使用“BorderLayout b = new BorderLayout();”感谢所有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-24
  • 2022-01-20
  • 1970-01-01
相关资源
最近更新 更多