0

I am trying to create a GUI class that extends JFrame. The Frame has 3 buttons with a background image. But so far I am getting nothing but a white blank frame.

public class guiOpen extends JFrame implements ActionListener {

  Container c = getContentPane();
  JButton database = new JButton("Database");
  JButton create = new JButton("Player Creator");
  JButton edit = new JButton("Player Editor");
  JButton Csave = new JButton("Create/Save");
  JButton Esave = new JButton("Edit/Save");
  JButton back = new JButton("Back");
  JTextArea infoBio = new JTextArea(""); 
  JPanel opener =  new JPanel();
  JPanel db = new JPanel();
  JPanel newplayer = new JPanel();
  JPanel tool = new JPanel();
  JLabel images ; 

  public guiOpen() 
  {
    Font FUD = new Font("Comic Sans MS", 1, 32);
    Font FA = new Font("Comic Sans MS", 5, 40);
    Font fBtns = new Font("Comic Sans MS", 1, 25);
    Color grassC = new Color(0, 100, 0);
    Color cBtns = new Color(255, 255, 255);

    c.add(opener);
    c.add(db);
    c.add(newplayer);
    c.add(tool);

    this.setTitle("Tatical Soccer Mania");
    this.setSize(800,600);
    this.setResizable(false);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setVisible(true);

    images = new JLabel(new ImageIcon ("Tatical Soccer Mania\\TSM_LOGO.jpg")) ;
    opener.add(images);

    validate();

    database.setBounds(50, 100, 240, 120);
    database.setFont(fBtns);
    database.setOpaque(true);
    database.setBackground(grassC);
    database.setVisible(true);
    database.addActionListener(this);
    database.setForeground(cBtns);
    opener.add(database);

    create.setBounds(50, 240, 240, 120);
    create.setFont(fBtns);
    create.setOpaque(true);
    create.setBackground(grassC);
    create.setForeground(cBtns);
    create.setVisible(true);
    create.addActionListener(this);
    opener.add(create);

    edit.setForeground(cBtns);
    edit.setBounds(50, 380, 240, 120);
    edit.setFont(fBtns);
    edit.setOpaque(true);
    edit.setBackground(grassC);
    edit.setVisible(true);
    edit.addActionListener(this);
    opener.add(edit);

    opener.setVisible(true);
    db.setVisible(false);
    newplayer.setVisible(false);
    tool.setVisible(false);
  }
}

If you can explain what is going on and it how to fix it would be very much appreciated. I looked around stack and nothing helped me fix my problem. I just started out programming, so please explain in terms that a beginner can understand.

Adi Inbar
  • 12,097
  • 13
  • 56
  • 69
HalDembd
  • 1
  • 2
  • 1
    What are the actual errors you are getting? – Evan Frisch Jun 05 '15 at 23:23
  • I don't understand why this has four close votes for the reason "Questions seeking debugging help must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it". The question states the desired behavior (the frame should have three buttons and a background image), the specific problem (getting nothing but a white blank frame), and the code necessary to reproduce it. I'm voting to leave open. – Adi Inbar Jun 06 '15 at 22:46
  • @EvanFrisch He didn't say he's getting errors, he said he's getting nothing but a white blank frame. – Adi Inbar Jun 06 '15 at 22:46

1 Answers1

1

This question has been already answered:

Java - JPanel with background image AND normal functionality

Simplest way to set image as JPanel background

There you are going to find several solutions for background images on JPanels or GUI components.

Now, as you are getting an error, if you want to get some feedback to solve it, you should give some detail about the error.

UPDATE

As you updated the question, here are my comments:

  1. You should name your class starting with a capital letter. This may help.
  2. As your class is implementing the ActionListener interface, it must implement the +actionPerformed(ActionEvent):void method.

After looking at your code, I see two possible solutions (I tested both and they work nicely):

1- Use a Layout Manager to add content to the JFrame. This way, Java knows what size is each panel, and they will be visible.

JPanel content = new JPanel();
content.setLayout(new BorderLayout());

content.add(opener, BorderLayout.NORTH);
content.add(db, BorderLayout.EAST);
content.add(newplayer, BorderLayout.CENTER);
content.add(tool, BorderLayout.SOUTH);

c.add(content);

2- Give a size to each JPanel.

opener.setSize(800, 200);

This is how it looks with a random image:

enter image description here

Now, that does not show a background image, as you used a JLabel, it only adds an image next to JButton elements.

Community
  • 1
  • 1
Miguel Jiménez
  • 1,276
  • 1
  • 16
  • 24