-1

我想设置 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);
4

2 回答 2

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);
    }
}

;
于 2013-03-29T14:08:58.613 回答
1
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);
    }
};
于 2018-03-21T20:21:44.840 回答