我的最终目标是拥有一个带有背景图片的
这就是我的意思:
有没有办法添加一种向内的缩进,使文本不会重叠在图片边缘上?
这是原始的评论气泡图像。
以下是代码:
JTextArea
。我在网上找到了代码,教我如何做到这一点,但现在我遇到一个问题,就是文本叠在图片上面。这就是我的意思:
![Text overlapping image](https://istack.dev59.com/mlBbQ.webp)
这是原始的评论气泡图像。
![Comment bubble](https://istack.dev59.com/JUBif.webp)
以下是代码:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.GrayFilter;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class myBackgroundSample {
String file;
public myBackgroundSample(String i) {
file = i;
setItUp();
}
public void setItUp() {
final ImageIcon imageIcon = new ImageIcon(file);
JTextArea textArea = new JTextArea() {
Image image = imageIcon.getImage();
public void paint(Graphics g) {
setOpaque(false);
g.drawImage(image, 0, 0, this);
super.paint(g);
}
};
JFrame frame = new JFrame("Background Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scrollPane = new JScrollPane(textArea);
Container content = frame.getContentPane();
content.add(scrollPane, BorderLayout.CENTER);
frame.setSize(400, 400);
frame.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String right = "chat1.jpg";
myBackgroundSample temp = new myBackgroundSample(right);
}
}
JTextArea
(而不是JLabel
或其他样式组件)?CommentBubbleBorder
。paintComponent(Graphics)
,而不是paint(Graphics)
。