簡體   English   中英

如何將JPanel從另一個類添加到框架

[英]How to add JPanel from another class to frame

我試圖弄清楚如何在我的主框架中添加一個JPanel。 但是,小組屬於不同的類別。 本質上,我需要用戶按下開始按鈕,一旦按下它就需要創建一個類的對象(該類創建一個JPanel)並添加到主框架。 我的問題是,一旦按下開始按鈕,什么都不會發生。

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

這是創建面板的另一個類。

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

將組件添加到可見的GUI時,基本代碼為:

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

你到底想要什么? 我的意思是您希望添加的汽車開始運行??? 對於這種情況,您需要編寫一個線程。 您的開始按鈕可以正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM