簡體   English   中英

在ActionListener上找不到符號錯誤

[英]Symbol not Found error on ActionListener

import javax.swing .*;    
import java.awt.event.ActionListener;    
import java.awt.event.ActionEvent;   
import java.awt.*;    
import javax.swing.JOptionPane;   
import java.io.FileWriter;    
class Myclass extends JFrame  
{
    Myclass()
    {
    setLayout(null);
    setLayout(new FlowLayout());
    //setBounds(50,50,700,700);
    JLabel lfname=new JLabel("Name ");
    JLabel llname=new JLabel("Lastname ");
    JLabel lmidname=new JLabel("Middlename ");
    JTextField tname=new JTextField(15);
    JTextField tmidname=new JTextField(15);
    JTextField tlastname=new JTextField(15);
    add(lfname);add(tname);
    add(lmidname);add(tmidname);
    add(llname);add(tlastname);
    JButton addrec=new JButton("Add Record");
    JButton delrec=new JButton("Delete Record");
    GridBagConstraints g=new GridBagConstraints();

    JPanel p=new JPanel(new GridLayout(1,2,20,20));

    add(p);

    p.add(addrec);
    p.add(delrec);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    /*tname.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent oe)
            {
                System.out.println("Text="+tname.getText());
            }


});*/
    //tname.addActionListener(obj);
JButton save=new JButton("Save");
MyListener obj=new MyListener();
add(save);
save.addActionListener(obj);

    setVisible(true);



}


public static void main(String args[])
{
    new Myclass();


 }

class MyListener implements ActionListener
 {

    public void actionPerformed(ActionEvent oe)
    {

            //if(ae.getSource().equals(calRect))
            if(oe.getSource().equals(save))
                {
                FileWriter fw=null;
                try{
                        fw=new FileWriter("c:\\users\\dell\\desktop\\File.txt");
                    }catch(Exception e){}

                }
    }

}

我試圖通過在Mylistener類上實現ActionListener來添加名為Mylistener的自定義偵聽器
我不知道這段代碼有什么問題,但是在actionPerformed()中,if條件開始的地方是錯誤的位置。 誰能糾正mycode?

Error is Layout.java:69: error: cannot find symbol
                                if(oe.getSource().equals(save))

                                                     ^
symbol:   variable save
location: class MyListener
1 error

您的保存按鈕未在偵聽器類中定義。 您必須將其作為參數添加到構造函數中:

class MyListener implements ActionListener {

  private JButton save;
  public MyListener(JButton save){
    this.save = save;
  }
  public void actionPerformed(ActionEvent oe) {

    // if(ae.getSource().equals(calRect))
    if (oe.getSource().equals(save)) {
      FileWriter fw = null;
      try {
        fw = new FileWriter("c:\\users\\dell\\desktop\\File.txt");
      } catch (Exception e) {}

    }
  }


//tname.addActionListener(obj);
JButton save=new JButton("Save");
MyListener obj=new MyListener();
add(save);
save.addActionListener(obj);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM