简体   繁体   中英

I need help figuring out why my clear button code does not work

I am working on a project for my Java class and cannot get my clear button to work. More specifically, I am having an issue implementing Action and ItemListeners. My understanding is that I need to use ActionListener for my clear button, but I need to use ItemListener for my ComboBoxes. I am very new to Java and this is an intro class. I haven't even begun to code the submit button and ComboBoxes and am a little overwhelmed. For now, I could use help figuring out why my clear function will not work. Any help is greatly appreciated. Thanks in advance.

Here is my updated code after suggestions:

import java.awt.*;
import java.applet.*;
import java.awt.event.*; 
import javax.swing.*;
import javax.swing.text.*;

public class cousinsTree extends JApplet

{
Container Panel;
JButton submitButton;
JButton clearButton;
JTextField firstName;
JTextField lastName;
JTextField Address;
JTextField City;
JMenuBar menuBar;
JTextField Total;
JComboBox Service;
JComboBox howOften;
JComboBox numTrees;
LayoutManager setLayout;
String[] TreeList;
String[] numList;
String[] oftenList;


@Override
public void init()
{
    Panel = getContentPane();
    this.setLayout(new FlowLayout());
    TreeList= new String[3];
    TreeList [0] = "Trim";
    TreeList [1] = "Chemical Spray";
    TreeList [2] = "Injection";
    numList = new String[3];
    numList [0] = "0-5";
    numList [1] = "6-10";
    numList [2] = "11 >";
    oftenList = new String[3];
    oftenList [0] = "Monthly";
    oftenList [1] = "Quarterly";
    oftenList [2] = "Annually";     
    Panel.setBackground (Color.green);
    submitButton = new JButton("Submit");
    submitButton.setPreferredSize(new Dimension(100,30));
    clearButton.addActionListener(new clrButton());
    clearButton = new JButton("Clear");
    clearButton.setPreferredSize(new Dimension(100,30));
    firstName = new JTextField("", 10);
    JLabel lblFirstName = new JLabel("First Name");
    lastName = new JTextField("", 10);
    JLabel lblLastName = new JLabel("Last Name");
    Address = new JTextField("", 15);
    JLabel lblAddress = new JLabel("Address");
    City = new JTextField("Columbus", 10);
    JLabel lblCity = new JLabel("City");
    Total = new JTextField("", 10);
    JLabel lblTotal = new JLabel("Total");

    //Service = new TextField("Service (Trim, Chemical Spray, or Injection).", 20);
    JLabel lblService = new JLabel("Service");
    Service=new JComboBox(TreeList);

    JLabel lblhowOften = new JLabel("How often?");
    howOften = new JComboBox(oftenList);

    JLabel lblnumTrees = new JLabel("Number of Trees");
    numTrees = new JComboBox(numList);

/* Configuration */
    //add items to panel
    Panel.add(lblFirstName);
    Panel.add(firstName);
Panel.add(lblLastName);
    Panel.add(lastName);
    Panel.add(lblAddress);
    Panel.add(Address);
    Panel.add(lblCity);
    Panel.add(City);
    Panel.add(lblnumTrees);
    Panel.add(numTrees);
    Panel.add(lblService);
    Panel.add(Service);
    Panel.add(lblhowOften);
    Panel.add(howOften);
    Panel.add(submitButton);
    Panel.add(clearButton);
    Panel.add(lblTotal);
    Panel.add(Total);

    this.setSize(new Dimension(375, 275));
    this.setLocation(0,0);

Service.setSelectedIndex (1);
howOften.setSelectedIndex (1);
numTrees.setSelectedIndex (1);

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        JMenu menuFile = new JMenu("File", true);
            menuFile.setMnemonic(KeyEvent.VK_F);
            menuFile.setDisplayedMnemonicIndex(0);
            menuBar.add(menuFile);

        JMenu menuSave = new JMenu("Save", true);
            menuSave.setMnemonic(KeyEvent.VK_S);
            menuSave.setDisplayedMnemonicIndex(0);
            menuBar.add(menuSave);

        JMenu menuExit = new JMenu("Exit", true);
            menuExit.setMnemonic(KeyEvent.VK_X);
            menuExit.setDisplayedMnemonicIndex(0);
            menuBar.add(menuExit);
}
class clrButton implements ActionListener {
public void actionPerformed(ActionEvent e) {
   // clearButton.addActionListener(this);

     firstName.setText("");
     lastName.setText("");
     Address.setText("");
     City.setText("");           
}
}
class subButton implements ItemListener { 
public void itemStateChanged(ItemEvent e) {
submitButton.addItemListener(this);
Service.addItemListener(this);
numTrees.addItemListener(this);
howOften.addItemListener(this);
}
}
}

I was able to get it to work...Thank you all for your help. I removed the class and it worked. Here is the working code:

[Code]

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class cousinsTree extends JApplet implements ActionListener
{
Container Panel;
JButton submitButton;
JButton clearButton;
JTextField firstName;
JTextField lastName;
JTextField Address;
JTextField City;
JMenuBar menuBar;
JTextField Total;
JComboBox Service;
JComboBox howOften;
JComboBox numTrees;
LayoutManager setLayout;
String[] TreeList;
String[] numList;
String[] oftenList;

public void init()
{
    Panel = getContentPane();
    this.setLayout(new FlowLayout());
    TreeList= new String[3];
    TreeList [0] = "Trim";
    TreeList [1] = "Chemical Spray";
    TreeList [2] = "Injection";
    numList = new String[3];
    numList [0] = "0-5";
    numList [1] = "6-10";
    numList [2] = "11 >";
    oftenList = new String[3];
    oftenList [0] = "Monthly";
    oftenList [1] = "Quarterly";
    oftenList [2] = "Annually";     
    Panel.setBackground (Color.green);
    submitButton = new JButton("Submit");
    submitButton.setPreferredSize(new Dimension(100,30));
    clearButton = new JButton("Clear");
    clearButton.addActionListener(this);
    clearButton.setPreferredSize(new Dimension(100,30));
    firstName = new JTextField("", 10);
    JLabel lblFirstName = new JLabel("First Name");
    lastName = new JTextField("", 10);
    JLabel lblLastName = new JLabel("Last Name");
    Address = new JTextField("", 15);
    JLabel lblAddress = new JLabel("Address");
    City = new JTextField("Columbus", 10);
    JLabel lblCity = new JLabel("City");
    Total = new JTextField("", 10);
    JLabel lblTotal = new JLabel("Total");

    //Service = new TextField("Service (Trim, Chemical Spray, or Injection).", 20);
    JLabel lblService = new JLabel("Service");
    Service=new JComboBox(TreeList);

    JLabel lblhowOften = new JLabel("How often?");
    howOften = new JComboBox(oftenList);

    JLabel lblnumTrees = new JLabel("Number of Trees");
    numTrees = new JComboBox(numList);

/* Configuration */
    //add items to panel
    Panel.add(lblFirstName);
    Panel.add(firstName);
Panel.add(lblLastName);
    Panel.add(lastName);
    Panel.add(lblAddress);
    Panel.add(Address);
    Panel.add(lblCity);
    Panel.add(City);
    Panel.add(lblnumTrees);
    Panel.add(numTrees);
    Panel.add(lblService);
    Panel.add(Service);
    Panel.add(lblhowOften);
    Panel.add(howOften);
    Panel.add(submitButton);
    Panel.add(clearButton);
    Panel.add(lblTotal);
    Panel.add(Total);



    this.setSize(new Dimension(375, 275));
    this.setLocation(0,0);

Service.setSelectedIndex (1);
howOften.setSelectedIndex (1);
numTrees.setSelectedIndex (1);

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        JMenu menuFile = new JMenu("File", true);
            menuFile.setMnemonic(KeyEvent.VK_F);
            menuFile.setDisplayedMnemonicIndex(0);
            menuBar.add(menuFile);

        JMenu menuSave = new JMenu("Save", true);
            menuSave.setMnemonic(KeyEvent.VK_S);
            menuSave.setDisplayedMnemonicIndex(0);
            menuBar.add(menuSave);

        JMenu menuExit = new JMenu("Exit", true);
            menuExit.setMnemonic(KeyEvent.VK_X);
            menuExit.setDisplayedMnemonicIndex(0);
            menuBar.add(menuExit);


}
public void actionPerformed(ActionEvent e) {
    if(e.getSource() == clearButton) {

     firstName.setText("");
     lastName.setText("");
     Address.setText("");
     City.setText("");           
}
}

} 

[/Code]

Add following line

  clearButton.addActionListener(new clrButton());

class clrButton implements ActionListener {
  public void actionPerformed(ActionEvent e) {
  //  clearButton.addActionListener(this); Comment it

  // if(e.getSource() == clearButton){-> this line don't need.

     firstName.setText("");
     lastName.setText("");
     Address.setText("");
     City.setText("");            
 }
}

You need to add actionlistener to your buttons. so, the action events will be propogated to the listener where you do your logic of the action. so, you have to addActionListener to your button

clearButton.addActionListener(new clrButton());

ie in your init() method

clearButton = new JButton("Clear");
clearButton.setPreferredSize(new Dimension(100,30));
//add this below line to add action listener to the button
clearButton.addActionListener(new clrButton());

Onemore line to be removed is clearButton.addActionListener(this); from your action performed method

You are doing good. But just place the following code

clearButton.addActionListener(new clrButton()); 

Before and outside of class clrButton

Also try placing these statements outside of class subButton

submitButton.addItemListener(new subButton());
Service.addItemListener(new anotherClass());
numTrees.addItemListener(new anotherClass());
howOften.addItemListener(new anotherClass());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM