0
我使用以下片段創建Chatlog。帶有修復程序的JScrollPane中的JEditorPane背景圖片
chatlog = new JEditorPane("text/html", "<html>") {
private static final long serialVersionUID = 1L;
protected void paintComponent(Graphics g) {
// set background green - but can draw image here too
g.setColor(new Color(0,0,0,0));
g.fillRect(0, 0, getWidth(), getHeight());
// uncomment the following to draw an image
Image img;
try {
img = ImageIO.read(new File("images/chatlog.png"));
g.drawImage(img, 0, 0, this);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
super.paintComponent(g);
}
};
chatlog.setFont(new Font("Segoe UI", Font.PLAIN, 14));
chatlog.setEditable(false);
chatlog.setOpaque(false);
chatScroll = new JScrollPane(chatlog);
chatScroll.setBounds(4, 454, 620, 100);
chatScroll.setOpaque(false);
chatScroll.setBackground(new Color(0,0,0,0));
chatScroll.setBorder(BorderFactory.createEmptyBorder());
chatScroll.setVisible(false);
就像你看到的,我在JEditorPane中使用了背景圖像。但是,如果我將大量內容寫入Chatlog,背景圖像將隨文本一起移動。
你能幫我嗎?
無關:不做組件的任何手動尺寸/定位,永遠 - 這就是佈局管理的全部責任。 – kleopatra
相關:是的,在編輯器頂部繪製時還應該做些什麼:-)要麼縮放以填充整個窗格(可能看起來有點奇怪),要麼將其繪製爲viewPort的背景 – kleopatra