user1043428
user1043428

Reputation: 9

Show Image when click a button

I have a really short and simple cuestion.

What must Y type on *.java file to show a png file on screen when I click on a button.

I've been searching and trying with intents and many otrher ways but I've not found the answer.

Thanks

Upvotes: 0

Views: 1619

Answers (2)

camickr
camickr

Reputation: 324108

Read the Swing tutorial. The sections on How to Write an Action Listener or How to Use Icons would probably be a good place to start.

Upvotes: 1

Michael Berry
Michael Berry

Reputation: 72284

What do you mean by "on screen"? In a window? On its own? Have you got anything of the sort already? Generally speaking, you can use the ImageIO class to read from a PNG file into a BufferedImage:

BufferedImage image = ImageIO.read(new File("image.png"));

Then the quickest way is probably to attach the image to a label using ImageIcon:

JLabel imgLabel = new JLabel(new ImageIcon(image));

You can then add imgLabel to a JFrame or similar which you can then display.

Upvotes: 1

Related Questions