Good Day...

the problem is that the Jbutton is not displaying in my Jpanel.
can you help me, this will help me a lot, Thanks Here's the code

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JOptionPane;

public class GameBoard extends JPanel implements ActionListener {
    JButton start = new JButton("START");
    JButton controls = new JButton("CONTROLS");
    JButton credit = new JButton("CREDITS");
    JButton exit = new JButton("EXIT");

    public GameBoard() {
        JFrame frame = new JFrame("The Palace");
        frame.setSize(800,600);
        frame.setResizable(false);
        frame.setVisible(true);
    }

    public void initComponents() {
        //to detect button clicks
        start.addActionListener(this);
        controls.addActionListener(this);
        credit.addActionListener(this);
        exit.addActionListener(this);
        add(start);
        add(controls);
        add(credit);
        add(exit);
    }

    public void actionPerformed (ActionEvent e) {
    JButton press = (JButton)e.getSource();
    if (press.equals(this.start)) {
      new start();
      setVisible(false);
    }
    if (press.equals(this.controls)) {
      new controls();
      setVisible(false);
    }
    if (press.equals(this.credit)) {
      new credit();
      setVisible(false);
    }
    if (press.equals(this.exit)) {
    int response = JOptionPane.showConfirmDialog(null, "Do you want \nto continue the Game?", "Exit Game", 0, 3);
      if (response == 0)
        System.exit(0);
    }
  }
    
    public void playLoop(String filename)
  {
    try {
      URL url = getClass().getResource(filename);
    }
    catch (Exception e) {
    }
  }

  public static void main(String[] args) {
    new GameBoard();
  }
}

Ive only done basic java, and it was a long time ago, but where do you call InitComponents()? You should call it in your ctor. I believe this is why nothing is being drawn because you don't add them to your frame(because you don't call InitComponents()).

you hv to take container class refernce
like contaner c; // before constructor
then c=getContentPane(); //in the starting of constructor
c.add(start); /*after the buttons setbounds
c.add(controls);
c.add(credit);

you hv to take container class refernce
like contaner c; // before constructor
then c=getContentPane(); //in the starting of constructor
c.add(start); /*after the buttons setbounds
c.add(controls);
c.add(credit);

This code is obsolete.
The need to add things to the content pane, rather than just the JFrame, was an annoyance in early versions of Java, but this was fixed in Java 1.5. In 1.5 and later you just add components to the JFrame, and don't need to worry about the content pane.

(LevyDee had the right answer)

yes! problem solved :D
look, i revised some codes for the GameBoard

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JFrame;

public class GameBoard extends JPanel implements ActionListener {
    JButton start = new JButton("START");
    JButton controls = new JButton("CONTROLS");
    JButton credit = new JButton("CREDITS");
    JButton exit = new JButton("EXIT");
    JFrame frame = new JFrame();

    public GameBoard(JFrame frame) {
    this.frame = frame;
    }

    public void initComponents() {
        //to detect button clicks
        start.addActionListener(this);
        controls.addActionListener(this);
        credit.addActionListener(this);
        exit.addActionListener(this);
        add(start);
        add(controls);
        add(credit);
        add(exit);
    }

    public void actionPerformed (ActionEvent e) {
    JButton press = (JButton)e.getSource();
    if (press.equals(this.start)) {
      new start();
      setVisible(false);
    }
    if (press.equals(this.controls)) {
      new controls();
      setVisible(false);
    }
    if (press.equals(this.credit)) {
      new credit();
      setVisible(false);
    }
    if (press.equals(this.exit)) {
    int response = JOptionPane.showConfirmDialog(null, "Do you want \nto continue the Game?", "Exit Game", 0, 3);
      if (response == 0)
        System.exit(0);
    }
  }
    
    public void playLoop(String filename)
  {
    try {
      URL url = getClass().getResource(filename);
    }
    catch (Exception e) {
    }
  }
}

and then i make implementation to this program here's the code

import javax.swing.*;
public class GameBoardImplementation {
    public GameBoardImplementation() {
        JFrame frame = new JFrame("The Palace");
        GameBoard board = new GameBoard(frame);
        board.initComponents();
        frame.add(board);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800,600);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);
    }

    public static void main (String[]args) {
        new GameBoardImplementation();
    }
}

i think here's the reason why the jbutton is not displaying

public static void main (String[]args) {
        new GameBoardImplementation();
    }

but anyway this program is solve, thanks..

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.