-1

I am working on a main menu for a program and i am finding it hard to apply a .png file to my JButton, this is where i have arrived

public class Menu extends JFrame implements ActionListener {
public static void main(String[] args) {
    Color b = new Color(0,89,255);
    Color t = new Color(255,0,0);
    Color bttn = new Color (255,255,0);


    final JFrame frame = new JFrame ("Main Menu");
    frame.setVisible(true);
    frame.setSize(1000,750);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setLayout(null);
    frame.setBackground(b);


    JButton start = new JButton ("Start");
    start.setBounds(300, 300, 150, 75);
    start.setForeground(t);


    JButton exit = new JButton ("Exit");
    exit.setBounds(550, 300, 150, 75);
    exit.setBackground(bttn);
    exit.setForeground(t);

Pls leave any answer as i am trying to hurry

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user2956959
  • 3
  • 1
  • 2

1 Answers1

2
myButton.setIcon(new ImageIcon("/path/to/image.png"));  

is the way you would do what you want after you have created a JButton. If you want to add an image at the time of creating a JButton then you can use the appropriate constructor.

JButton myButton = new JButton("What The Heck?", new ImageIcon("/path/to/image.png"));  

and please so not use a null layout. Use a LayoutManager instead.

An SO User
  • 24,612
  • 35
  • 133
  • 221