jtextarea不会使用GridBagConstraints填充面板

时间:2015-11-02 21:07:32

标签: java swing layout-manager jtextarea gridbaglayout

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

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

有一个主类可以调用一个叫做frame的类。此类创建JFrame并设置大小以及其他内容。然后从MainFrame.java调用它,它扩展了jframe,创建了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填满屏幕截图的左上角。

提前感谢任何回复

-Tom 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 个答案:

答案 0 :(得分:3)

  1. 不要设置storyPanel的布局,因为它的内容已经添加并布局,因此在此处更改布局管理器将丢弃您应用的所有属性。而是在添加任何组件之前在Panel1的构造函数中设置布局。
  2. GridBagConstraints.BOTH用于fill媒体资源。 fill属性只能有一个指定值
  3. 使用weightxweighty指定组件应使用多少可用空间
  4. 例如......

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

    我尝试了边框布局并将其设置为中心,期望它调整大小但是也失败了

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