提交按钮不起作用

时间:2015-02-27 06:54:53

标签: jbutton

我的java代码确实编译但是它没有 提交按钮不起作用。到目前为止,这是我的代码;

/**
 * Orchestration class for Account

 */

import java.awt.*; //Importing the java.awt.* class

import java.awt.event.*;//Importing the java.awt.event.* class

import javax.swing.*;//Importing the  javax.swing. class


public class AccountDemo extends JFrame 

{

/// initializes the variables in the container

    String text1;
    String text2;
    String text3;
    String text4;
    String text5;
    String text6;
    JTextField nameText ;
    JTextField addressText ;
    JTextField currentBalanceText ;
    JTextField creditLimitText ;
    JTextField genderText ;
    JLabel nameLabel;
    JLabel addressLabel;
    JLabel currentBalanceLabel;
    JLabel creditLimitLabel;
    JLabel genderLabel;
    JRadioButton getRadioButton1;
    JRadioButton getRadioButton2;
    JButton getJButton;

    /**
     * Constructor
     */
    public AccountDemo()
    {
    super("This is a JFrame");
   final Container con = getContentPane(); 

   //Create window, get content pane


        //Creates buttons, text fields and labels

        nameLabel = new JLabel("Please enter your name");
        nameText = new JTextField(15);

        addressLabel = new JLabel("Please enter your address");
        addressText = new JTextField(15);

        currentBalanceLabel = new JLabel("Please enter your current balance");
        currentBalanceText = new JTextField(15);

        creditLimitLabel= new JLabel("Please enter your credit limit");
        creditLimitText = new JTextField(15); 

        genderLabel = new JLabel("Gender");

        getRadioButton1 = new JRadioButton("Male");

        getRadioButton2 = new JRadioButton("Female");

        getJButton = new JButton("SUBMIT");


        //Sets the layout
        FlowLayout myLayout = new FlowLayout();
        con.setLayout(myLayout);

        //Adds all the created GUI elements to the content pane
        con.add(nameLabel);
        con.add(nameText);

        con.add(addressLabel);
        con.add(addressText);

        con.add(currentBalanceLabel);
        con.add(currentBalanceText);

        con.add(creditLimitLabel);
        con.add(creditLimitText);

        con.add(genderLabel);

        con.add(getRadioButton1);
        con.add(getRadioButton2);

        con.add(getJButton);


        getJButton.addActionListener(new ActionListener() //Gets the text for each button and creates account object using constructor. 
        {
            public void actionPerformed(ActionEvent e)
            {
                text1 = nameText.getText();         
                text2 = addressText.getText();          
                text3 = currentBalanceText.getText();           
                text4 = creditLimitText.getText();
                text5 = genderText.getText() ; 

                System.out.println("Account holders name is " + text1 +"  Account holders address is " + text2  +  " balance = "  + text3 + " credit = " + text4 + " I am " + text5 + " SUBMIT " + text6);



            }
        });
        pack();//Sorts out the layout and tells Java to exit when pressing Close button
        setLocation(210,150);
        setSize(400, 300);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


   }

    public static void main(String [] args)  {

         AccountDemo demoObject = new AccountDemo();
    }

}

1 个答案:

答案 0 :(得分:0)

问题在于您的101行。那个GenderTextField,你从未初始化过。所以指针为空,你得到一个错误。 我注意到你想使用单选按钮,所以我附加了你的新actionListner()代码。它会起作用

        getJButton.addActionListener(new ActionListener() //Gets the text for each button and creates account object using constructor. 
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            text1 = nameText.getText();         
            text2 = addressText.getText();          
            text3 = currentBalanceText.getText();           
            text4 = creditLimitText.getText();
            //text5 = genderText.getText() ; 
            if(getRadioButton1.isSelected()){
                text5 = "Male";
            }
            else{
                text5 = "Female";
            }
            System.out.println("Account holders name is " + text1 +"  Account holders address is " + text2  +  " balance = "  + text3 + " credit = " + text4 + " I am " + text5 + " SUBMIT " + text6);



        }
    });