Java Textarea在不可编辑且换行为真时工作不正常

Java Textarea在不可编辑且换行为真时工作不正常,java,swing,jtextarea,gridbaglayout,preferredsize,Java,Swing,Jtextarea,Gridbaglayout,Preferredsize,我的程序有这样一个问题,我似乎不明白为什么会这样。应该发生的是,当你在下面的输入区域输入一些东西时,它应该把它放在黑线的位置,这实际上是一个文本框。正常情况下,它可以工作,但当我将“可编辑”设置为false,将“换行”设置为true时,就会发生这种情况,并且大小应该延伸到整个面板直到图像。我已经在下面写下了相关代码。我已经绞尽脑汁好几个小时了,需要一个新的视角 private JTextArea message = new JTextArea(5,20); private JLabel dat

我的程序有这样一个问题,我似乎不明白为什么会这样。应该发生的是,当你在下面的输入区域输入一些东西时,它应该把它放在黑线的位置,这实际上是一个文本框。正常情况下,它可以工作,但当我将“可编辑”设置为false,将“换行”设置为true时,就会发生这种情况,并且大小应该延伸到整个面板直到图像。我已经在下面写下了相关代码。我已经绞尽脑汁好几个小时了,需要一个新的视角

private JTextArea message  = new JTextArea(5,20);
private JLabel date = new JLabel();
private ImageIcon img = new ImageIcon(getClass().getResource("/assignment1/img/silhouette.png"));   
private JLabel ImageLabel = new JLabel();



public MessagePanel(String pmessage, Date timestamp) {
    this.setLayout(new GridBagLayout());
    this.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0)));
    this.setPreferredSize(new Dimension(550,150));

    ImageLabel.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0)));
    message.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0)));
    ImageLabel.setIcon(img);

    message.setEditable(false);
    message.append(pmessage);
    message.setLineWrap(true);
    message.setWrapStyleWord(true);
    message.setCaretPosition(message.getDocument().getLength());
    //message.setText(pmessage);
    message.setPreferredSize(new Dimension(400,100));


    SimpleDateFormat f = new SimpleDateFormat("dd/MM/yyyy hh:mm a");
    date.setText(f.format(timestamp));

    GridBagConstraints messageConst = new GridBagConstraints();
    messageConst.gridx = 0;
    messageConst.gridy = 0;
    messageConst.fill = GridBagConstraints.HORIZONTAL;


    //messageConst.anchor = java.awt.GridBagConstraints.NORTHWEST;
    messageConst.insets = new Insets(12, 83, 0, 0);

    GridBagConstraints iconConst = new GridBagConstraints();
    iconConst.gridx = 1;
    iconConst.gridy = 0;
    iconConst.anchor = java.awt.GridBagConstraints.NORTHWEST;
    iconConst.insets = new Insets(49, 425, 0, 11);

    GridBagConstraints dateConst = new GridBagConstraints();
    dateConst.gridx = 0;
    dateConst.gridy = 1;
    dateConst.gridwidth = 2;
    dateConst.ipadx = 70;
    dateConst.anchor = GridBagConstraints.NORTHWEST;
    dateConst.insets = new Insets(6, 460,0, 11);

    this.add(message,messageConst);
    this.add(date,dateConst);
    this.add(ImageLabel,iconConst);
}

而不是使用
dateConst.ipadx=70
可能会影响
GridBagLayout
遵守文本区域首选大小的能力,请尝试使用
messageConst.weightx=1取而代之

问题可能是,
GridBagLayout
已经查看了文本区域的可用空间,并决定没有足够的空间来满足其首选大小,而是使用其最小大小(通常不是很大)

已更新

所以我对代码进行了快速的修改,并提出了这个

要记住的是,
insest
给给定的单元格增加权重,使其变大,这会推动其他单元格,但并不总是以一种好的方式

在右边,
西
在左边;)


而不是使用
dateConst.ipadx=70
可能会影响
GridBagLayout
遵守文本区域首选大小的能力,请尝试使用
messageConst.weightx=1取而代之

问题可能是,
GridBagLayout
已经查看了文本区域的可用空间,并决定没有足够的空间来满足其首选大小,而是使用其最小大小(通常不是很大)

已更新

所以我对代码进行了快速的修改,并提出了这个

要记住的是,
insest
给给定的单元格增加权重,使其变大,这会推动其他单元格,但并不总是以一种好的方式

在右边,
西
在左边;)


尝试了一下,它确实解决了我遇到的另一个问题,但是文本区域仍然是最重要的same@AlexMurray我也会放弃插图(首先)啊,现在它工作了,我应该在插图上使用什么来代替它,这样我就可以定位东西了correctly@AlexMurray用户正确的锚定位置。看看更新后的代码,我在玩代码,看看是否还有其他可能导致问题的东西;)“取而代之在插图上”使用布局填充和边框。尝试了一下,这确实解决了我遇到的另一个问题,但文本区域仍然是唯一的same@AlexMurray我也会放弃插图(首先)啊,现在它工作了,我应该在插图上使用什么来定位东西correctly@AlexMurray用户正确的锚定位置。看看更新后的代码,我在玩代码,看看是否还有其他可能导致问题的东西;)“而是在插图上”使用布局填充和边框。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class TestTextArea100 {

    public static void main(String[] args) {
        new TestTextArea100();
    }

    public TestTextArea100() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JTextArea message = new JTextArea(5, 20);
        private JLabel date = new JLabel();
        private JLabel ImageLabel = new JLabel();

        public TestPane() {
            this.setLayout(new GridBagLayout());
            this.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0)));
            this.setPreferredSize(new Dimension(550, 150));

            ImageLabel.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0)));
            message.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0)));
            ImageLabel.setText(":)");
            ImageLabel.setBorder(new LineBorder(Color.RED));

            message.setEditable(false);
            message.append("Blah");
            message.setLineWrap(true);
            message.setWrapStyleWord(true);
            message.setCaretPosition(message.getDocument().getLength());
            //message.setText(pmessage);
            message.setPreferredSize(new Dimension(400, 100));

            SimpleDateFormat f = new SimpleDateFormat("dd/MM/yyyy hh:mm a");
            date.setText(f.format(new Date()));

            GridBagConstraints messageConst = new GridBagConstraints();
            messageConst.gridx = 0;
            messageConst.gridy = 0;
            messageConst.weightx = 1;
            messageConst.weighty = 1;
            messageConst.fill = GridBagConstraints.BOTH;

            messageConst.insets = new Insets(12, 12, 12, 12);

            GridBagConstraints iconConst = new GridBagConstraints();
            iconConst.gridx = 1;
            iconConst.gridy = 0;
            iconConst.insets = new Insets(12, 12, 12, 12);

            GridBagConstraints dateConst = new GridBagConstraints();
            dateConst.gridx = 0;
            dateConst.gridy = 1;
            dateConst.gridwidth = 2;
            dateConst.anchor = GridBagConstraints.EAST;

            this.add(message, messageConst);
            this.add(date, dateConst);
            this.add(ImageLabel, iconConst);
        }
    }

}