我需要帮助搞清楚为什么我的清晰按钮代码不起作用

时间:2013-12-07 05:31:07

标签: java

我正在为我的Java类开发一个项目,但无法使用我的清除按钮。更具体地说,我在实现Action和ItemListeners时遇到了问题。我的理解是我需要使用ActionListener作为我的清除按钮,但我需要为我的ComboBox使用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("");           
}
}

} 

[/ Code]

3 个答案:

答案 0 :(得分:1)

添加以下行

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

答案 1 :(得分:0)

您需要将actionlistener添加到按钮中。因此,动作事件将被传播给听众,您可以在其中执行操作的逻辑。所以,你必须addActionListener到你的按钮

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

要删除的Onemore行是clearButton.addActionListener(this);来自您执行的操作方法

答案 2 :(得分:0)

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

clearButton.addActionListener(new clrButton()); 

课程clrButton

之前和之外

还可以尝试将这些语句放在课程subButton

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