【问题标题】:Textarea not working properly when not editable and line wrap is true当不可编辑且换行为真时,Textarea 无法正常工作
【发布时间】:2013-10-18 06:34:29
【问题描述】:

我的程序有这个问题,我似乎无法弄清楚它为什么会发生。应该发生的是,当您在下面的输入区域中输入某些内容时,它应该将其放在黑线所在的位置,该黑线实际上是一个文本区域框。通常它可以工作,除了当我将可编辑设置为 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);
}

【问题讨论】:

    标签: java swing jtextarea gridbaglayout preferredsize


    【解决方案1】:

    不要使用dateConst.ipadx = 70;,这可能会影响GridBagLayout的能力,以适应文本区域的首选大小,请尝试使用messageConst.weightx = 1;

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

    更新

    所以我快速玩了一下代码并想出了这个......

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

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

    【讨论】:

    • 试过了,它确实解决了我遇到的另一个问题,但文本区域仍然相同
    • @AlexMurray 我也会放弃插图(开始)
    • 啊现在它工作了,我应该在插图上使用什么来代替,以便我可以正确定位东西
    • @AlexMurray 用户正确的anchor 职位。看看更新,我在玩代码,看看是否还有其他可能导致问题的东西;)
    • "instead on insets"white space 使用布局内边距和边框。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 2014-12-13
    • 1970-01-01
    相关资源
    最近更新 更多