Messaging 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. 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. 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; which may effecting GridBagLayout's ability to honor the preferred size of the text area, try using 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)

Updated

So I had a quick play around with the code and came up with this...

enter image description here

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.

EAST is to the right, WEST is to the left ;)

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归因
不隶属于 StackOverflow
scroll top