954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Can anyone help me to this code(JButton Problem)?

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();
  }
}
ecclesiastes3:1
Newbie Poster
4 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

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()).

LevyDee
Posting Whiz in Training
260 posts since Mar 2010
Reputation Points: 13
Solved Threads: 25
 

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);

Shubham266
Newbie Poster
1 post since Aug 2011
Reputation Points: 10
Solved Threads: 1
 
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)

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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..

ecclesiastes3:1
Newbie Poster
4 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: