简体   繁体   English

Textarea在不可编辑且换行符合时无法正常工作

[英]Textarea not working properly when not editable and line wrap is true

消息传递GUI

I have this issue with my program that I can't seem to figure out why its happening. 我的程序有这个问题,我似乎无法弄清楚它为什么会发生。 What should happen is that when you enter in something in the input area below, it should take that and put it where the black line is which is actually a textarea box. 应该发生的是,当你输入下面输入区域中的某个东西时,它应该采取并将它放在黑线实际上是textarea框的位置。 Normally it works except that when I have both editable set to false and line wrap set the true this happens and the size should stretch across the whole panel up to the image. 通常它的工作原理除外,当我将editable设置为false和换行设置时,会发生这种情况,并且大小应该在整个面板上延伸到图像。 I've put down below the relevant code. 我已经在相关代码下面了。 I have been racked my brain for hours and need a new perspective. 我已经绞尽脑汁几个小时,需要一个新的视角。

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

Instead of using dateConst.ipadx = 70; 而不是使用dateConst.ipadx = 70; which may effecting GridBagLayout 's ability to honor the preferred size of the text area, try using messageConst.weightx = 1; 这可能影响GridBagLayout能够尊重文本区域的首选大小,尝试使用messageConst.weightx = 1; instead. 代替。

The issue could that GridBagLayout has looked at the available space for the text area and decided there isn't enough space to honor it's preferred size and resorts to using it's minimum size instead (which is typically not very large) 问题可能是GridBagLayout查看了文本区域的可用空间,并确定没有足够的空间来支持它的首选大小,并决定使用它的最小大小(通常不是很大)

Updated 更新

So I had a quick play around with the code and came up with this... 所以我快速了解了代码并提出了这个......

在此输入图像描述

Things to remember, insest add weight to a given cell, making it bigger, which will push other cells around, not always in a good way. 要记住的事情, insest增加给定细胞的重量,使其变大,这将推动其他细胞周围,而不是总是以一种好的方式。

EAST is to the right, WEST is to the left ;) EAST在右边, WEST在左边;)

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM