user_123945839432
user_123945839432

Reputation: 189

How to add JPanel from another class to frame

Im trying to figure out how to add a JPanel into my main Frame. However, the Panel is in a different class. Essentially, I need the user to press the start button,once pressed it needs to create an object of a class (this class creates a JPanel) and add to main frame. My issue is that once I press the start button nothing happens.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;




public class Display extends JFrame{


    private JPanel right,left,center,south;
    private JButton start, stop,car1,car2,car3,car4;
    private JTextArea text1,text2;
    private TitledBorder title1,title2;
    private JLabel label,label2,label3;
    private RaceDisplay rd;
    private Environment env;



    public Display() {

        super("CAR GAME");


   /* ---------------------------------
    *            BOARD PANELS
    -----------------------------------*/
        //right panel uses a different layout
        right = new JPanel();
        right.setLayout(new BoxLayout(right, BoxLayout.PAGE_AXIS));
        right.setBackground(Color.GRAY);

         //center panel uses default layout
        center = new JPanel();


        //left panel uses a different layout
        left = new JPanel();
        left.setLayout(new BoxLayout(left, BoxLayout.PAGE_AXIS));
        left.setBackground(Color.GRAY);
        //south panel
        south = new JPanel();
        south.setBackground(Color.GRAY);

       /* ---------------------------------------
        * Text area used to diaply the results.
        ------------------------------------------*/  
        text1 = new JTextArea();
        text2 = new JTextArea();

    // ------------>car images to be used in the Car class<------------------
        ImageIcon img = new ImageIcon("./images/Car1-small.gif");
        ImageIcon img2 = new ImageIcon("./images/car2-small.gif");
        ImageIcon img3 = new ImageIcon("./images/car3-small.gif");
        ImageIcon img4 = new ImageIcon("./images/car4-small.gif");

        ImageIcon imgFlag = new ImageIcon("./images/flag1.png");
        ImageIcon imgFlag2 = new ImageIcon("./images/flag2.png");

        label2 = new JLabel(imgFlag);
        label3 = new JLabel(imgFlag2);
        center.add(label3);

        label = new JLabel("BEST TEAM EVER RACE GAME");
        label.setFont(new Font("Georgia", Font.CENTER_BASELINE, 16));



     /* ----------------------------------------------------
      * creates the buttons and adds the proper image to them 
      --------------------------------------------------------*/   
        car1 = new JButton("BRITISH MOTOR COMPANY",img);
        car2=new JButton("FAST AND FURIOUS",img2);
        car3=new JButton("SCOOBY GANG",img3);
        car4=new JButton("SPEEDY CADDY",img4);
        start=new JButton("START");
        stop  = new JButton("STOP");

    /* ----------------------------------------------------
     * creates the title border and adds them to panels 
      --------------------------------------------------------*/   
       title1 = new TitledBorder("RESULTS");
       title2 = new TitledBorder("CHOOSE YOUR RACER!");
       //adds the title borders to the Panels.
       right.setBorder(title1);
       left.setBorder(title2);


   /* ----------------------------------------------------
    * This TextArea is added to the right Panel and it where
    * the result will be displayed
    --------------------------------------------------------*/    

       text1 = new JTextArea(" ",100,30);
       right.add(text1); 
       text1.setLineWrap(true);

  /* ----------------------------------------------------
   * adds the buttons to the proper panels 
   --------------------------------------------------------*/
       south.add(start);
       south.add(stop);
       left.add(car1);
       left.add(car2);
       left.add(car3);
       left.add(car4);

       left.add(label);
       left.add(label2);


    /* ----------------------------------------------------
    * adds the panels to the main Frame at proper location
     --------------------------------------------------------*/
        add(right,BorderLayout.EAST);
        add(left,BorderLayout.WEST);
        add(south,BorderLayout.SOUTH);
        add(center,BorderLayout.CENTER);


  /* -------------------------------------------------
   *        Gives actions to the buttons
   ---------------------------------------------------- */
        car1.addActionListener(new Car1Button());
        car2.addActionListener(new Car2Button());
        car3.addActionListener(new Car3Button());
        car4.addActionListener(new Car4Button()); 
        start.addActionListener(new Start()); 


   /* ----------------------------------------------------
    *           sets up the main frame's components 
    --------------------------------------------------------*/
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1900,700);
        setVisible(true);

    }//end of constructor

     /**
     * 
     */
    private class Start implements ActionListener{

        public void actionPerformed(ActionEvent event){
             rd  = new RaceDisplay();
              add(rd);
        }  
    }

This is the other class where the panel is created.

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;

public class RaceDisplay extends JPanel implements ActionListener{

   private   Image img1,img2,img3,img4;
   private int velX;
   private int x;
   private Timer tm;
   private Environment env;
   private Car car;

   public RaceDisplay(){

        tm = new Timer(30,this);
        x=0;
        //velX=car.getSpeed();
        velX=x;
    }

    public void paintComponent(Graphics g){

        super.paintComponent(g);
        ImageIcon car1 = new ImageIcon("./images/Car1-small.gif");
        ImageIcon car2 = new ImageIcon("./images/car2-small.gif");
        ImageIcon car3 = new ImageIcon("./images/car3-small.gif");
        ImageIcon car4 = new ImageIcon("./images/car4-small.gif");

        img1 = car1.getImage();        
        img2 = car2.getImage();
        img3= car3.getImage();
        img4= car4.getImage();



        g.drawImage(img1,x,100,null);
        g.drawImage(img2,x,200,null);
        g.drawImage(img3,x,300,null);
        g.drawImage(img4,x,400,null);
        tm.start();

    }

//method runs the images from left to right 
   public void actionPerformed(ActionEvent e) {
       x = x+velX;

       if(x>=600){
           x=0;
           x=x+velX;
         // //     this.wait();
          // } catch (InterruptedException ex) {
          //     Logger.getLogger(RaceDisplay.class.getName()).log(Level.SEVERE, null, ex);
         //  }
           repaint();
       }
       repaint();
    }
    public int getX(){
        return x;
    }
}

Upvotes: 0

Views: 675

Answers (2)

Prakhar
Prakhar

Reputation: 536

What exactly do you want? I mean you want the added cars to start running??? for that case you need to write a thread. your start button works perfectly.

Upvotes: 0

camickr
camickr

Reputation: 324098

When you add a component to a visible GUI the basic code is:

panel.add(...);
panel.revalidate(); // to invoke the layout manager
panel.repaint();

Upvotes: 1

Related Questions