java - 如何使addActionListener和add工作?

标签 java swing actionlistener

我浏览了很多与 addActionListener 有关的帖子,但没有一个真正帮助我解决这个问题。我只是想让这个程序在修改了一段时间后运行,但我仍然不知道如何让我的“addActionListener”和“add”工作,所以任何帮助将不胜感激

我遇到的问题大约在中间,然后在底部附近,都有箭头指向它们。我是这个网站的新手,所以我不知道如何对代码进行编号(如果可以的话):( 对于给您带来的不便,我们深表歉意,并感谢您的帮助!

    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

最佳答案

尝试将 ActionListener 添加到 keyJButton[0] 时,您没有获得正确的语法。看起来你对其他人来说是正确的,但是这一行:

keyJButton[0].addActionListener;

应替换为:

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

不要忘记关闭任何附加的方括号和大括号。

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

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

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

关于java - 如何使addActionListener和add工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20436226/

相关文章:

java - 尽管添加了 JScrollPane,JList View 区域仍在增长

java - Main 必须是抽象的,ActionPerformed 方法与 Listener 存在问题

JavaFX - 将节点添加到父级时的奇怪行为

java - 为什么我的框架没有调整大小?

Java String.split() 有时会给出空白字符串

java - 在 iOS7 中编码 UIImage 并在 Java 中解码(Base64)

java - ActionListener 中的 ArrayList 不工作

java - JButton 在 JFrame 中响应,但不在 JPanel 中响应

java - 使用类似 'Select * from some_table where some condition' 的查询从数据库创建 Pdf

java - 能够在 RecyclerView 中同时点击两个项目