繁体   English   中英

我需要帮助弄清楚为什么清除按钮代码不起作用

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

我正在为我的Java类开发项目,但是无法使我的清除按钮起作用。 更具体地说,我在实现Action和ItemListeners时遇到问题。 我的理解是,我需要为清除按钮使用ActionListener,但需要为ComboBoxes使用ItemListener。 我对Java非常陌生,这是入门课程。 我什至还没有开始编写“提交”按钮和ComboBoxes的代码,这有点不知所措。 现在,我可以借助帮助弄清楚为什么清除功能无法正常工作。 任何帮助是极大的赞赏。 提前致谢。

这是我在建议后更新的代码:

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);
}
}
}

我能够使它正常工作...谢谢大家的帮助。 我删除了班级,它起作用了。 这是工作代码:

[码]

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("");           
}
}

} 

[/码]

添加以下行

  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("");            
 }
}

您需要在按钮上添加动作监听器。 因此,动作事件将传播到侦听器,您可以在其中执行动作逻辑。 因此,您必须在按钮上添加ActionListener

clearButton.addActionListener(new clrButton());

即在您的init()方法中

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());

要删除的另一行是clearButton.addActionListener(this); 从你的动作执行方法

你做得很好。 但是只需放置以下代码

clearButton.addActionListener(new clrButton()); 

clrButton之前和之外

还要尝试将这些语句放在class subButton

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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