【问题标题】:JPanel How to make button work?JPanel 如何使按钮工作?
【发布时间】:2014-04-05 04:58:28
【问题描述】:

当您阅读此代码时,您会意识到我有一个动作事件可以工作,它会打开一个新的 JPanel,显示将运行 ballBounce 的按钮,但现在我卡在试图在该框架内获得一个工作按钮因为该框架已经在 actionEvent 中,有什么帮助吗?

 public class MainJPanelOperation
    {
        public static void main(String[] a)
        {

            JPanel panel1 = new JPanel(new GridLayout(5, 10, 1, 1));
            JButton t1 = new JButton();
            JButton t2 = new JButton();
            JButton letsStart = new JButton("Start The Program!");
            JButton t3 = new JButton();
            JButton t4 = new JButton();
            //letsStart.setBounds(200,250,12,12);
            panel1.add(t1);
            panel1.add(t2);
            panel1.add(letsStart);
            panel1.add(t3);
            panel1.add(t4);
            final JFrame frame1 = new JFrame("Game");
            frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame1.add(panel1);
            frame1.setSize(1000,1000);
            frame1.setVisible(true);
            t1.setVisible(false);
            t2.setVisible(false);
            t3.setVisible(false);
            t4.setVisible(false);
            letsStart.setBackground(Color.yellow);
            panel1.setBackground(Color.black);
            letsStart.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("panel 2 main menu intro online");
                    JPanel panelMM = new JPanel(new GridLayout(5, 10, 1, 0));
                    JButton MM1 = new JButton("BallBounce");
                    panelMM.add(MM1);
                    JFrame frameMM = new JFrame("Game/Main Menu");
                    frameMM.add(panelMM);
                    frameMM.setSize(1000,1000);
                    frameMM.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frameMM.setVisible(true);
                    frame1.setVisible(false);
                }
            });//end of start sequence

        }
    }

【问题讨论】:

    标签: java swing jpanel jbutton actionlistener


    【解决方案1】:
    JPanel panelMM = new JPanel(new GridLayout(5, 10, 1, 0));    
    JButton MM1 = new JButton("BallBounce");
    panelMM.add(MM1);
    final JFrame frameMM = new JFrame("Game/Main Menu");
    frameMM.add(panelMM);
    frameMM.setSize(1000,1000);
    frameMM.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    letsStart.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("panel 2 main menu intro 
                    frameMM.setVisible(true);
                    frame1.setVisible(false);
                }
            });
    

    您可以将 frameMM 设为最终版本,无需将所有代码都放在 ActionListener 中。

    【讨论】:

      【解决方案2】:

      试试这个:它在动作监听器中工作。

               JButton  MM1 = new JButton("BallBoe");
                MM1.addActionListener(new ActionListener()
                  {
                   public void actionPerformed(ActionEvent e)
                  {
                    System.out.println("panel 2");
                  }
                 });
      

      class MainJPanelOperation implements ActionListener

      你可以使用class MainJPanelOperation { static JButton MM1; //your code }

             MM1=new JButton("Button");
             MM1.addActionListener(this);
      

      在 Main() 之外写一个方法

          public void ActionPerformed(ActionEvent e)
         {
          if(e.getSource()==MM1)
          {
           System.out.print("");
          }
         if(e.getSource()==Buttonobject)
         {
           //your code for button Pressing Event
         }
        }
      

      【讨论】:

      • 所以只需将其添加到构成按钮的行下方?我想我可以做到这一点,但是如果让动作监听器像这样在彼此内部进行操作会变得很混乱
      • 静态 JButton MM1;出错:表达式的非法开始
      • 好的,我得到了静态工作,但现在我得到了一个 nullPointerException
      猜你喜欢
      • 2012-11-13
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      • 1970-01-01
      • 2013-11-17
      • 2015-06-26
      • 1970-01-01
      相关资源
      最近更新 更多