3

我正在尝试使用 AWT 制作 UI。我只想使用图像和透明组件。现在我无法理解如何制作一个应该是具有自定义形状的 PNG 图像的主窗口。图像中所有透明的区域都被替换为黑色。这是我使用的代码:

public class Test {
static Image image;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        //switch to the right thread

        image = ImageIO.read(Test.class.getClassLoader().getResource("resources/images/panel.png").openStream());

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Frame frame = new Frame("Test");
                frame.setUndecorated(true);
                frame.setBackground(new Color(0,0,0,0));
                frame.add(new BackGround(image,image.getWidth(frame),image.getHeight(frame)));
                frame.pack();
                frame.setSize(image.getWidth(frame), image.getHeight(frame));
                frame.setVisible(true);
                frame.setLocationRelativeTo(null);
            }
        }
        );
    }
    private static class BackGround extends Component {
        private Image img;
        private int wid, hgt;
        public BackGround(Image img, int wid, int hgt){
            this.img=img;
            this.wid=wid;
            this.hgt=hgt;

        }
        @Override
        public void paint(Graphics graphics) {
                graphics.drawImage(image,0,0,wid,hgt,0,0,wid,hgt,null);
        }
    }
}
4

1 回答 1

6

AWT 组件没有透明度的概念,它们总是不透明的

试着看看...

有关使用 Swing 的更多示例

于 2013-08-20T01:26:01.397 回答