如何制作addActionListener并添加工作?

时间:2013-12-07 01:00:53

标签: java swing actionlistener

我查看了很多与addActionListener有关的帖子,但没有一个真的帮助我。我只是试图让这个程序在修补它一段时间后运行,我仍然不知道如何获得我的" addActionListener"和"添加"工作,所以任何帮助将不胜感激

我遇到的问题是大约一半,然后靠近底部,两个箭头都指向它们。我是这个网站的新手,所以我不知道如何编码代码,如果我可以:(对不起给您带来的不便,谢谢您的帮助!

    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;

    public class Phone extends JFrame
    {
       private JButton keyJButton[];
       private JPanel keyJPanel;
       private JPanel lcdJPanel;
       private JTextArea lcdJTextArea;
       private String lcdOutput = "";
       private int count;

       // constructor sets up GUI
       public Phone()
       {
          super( "Phone" );

          lcdJTextArea = new JTextArea( 4, 15 );
          lcdJTextArea.setEditable( false );
          lcdJPanel.add( lcdJTextArea );

           JButton keyJButton[] = new JButton[15];

          // initialize all digit key Buttons
          for ( int i = 3; i <= 11; i++ )
             keyJButton[ i ] = new JButton( String.valueOf( i - 2 ) );

          // initialize all non-digit key Buttons
          keyJButton[ 0 ] = new JButton( "Send" );
          keyJButton[ 1 ] = new JButton( "clr" );
          keyJButton[ 2 ] = new JButton( "End" );
          keyJButton[ 12 ] = new JButton( "*" );
          keyJButton[ 13 ] = new JButton( "0" );
          keyJButton[ 14 ] = new JButton( "#" );

          keyJButton[0].addActionListener;//<------------------THIS IS WHERE I'M HAVING                  //                                                                 PROBLEMS

                public void actionPerformed( ActionEvent e ) 
                {
                   lcdOutput = "Calling...\n\n" + lcdOutput;
                   lcdJTextArea.setText( lcdOutput );
                } // end method actionPerformed
              // end new ActionListener
        // end addActionListener call

          keyJButton[ 1 ].addActionListener(

             new ActionListener()
             {
                public void actionPerformed( ActionEvent e ) 
                {
                   if ( lcdOutput.length() == 0 || 
                      lcdOutput.substring( 0, 1 ).equals( "C" ) )
                      return;
                   else
                   {
                      lcdOutput = lcdOutput.substring( 0, ( lcdOutput.length() - 1 ) );
                      lcdJTextArea.setText( lcdOutput );
                   } // end else
                } // end method actionPerformed
             } // end object ActionLstener
          ); // end addActionListener call

          keyJButton[ 2 ].addActionListener(

             new ActionListener()
             {
                public void actionPerformed( ActionEvent e ) 
                {            
                   lcdJTextArea.setText( " " );
                   lcdOutput = "";
                } // end method actionPerformed
             } // end new ActionListener
          ); // end ActionListener call

          for ( int i = 3; i <= 14; i++ )
          {
             keyJButton[ i ].addActionListener(

                new ActionListener()
                {
                   public void actionPerformed( ActionEvent e ) 
                   {
                      lcdOutput += e.getActionCommand();

                      if ( lcdOutput.substring( 0, 1 ).equals( "C" ) )
                          return;

                      lcdJTextArea.append( e.getActionCommand() );
                   } // end method actionPerformed
                } // end new ActionListener
             ); // end addActionListener call
          } // end for loop

          // set keyJPanel layout to grid layout
          keyJPanel = new JPanel();
          keyJPanel.setLayout( new GridLayout( 5, 3 ) );

          // add buttons to keyJPanel
          for ( int i = 0; i <= 14; i++ )
             keyJPanel.add( keyJButton[ i ] );

          // add components to container
          add( lcdOutput, BorderLayout.NORTH );//<---------------THIS AS WELL
       } // end Phone constructor
    } // end class Phone

2 个答案:

答案 0 :(得分:3)

尝试将ActionListener添加到keyJButton [0]时,您没有正确的语法。看起来你已经适合其他人,但这一行:

keyJButton[0].addActionListener;

应替换为:

keyJButton[0].addActionListener(
             new ActionListener()
             {

不要忘记关闭任何额外的支架和支架。

您也无法将lcdOutput添加到您的框架中,因为它是一个String而不是某种类型的组件。也许你可以构建一个标签并添加它?

add( new JLabel(lcdOutput), BorderLayout.NORTH );

编辑:刚刚意识到第27行将抛出一个NPE。在向其添加任何内容之前,需要初始化lcdJPanel。

答案 1 :(得分:0)

您可以将actionListener添加到按钮中,如下所示:

keyJButton[0].addActionListener(this);

然后在actionPerformed方法中,执行以下操作:

public void actionPerformed( ActionEvent e ) 
                {
                   Object target=e.getSource();

                   if (target == keyJButton[0] )
                   {
                      //do stuff
                   }
                   if (target == keyJButton[1] )
                   {
                         //do stuff
                   }
                }

使用此选项,您无需为每个按钮创建new ActionListener()。只需向每个按钮添加一个actionListener,并使用actionPerformed方法中的if语句来决定单击哪个按钮。