如何将按钮从一个类添加到另一个类

时间:2016-03-23 01:45:29

标签: java class button

我一直在尝试将位于Players类中的按钮添加到footballField类中。当我在“控制”面板类中将按钮添加到footballFiel类时,按钮不会出现。 Players类具有空布局设计,控制面板有2个面板,边框布局设计。如何让一个类的按钮出现在另一个类中?我的总体目标是创造一个足球比赛:

    public class ControlPanel extends JPanel{

private Players footballPlayers;
private FootBallField footField;
private Score scoreBoard;

private TimerSetup Timer;



ControlPanel(){
    super();
 // This is the scoreboard clas
 scoreBoard = new Score(); 
 // The players class
 footballPlayers = new Players();

 //The football player class object is created
 footField = new FootBallField(footballPlayers);
 Timer = new TimerSetup(footballPlayers);

 //The field is added
 //The scoreboard is added
 setLayout(new BorderLayout());
 add(footField,"Center");
 add(scoreBoard,"North");

 footField.add(footballPlayers);

}

Players(){

          setLayout(null); 
          movablePlayer();
          addMachinePlayers();

    }

public void movablePlayer(){
     //Assigns image to variable Player Image
    playerImage = new ImageIcon("Images/footballPlayer.jpg");
    //FootballPlayer button is added
    footBallPlayer = new JButton();  
    add(footBallPlayer); 
    footBallPlayer.setIcon(playerImage);
    footBallPlayer.setOpaque(false);
    footBallPlayer.setBounds(new Rectangle(x, y, 50, 50)); 
    setFocusable(true); 
    addKeyListener(this); 
    requestFocusInWindow();
}

public void addMachinePlayers(){

    // Bot 1
    machinePlayers.add(new JButton("Bot1"));
    //machinePlayers.get(0).setIcon(playerImage);
    machinePlayers.get(0).setBounds(100,100,100,100);
    add(machinePlayers.get(0));
    // Bot 2

    machinePlayers.add(new JButton("Bot2"));
    //machinePlayers.get(0).setIcon(playerImage);
    machinePlayers.get(1).setBounds(100,200,100,100);
    add(machinePlayers.get(1));

    //Bot 3   
}

0 个答案:

没有答案