java 刷新jpanel_为JPanel添加滚动条需要刷新窗口才能显示

JBubble是一个自定义的JComponent子类,用于显示带有气泡效果的消息。它根据传入的消息和图片来调整自身的绘制,支持两种显示模式。代码中实现了鼠标监听,双击事件未作具体处理。此外,JBubble还包含了一个字符串列表,用于拆分长消息并在气泡内按行显示。代码中包含了计算字符串宽度的逻辑,确保气泡内容不会超出边界。
摘要由CSDN通过智能技术生成

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

JBubble的定义,是否和我没有重写repaint有关?那该怎么改?

public class JBubble extends JComponent {

private String msg;

private BufferedImage image;

private JComponent jComponent;

private int width = 0;

private boolean flag;

private List stringList;

public int height = 0;

public JBubble(String msg, JComponent jComponent) {

try {

this.image = ImageIO.read(new File("src"+File.separator+"image"+File.separator+"img_yourself.png"));

} catch (IOException e) {

e.printStackTrace();

}

this.jComponent = jComponent;

this.msg = msg;

this.flag = true;

this.stringList = new ArrayList<>();

cutString(msg);

height = (int)(stringList.size()*getPoint(msg).getY());

addMouseListener(new MouseAdapter() {

@Override

public void mouseClicked(MouseEvent e) {

if(e.getClickCount() == 2) {

}

}

});

}

public JBubble(String msg, String ip, JComponent jComponent) {

this(msg, jComponent);

this.flag = false;

try {

this.image = ImageIO.read(new File("src"+File.separator+"image"+File.separator+"img_"+ip+".png"));

} catch (IOException e) {

e.printStackTrace();

}

}

@Override

protected void paintComponent(Graphics g) {

Graphics2D graphics2D = (Graphics2D) g;

if(!flag) {

graphics2D.drawImage(image, 0, 0, 50, 50, this);

graphics2D.setColor(Color.LIGHT_GRAY);

int[] xs = {60, 75, 75, 60};

int[] ys = {25, 20, 30, 25};

graphics2D.fillPolygon(xs, ys, 3);

graphics2D.setColor(Color.lightGray);

graphics2D.fillRoundRect(75, 10, width + 5, height+5, 15, 15 );

graphics2D.setColor(Color.DARK_GRAY);

height = 0;

Iterator iterator = stringList.iterator();

while(iterator.hasNext()) {

height += getPoint(msg).getY();

graphics2D.drawString((String) iterator.next(), 80, height + 10);

}

} else {

graphics2D.drawImage(image, jComponent.getWidth() - 50, 0, 50, 50, this);

graphics2D.setColor(Color.LIGHT_GRAY);

int[] xs = {jComponent.getWidth() - 60, jComponent.getWidth() - 75, jComponent.getWidth() - 75, jComponent.getWidth() - 60};

int[] ys = {25, 20, 30, 25};

graphics2D.fillPolygon(xs, ys, 3);

graphics2D.setColor(Color.lightGray);

int height = (int)(stringList.size()*getPoint(msg).getY());

graphics2D.fillRoundRect(jComponent.getWidth() - 75 - width, 10, width + 5, height+5, 15, 15 );

graphics2D.setColor(Color.DARK_GRAY);

height = 0;

Iterator iterator = stringList.iterator();

while(iterator.hasNext()) {

height += getPoint(msg).getY();

graphics2D.drawString((String) iterator.next(), jComponent.getWidth() - 70 - width, height + 10);

}

}

super.paintComponent(g);

}

@Override

public boolean contains(int x, int y) {

if(!flag) {

return (x - 25) * (x - 25) + (y - 25) * (y - 25) < 625;

}

return false;

}

public void cutString(String msg) {

if(getPoint(msg).getX() + 160 < jComponent.getWidth()) {

stringList.add(msg);

if(width == 0) {

width = (int) getPoint(msg).getX();

}

} else {

width = jComponent.getWidth() - 160;

String temp = "";

for(int i = 0; i < msg.length(); i++) {

if(getPoint(temp + msg.charAt(i)).getX() + 160 > jComponent.getWidth()) {

stringList.add(temp);

msg = msg.substring(i, msg.length());

cutString(msg);

break;

}

temp += msg.charAt(i);

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值