我想设置JDesktopPane的背景(我将这个JDesktopPane
直接从调色板添加到JFrame
)
我尝试覆盖方法public void paintComponent (Graphics g)
,但它不起作用
以下是代码:
JDesktop p = new JDesktop();
ImageIcon icon = new ImageIcon("images/Nénuphars6892.jpg");
final Image img = icon.getImage();
img.getScaledInstance(159, 207, Image.SCALE_SMOOTH);
p.principal = new JDesktopPane() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(img, 0, 0, getSize().width, getSize().height, this);
}
};
p.setVisible(true);
答案 0 :(得分:5)
我通过在创建JDesktopPane时添加选择自定义代码(可变主体)来解决此问题:
principal = new javax.swing.JDesktopPane()
{
ImageIcon icon = new ImageIcon("images/blue_digital_waves_abstract.jpg");
Image image = icon.getImage();
Image newimage = image.getScaledInstance(1500, 1000, Image.SCALE_SMOOTH);
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(newimage, 0, 0, this);
}
}
;
答案 1 :(得分:0)
JDesktopPane desktopPane = new JDesktopPane() {
private final ImageIcon image = new ImageIcon("sample.jpg");;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x = (desktopPane.getWidth() - image.getIconWidth()) / 2;
int y = (desktopPane.getHeight() - image.getIconHeight()) / 2;
g.drawImage(image.getImage(), x, y, this);
}
};