I'm learning GUI, can anyone show me how to display an image on a panel. So far I have this:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import java.applet.*;
public class JlabelDemo
{
public static void main(String[] args)
{
// create an image icon, put the icon on a button, then create a panel and a frame
ImageIcon imageIcon1 = new ImageIcon("red.jpg");
JButton button1 = new JButton("Press me!", imageIcon1);
JPanel panel1 = new JPanel();
JFrame frame1 = new JFrame("Image Display");
// add button1 to panel1, now add panel1 to frame1
panel1.add(button1);
frame1.add(panel1);
frame1.setSize(400, 300);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setVisible(true);
}
}
I tried to create an image like this:
Image image1 = new Image("red.jpg");
but it says its abstract.
If possible, try to show me how I can create an image, set it to "red.jpg", and display my image on the panel.