java怎么在界面上添加图片?

2025-02-23 18:15:17
推荐回答(2个)
回答1:

public class Test extends JFrame {
     
public static void main(String[] args) {
new Test();
    }
     
public Test() {
JPanel panel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Image image = new ImageIcon("picture.png").getImage();
int width = Test.getFrames()[0].getWidth();
int heigh = Test.getFrames()[0].getHeight();
g.drawImage(image, 0, 0, width, heigh, null);

}
};
add(panel);
setBounds(500, 100, 480, 320);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

回答2:

java.swing api有详细解释