JTextPane在scrollpane内部伸展

时间:2015-11-01 22:48:28

标签: java swing user-interface jscrollpane jtextarea

我遇到滚动窗格问题。基本上我有一个面板,其中包含一个滚动窗格,其中包含另一个面板,我在其中添加了文本区域。

这是我的代码。

package messagebubble;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;

 import javax.swing.Box;
 import javax.swing.BoxLayout;
 import javax.swing.JFrame;
 import javax.swing.JPanel; 
 import javax.swing.JScrollBar;
 import javax.swing.JScrollPane;
 import javax.swing.JTextArea;
 import javax.swing.ScrollPaneConstants;
 import javax.swing.SwingUtilities
 import javax.swing.border.CompoundBorder;
 import javax.swing.border.EmptyBorder;


public class JanelaDeConversa extends JPanel {

private String nome;
private static final int PREF_W = 400;
private static final int PREF_H = 500;
private JPanel msgHoldingPanel = new JPanel();
private JScrollPane scroll;

public JanelaDeConversa(String nome) {

    msgHoldingPanel.setLayout(new BoxLayout(msgHoldingPanel, BoxLayout.PAGE_AXIS));
    setLayout(new BorderLayout());
    //scroll = new JScrollPane(msgHoldingPanel);
    //scroll.setPreferredSize(getPreferredSize());
    add(msgHoldingPanel, BorderLayout.PAGE_START);
    this.nome = nome;

}

public void receiveMessage(String s) {
    msgHoldingPanel.add(Box.createRigidArea(new Dimension(0,10)));
    JTextArea msg = new JTextArea();

    msg.setText(s);
    Color bg = Color.decode("#AFEEEE");
    msg.setBackground(bg);
    msg.setEditable(false);

    if (msg.getPreferredSize().width >= PREF_W) {

        msg.setBorder(new RoundedBorder());
        msg.setLineWrap(true);
        msg.setWrapStyleWord(true);

    } else {
        int left = PREF_W - msg.getPreferredSize().width - 20;
        msg.setBorder(new CompoundBorder(new EBorder(0, 0, 0, left),
                new RoundedBorder()));
        msg.setMaximumSize(msg.getPreferredSize());
    }
    msgHoldingPanel.add(msg);
}

public void sendMessage(String s) {
    msgHoldingPanel.add(Box.createRigidArea(new Dimension(0,10)));
    JTextArea msg2 = new JTextArea();
    msg2.setEditable(false);
    msg2.setText(s);
    msg2.setBorder(new RoundedBorder());
    Color bg3 = Color.decode("#BADBAD");

    msg2.setBackground(bg3);

    if (msg2.getPreferredSize().width >= PREF_W) {
        System.out.println("yes");
        msg2.setLineWrap(true);
        msg2.setWrapStyleWord(true);

    } else {
        int right = PREF_W - msg2.getPreferredSize().width;
        msg2.setBorder(new CompoundBorder(new EBorder(0, right, 0, 0),
                new RoundedBorder()));
        msg2.setMaximumSize(msg2.getPreferredSize());
    }
    msgHoldingPanel.add(msg2);
}

@Override
public Dimension getPreferredSize() {
    return new Dimension(PREF_W, PREF_H);

}

@Override
public String toString() {
    return nome;
}

public static void main(String[] args) {
    JFrame window = new JFrame();
    JanelaDeConversa chat = new JanelaDeConversa("EU");
    chat.sendMessage("Hey");
    chat.receiveMessage("Heeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeey");
    window.add(chat);
    window.pack();
    window.setVisible(true);
}




}

以下是自定义边框的代码

RoundedBorder

package messagebubble;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;

import javax.swing.border.AbstractBorder;

public class RoundedBorder extends AbstractBorder {



@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width,
        int height) {

    Graphics2D graph = (Graphics2D) g.create();
    RoundRectangle2D round = new RoundRectangle2D.Float(x, y, width - 1,
            height - 1, 30, 30);
    Container parent = c.getParent();

    if (parent != null) {
        graph.setColor(parent.getBackground());
        Area canto = new Area(new Rectangle2D.Float(x,y,width,height));
        canto.subtract(new Area(round));
        graph.fill(canto);
    }

    graph.setColor(Color.GRAY);
    graph.draw(round);
    graph.dispose();

}

@Override
public Insets getBorderInsets(Component c) {
    return new Insets(5, 10, 5, 10);
}

@Override
public Insets getBorderInsets(Component c, Insets insets) {
    insets.left = 10;
    insets.right = insets.left;
    insets.top = 5;
    insets.bottom = insets.top;
    return insets;
}

}

EBorder

package messagebubble;

import java.awt.Component;
import java.awt.Graphics;

import javax.swing.border.EmptyBorder;

public class EBorder extends EmptyBorder {

private static final long serialVersionUID = 1265703863790826185L;

public EBorder(int top, int left, int bottom, int right) {
    super(top, left, bottom, right);
}

@Override
public void paintBorder(Component c, Graphics g, int x, int y,
        int width, int height) {
    g.setColor(c.getParent().getBackground());
    g.fillRect(x, y, left, height);
    g.fillRect(x + width - right, y, right, height);
}
}

现在,我没有滚动窗格就是这个,这就是我想要的。 Good

但是使用滚动窗格我得到了这个

Bad

我需要它才能滚动。有谁知道如何解决这个问题?

0 个答案:

没有答案