将ActionListener添加到JButton时出错

时间:2013-12-07 12:09:10

标签: java actionlistener

如何向ActionListener添加JButton? 我无法在此代码

中向buttons[1]添加操作
import java.awt.*;   
import java.awt.event.*;
import javax.swing.*;
public class MyForm extends Frame implements ActionListener   {
    Label  Label1, Label2, Label3, Label4;
    TextField  txt1, txt2, txt3;
    Panel p1, p2, p21, p22, p3, p4;
    public JButton[] buttons;
    public MyForm ( String title )  {        
        super( title );
        addWindowListener ( new MyWindowAdapter( ) );  /* Default code */
        Panel p1= new Panel( );
            p1.setLayout ( new GridLayout( 1, 4 )  );
            p1.setMaximumSize( new Dimension( 4*100,  1*100 ) );
        Font font1 = new Font( "Times New Roman",  Font.BOLD,  14 );

        JButton[] buttons = new JButton[5];
        for (int i = 1;  i < 5;  i++ )  {
            String k = "Button" + i;  buttons[i] = new JButton( k );
            buttons[i].setPreferredSize( new Dimension( 100, 40 ) );
            buttons[i].setFont( font1 );  buttons[i].setForeground( Color.blue );
            buttons[i].addActionListener( this ); 
            p1.add ( buttons[i] );
        }
        /* btn.setSize( 100, 100 );  This code don't works */
        p21= new Panel( );
            p21.setLayout ( new GridLayout ( 3, 1 )  );
        Label1 = new Label ( "Name: ", Label.LEFT );
            Label1.setFont( font1 );
            Label1.setForeground( Color.BLUE );
        Label2 = new Label ( "Email: ", Label.LEFT );
            Label2.setFont( font1 );
        Label3 = new Label( "Password: ", Label.LEFT );
            Label3.setFont( font1 );
        p21.add( Label1 );  p21.add( Label2 );  p21.add( Label3 );

        p22 = new Panel( );  
            p22.setLayout ( new GridLayout( 3, 1 ) );
            txt1 = new TextField( 30 );
            txt2 = new TextField( 30 );
            txt3 = new TextField( 30 );
            txt3.setEchoChar( '*' ); 
            p22.add( txt2 );  p22.add( txt1 );  p22.add( txt3 );

        p2 = new Panel( );
            p2.setLayout ( new FlowLayout( ) );
            p2.add( p21 );  p2.add( p22 );

        this.setLayout( new FlowLayout( ) );
        this.add( p1 );  this.add( p2 );
        buttons[1].addActionListener( this ); 
    }
    public void actionPerformed ( ActionEvent e ) {
        /* Method will be automatic called when ActionListener receive action
            from the listened objects */
        try  {
            if ( e.getSource( ) == buttons[1] )  {  
                // If event source is button Add
                txt1.setText( "Phoenix Knight" ); 
            } 
        } 
        catch ( Exception b1 )  {
            System.out.println( "Error");  }
        finally  { }

    //  if ( e.getSource( ) == buttons[1] )  {  
            // If event source is button Add
            //txt1.setText( "Chuong" ); 
    //  } 
    }
    public class MyWindowAdapter extends WindowAdapter  {
        public void windowClosing  ( WindowEvent event )  {
            System.exit( 0 );  /* Default code */
        }
    }  
@SuppressWarnings("deprecation")
    public static void main ( String arg[] )  {
        MyForm f = new MyForm( "Form test !" );
        f.setSize ( 450, 200 );
        f.show( );
    }  
}

您能否告诉我如何解决此错误?

1 个答案:

答案 0 :(得分:1)

我猜您希望电子邮件块中的输出为Phoenix Knight

看起来你的问题很简单:

您在初始化中将按钮声明为JButton

public class TestCode extends Frame implements ActionListener   {
...
public JButton[] buttons;

然后,您在此处创建了一个新的按钮实例并设置了所有值:

JButton[] buttons = new JButton[5];

然后在底部引用buttons[]

if ( e.getSource( ) == buttons[1] )  {  

这是指第一个按钮,您声明为JButton类型但从未初始化。

您需要从

中删除JButton[]部分
JButton[] buttons = new JButton[5];

并将其保留为:

buttons = new JButton[5];