JButton没有禁用

时间:2014-03-26 01:20:54

标签: java swing jbutton actionlistener jtextfield

我的JButton" btnNew"在Employee Constructor中的Employee类中没有禁用,我也不知道为什么。我是Java的新手,非常感谢任何帮助。我认为这是因为我将两个方法命名为actionPerformed,但是他们给了我一个没有相同名称的错误。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

abstract class Employee extends JFrame   {
     private Customers cust;
     public Employee () {
             setTitle("Employee");
             SetLookandFeel();
             setLayout(null);
             setBounds(20,20,700,700);
             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);




             JLabel lblID               = new JLabel("Employee ID:");
             JLabel lblFname            = new JLabel("First Name:");
             JLabel lblLname            = new JLabel ("Last Name:");
             JRadioButton rbMale        = new JRadioButton("Male", false);
             JRadioButton rbFemale      = new JRadioButton("Female", false);
             JLabel lblJob              = new JLabel("Job Title:");
             JLabel lblEmail            = new JLabel("Email Address:");
             JLabel lblLocation         = new JLabel("Location:");
             JLabel lblworkingdays      = new JLabel("Working Days:");
             JLabel lblDesc             = new JLabel ("Job Description:");
             JLabel lblGender           = new JLabel("Gender");
             final JTextArea txtDesc    = new JTextArea(8,40);
             JCheckBox chbxSun          = new JCheckBox("Sunday");
             JCheckBox chbxMon          = new JCheckBox("Monday");
             JCheckBox chbxTue          = new JCheckBox("Tuesday");
             JCheckBox chbxWed          = new JCheckBox("Wednesday");
             JCheckBox chbxThur         = new JCheckBox("Thursday");
             JCheckBox chbxFri          = new JCheckBox("Friday");
             JCheckBox chbxSat          = new JCheckBox("Saturday");
             final JTextField txtID     = new JTextField();
             final JTextField txtFname  = new JTextField();
             final JTextField txtLname  = new JTextField();
             final JTextField txtJob    = new JTextField();
             final JTextField txtEmail  = new JTextField();
             final JTextField txtLocate = new JTextField();
             final JButton btnOpen            = new JButton("Open");
             final JButton btnNew             = new JButton("Add Employee");
             final JButton btnDelete          = new JButton("Delete");
             final JButton btnUpdate          = new JButton("Update");
             final JButton btnCancel          = new JButton("Clear");




             lblID.setBounds(20,20,100,20);
             lblFname.setBounds(20,45,100,20);
             lblLname.setBounds(20,70,100,20);
             lblJob.setBounds(20,95,100,20);
             lblEmail.setBounds(20,120,100,20);
             lblLocation.setBounds(20,145,100,20);
             lblworkingdays.setBounds(20,170,100,20);
             lblDesc.setBounds(20,220,100,20);
             lblGender.setBounds(20,320,100,20);
             txtID.setBounds(150,20,230,25);
             txtFname.setBounds(150,45,300,25);
             txtLname.setBounds(150,70,300,25);
             txtJob.setBounds(150,95,300,25);
             txtEmail.setBounds(150,120,300,25);
             txtLocate.setBounds(150,145,300,25);
             rbMale.setBounds(150,320,100,20);
             rbFemale.setBounds(250,320,100,20);
             txtDesc.setBounds(150,220,300,100);
             chbxMon.setBounds(150,170,100,20);
             chbxTue.setBounds(250,170,100,20);
             chbxWed.setBounds(350,170,100,20);
             chbxThur.setBounds(450,170,100,20);
             chbxFri.setBounds(550,170,100,20);
             chbxSat.setBounds(150,195,100,20);
             chbxSun.setBounds(250,195,100,20);
             btnOpen.setBounds(400,20,80,20);
             btnNew.setBounds(150,345,150,40);
             btnDelete.setBounds(320,345,80,40);
             btnUpdate.setBounds(420,345,80,40);
             btnCancel.setBounds(150,600,80,40);



             Container con = getContentPane();
             con.add(lblID);
             con.add(lblFname);
             con.add(lblLname);
             con.add(lblJob);
             con.add(lblEmail);
             con.add(lblLocation);
             con.add(lblworkingdays);
             con.add(lblDesc);
             con.add(lblGender);
             con.add(txtID);
             con.add(txtFname);
             con.add(txtLname);
             con.add(txtJob);
             con.add(txtEmail);
             con.add(txtLocate);
             con.add(rbMale);
             con.add(rbFemale);
             con.add(txtDesc);
             con.add(chbxMon);
             con.add(chbxTue);
             con.add(chbxWed);
             con.add(chbxThur);
             con.add(chbxFri);
             con.add(chbxSat);
             con.add(chbxSun);
             con.add(btnOpen);
             con.add(btnNew);
             con.add(btnDelete);
             con.add(btnUpdate);
             con.add(btnCancel);

             setVisible(true);     
             btnCancel.addActionListener(new ActionListener() {
                 public void actionPerformed(ActionEvent ae) {
                        txtID.setText(null);
                        txtFname.setText(null);
                        txtLname.setText(null);
                        txtJob.setText(null);
                        txtEmail.setText(null);
                        txtLocate.setText(null);
                        txtDesc.setText(null);

     }});

             btnNew.addActionListener(new ActionListener() {
                 public void actionPerformed(ActionEvent ae) {
                      if (txtID.getText().length() > 0) {
                          btnNew.setEnabled(false);
                      }
                      else {
                          btnNew.setEnabled(true);
                      }

     }});


         }




     private void SetLookandFeel() {
                 try { 
                     UIManager.setLookAndFeel(
                             "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");

                 } catch (Exception exc) {

                 }
             }

     public Customers getCustomers() {
         return cust;
     }

     public void setCustomers (Customers cust) {
         this.cust = cust;
     }

}
class SalesPerson extends Employee {
    public SalesPerson (Customers cust) {
        setCustomers(cust);




    }
}

class OtherStaff extends Employee {

}

class Customers extends Hashtable {


}

abstract class Customer extends JFrame {

    JFrame frame = new JFrame();


    public Customer(){

        String[] days = {"01", "02", "03", "04", "05", "06", "07", "08", "09",
                "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"
                , "21", "22", "23", "24", "25", "26", "27", "28", "29", "30",
                "31"};
        String[] months = {"01", "02", "03", "04", "05", "06", "07", "08", "09"
                ,"10","11", "12"};




        String[] years = {"40","41","42","43","44","45","46","47","48","49","50",
            "51","52","53","54","55","56","57","58","59","60","61","62","63","64"
                ,"65","66","67","68","69","70","71","72","73","74","75","76","77"
                ,"78","79","80","81","82","83","84","85","86","87","88","89","90"
                ,"91","92","93","94","95","96","97","98","99"};


        setTitle("Customer");
        setLayout(null);
        setBounds(20,20,700,700);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel lblID                = new JLabel("Customer ID:");
        JLabel lblFname             = new JLabel("First Name:");
        JLabel lblLname             = new JLabel("Last Name:");
        JLabel lblStreet            = new JLabel("Street:");
        JLabel lblCity              = new JLabel("City:");
        JLabel lblZip               = new JLabel("Zip Code:");
        JLabel lblPhone             = new JLabel("Phone:");
        JLabel lblEmail             = new JLabel("Email:");
        JLabel lblDOB               = new JLabel("DOB DD/MM/YY:");
        JLabel lblPay               = new JLabel("Cash or Credit?");
        JRadioButton rbCash         = new JRadioButton("Cash");
        JRadioButton rbCredit       = new JRadioButton("Credit");
        final JTextField txtFname   = new JTextField();
        final JTextField txtLname   = new JTextField();
        final JTextField txtStreet  = new JTextField();
        final JTextField txtCity    = new JTextField();
        final JTextField txtZip     = new JTextField();
        final JTextField txtPhone   = new JTextField();
        final JTextField txtEmail   = new JTextField();
        JComboBox cbday             = new JComboBox(days);
        JComboBox cbmonth           = new JComboBox(months);
        JComboBox cbyear            = new JComboBox(years);
        final JTextField txtID      = new JTextField();
        JButton btnOpen             = new JButton("Open");
        JButton btnAdd              = new JButton("Add Customer");
        JButton btnDelete           = new JButton("Delete");
        JButton btnUpdate           = new JButton("Update");
        JButton btnCancel           = new JButton("Clear");



        lblID.setBounds(20,20,100,20);
        lblFname.setBounds(20,45,100,20);
        lblLname.setBounds(20,70,100,20);
        lblStreet.setBounds(20,95,100,20);
        lblCity.setBounds(20,120,100,20);
        lblZip.setBounds(20,145,100,20);
        lblPhone.setBounds(20,170,100,20);
        lblEmail.setBounds(20,195,100,20);
        lblDOB.setBounds(20,220,100,20);
        lblPay.setBounds(20,245,100,20);
        txtID.setBounds(150,20,230,25);
        txtFname.setBounds(150,45,300,25);
        txtLname.setBounds(150,70,300,25);
        txtStreet.setBounds(150,95,300,25);
        txtCity.setBounds(150,120,300,25);
        txtZip.setBounds(150,145,300,25);
        txtPhone.setBounds(150,170,300,25);
        txtEmail.setBounds(150,195,300,25);
        cbday.setBounds(150,220,50,20);
        cbmonth.setBounds(200,220,50,20);
        cbyear.setBounds(250,220,50,20);
        rbCash.setBounds(150,245,100,20);
        rbCredit.setBounds(250,245,100,20);
        btnOpen.setBounds(400,20,80,20);
        btnAdd.setBounds(150,270,150,40);
        btnDelete.setBounds(320,270,80,40);
        btnUpdate.setBounds(420,270,80,40);
        btnCancel.setBounds(150,600,80,40);

        Container con = getContentPane();
        con.add(lblID);
        con.add(lblFname);
        con.add(lblLname);
        con.add(lblStreet);
        con.add(lblCity);
        con.add(lblZip);
        con.add(lblPhone);
        con.add(lblEmail);
        con.add(lblDOB);
        con.add(lblPay);
        con.add(txtID);
        con.add(txtFname);
        con.add(txtLname);
        con.add(txtStreet);
        con.add(txtCity);
        con.add(txtZip);
        con.add(txtPhone);
        con.add(txtEmail);
        con.add(cbday);
        con.add(cbmonth);
        con.add(cbyear);
        con.add(rbCash);
        con.add(rbCredit);
        con.add(btnOpen);
        con.add(btnAdd);
        con.add(btnDelete);
        con.add(btnUpdate);
        con.add(btnCancel);

        setVisible(true);



        btnCancel.addActionListener(new ActionListener() {
                 public void actionPerformed(ActionEvent ae) {
                        txtID.setText(null);
                        txtFname.setText(null);
                        txtLname.setText(null);
                        txtCity.setText(null);
                        txtEmail.setText(null);
                        txtStreet.setText(null);
                        txtZip.setText(null);
                        txtPhone.setText(null);

     }});

    }   

}

class CashCustomer extends Customer {

}

class CreditCustomer extends Customer {

}

class Tester {
    public static void main(String[] args) {
       new SalesPerson(new Customers());
       new CashCustomer();


    }
}

1 个答案:

答案 0 :(得分:1)

除了关于使用布局管理器的注释之外,当动作发生在其对象上时,即当单击newBtn时,会发生actionPerformed。我认为你想要的是根据用户对txtID的操作实际启用和禁用此按钮。

要在JTextField上实现侦听器,请查看 Let ActionListener listen for change in JTextField instead of only enter?