Hi I have design a basic JFrame which displays three buttons in a frame. The code is given below:

import java.awt.*;
import javax.swing.*;

public class PaymentFrame {

public static void main(String[] args) {

JFrame f = new JFrame("Payment Frame ");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(800, 600);
Container content = f.getContentPane();
content.setBackground(new Color(240,170,50));
content.setLayout(new GridBagLayout());
content.add(new JButton("Enter Ticket No"));
content.add(new JButton("Pay (Cash)"));
content.add(new JButton("Pay (Card)"));
f.setVisible(true);
}
}


I wantto include images in the frame. I now how to do this using applets, but i would like to include and image or gif file in this frame to go together with buttons that are already in the frame. But Im not sure that this will work.

I am tempted to include the or import, the java.awt.Gaphics and the java.awt.Image in this frame to be able to handle and manilpulate an image. I am also thinking of using the
paint(Graphics g) method to do this. But I want to know if this is even possible having images in a frame and not in an applet. And also if there was a simpler way to do this. If anyone has the expetice on doing this could you please point me in the right direction and telling what is the best way to accomplish this?

Many thanks in advance...

Recommended Answers

All 3 Replies

Use this to load your image: ImageIcon ICON = new ImageIcon("YOUR.GIF"); Then use this to attach the image to your buttons: BUTTON.setIcon(ICON); Make sure you import the single file or folder that contains the icons into your project.
The example above would work if you import just the file. If you import, a folder instead, use new ImageIcon("FOLDER/YOUR.GIF"); instead

Thanks for the advice I was also thinking of something like that. My idea ws of adding an image into a component pane sepate to that of the buttons component. But thanks very much for the advice, I will go and try it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.