在主类中重用动作监听器和jframe

时间:2017-01-26 03:02:02

标签: java jframe

所以,如果有人看看下面的代码并给我一臂之力,我将欠你一生。所以问题就在这里,显然如果我把playerCreationSelection放在自己的课堂上,我可以让它发挥作用,我的问题就是让它发挥作用,在class superClass里面我无法生活我移动东西让它工作。任何帮助都会很棒,谢谢大家!

忘记了实际上出了什么问题!那么将会发生的事情是playerCreationSelection不是符号



public class superClass
{
   public static void main(String[] args) 
   {
       playerCreationSeletion gui = new playerCreationSeletion();  
   }

   public class playerCreateSelection extends JFrame implements ActionListener 
   {
   
      //create label
      public JLabel playerCreatedLabel;
      public void playerCreationSeletion()
      {
          setSize(WIDTH,HEIGHT);
          WindowDestroyer listener = new WindowDestroyer();
          addWindowListener(listener);
            
          Container contentPane = getContentPane();
          contentPane.setBackground(Color.DARK_GRAY);
            
          contentPane.setLayout(new FlowLayout());
            
          //create button
          JButton playerCreationButton = new JButton("Create New Player");
          playerCreationButton.addActionListener(this);
          contentPane.add(playerCreationButton);
            
          //create label
          playerCreatedLabel = new JLabel("Welcome New Player!");
          playerCreatedLabel.setVisible(false);
            
        }
        public void actionPerformed(ActionEvent e)
        {
            String actionCommand = e.getActionCommand();
            Container contentPane = getContentPane();
            if(actionCommand.equals("Create New Player"))
            {
                contentPane.setBackground(Color.LIGHT_GRAY);
                playerCreatedLabel.setVisible(true);
            }
        }
     }
  }




1 个答案:

答案 0 :(得分:0)

嗯,你有一个拼写错误" playerCreationSeletion()"。此外,您需要像这样调用内部类构造函数并使用setVisible和setSize。

public static void main(String[] args) {
       playerCreateSelection gui = new superClass().new playerCreateSelection();  

       gui.setSize(500, 500);
       gui.setVisible(true);
   }

试试。