focusGained和focusLost不工作​​?

时间:2017-06-02 18:41:06

标签: java focuslistener

我想创建一个程序,允许用户添加到文件,搜索,删除和加载动物园中的动物文件以及有关它们的信息。按钮删除和搜索到目前为止都没有用,所以不要理会它们。问题是teamsfocusGained似乎不起作用。我尝试了迄今为止找到的每个解决方案,但我无法修复它们。所以,所有的帮助表示赞赏。如果您对如何编写搜索和删除类有任何想法,请建议!

这是主程序:

focusLost

我划分了程序,所以类GUI_ADD就在这里:

        package GUI_Try;

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


public class GUI_ZOO extends JFrame{

    ArrayList <ANIMAL> animal= new ArrayList<ANIMAL>();
    TextField name,ID,country;
    Button  add = new Button("add"),
            save = new Button("save"),
            //delete = new Button("delete"),
            //search = new Button("search"),
            load = new Button("load");
    String sName="",sID="",sCountry="";
    JPanel pc;
    JScrollPane scrPanName;
    JTextArea text; 


    GUI_ZOO(){
        super("Zoo \"Sofia\" ");
        setBounds(80, 80, 600, 500);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLayout(new BorderLayout());

        GUI_DELETE delObj = new GUI_DELETE(); 
        GUI_SEARCH searchObj = new GUI_SEARCH();
        GUI_ADD addObj = new GUI_ADD(sName,name,sID,ID,text,animal,sCountry,country);

        pc = new JPanel();
        pc.setLayout(new FlowLayout());
        text = new JTextArea(20,10);

        scrPanName = new JScrollPane(text);

        name = new TextField("Name",15);
        country = new TextField("Country",15);
        ID = new TextField("ID",5);
        text.setEditable(false);

        pc.add(name);
        pc.add(ID);
        pc.add(country);
        pc.add(add);
        pc.add(save);
        pc.add(load);
        pc.add(search);
        pc.add(delete);

        ID.addActionListener(addObj);
        name.addActionListener(addObj);
        country.addActionListener(addObj);
        add.addActionListener(addObj);

        save.addActionListener(new Save());
        load.addActionListener(new Load());
        delete.addActionListener(delObj);
        search.addActionListener(searchObj);

        name.addFocusListener(new Focus()) ;   
        country.addFocusListener(new Focus()) ;  
        ID.addFocusListener(new Focus()) ;  

        add("North",pc);
        add("Center",scrPanName);
        setVisible(true);
    }

    class Focus implements FocusListener{

        public void focusGained (FocusEvent e){
            try{
                TextField src = (TextField)e.getSource();
                if(src.getText().equals("Name") ){
                    name.setText("");
                }
                if(src.getText().equals("ID") ){
                    ID.setText("");
                }
                if(src.getText().equals("Country") ){
                    country.setText("");
                }
            }

            catch(ClassCastException ignored){

            }
        }

        public void focusLost (FocusEvent e){
            try{
                TextField src = (TextField)e.getSource();
                if(src.getText().equals("") ){
                    name.setText("Name");
                }
                if(src.getText().equals("") ){
                    ID.setText("ID");
                }
                if(src.getText().equals("") ){
                    country.setText("Country");
                }
            }

            catch(ClassCastException ignored){

            }
        }
    }


            class Save implements ActionListener{
                public void actionPerformed(ActionEvent e){
                    try{
                        ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("zoo.dat"));
                        os.writeObject(animal);
                        os.close();
                    }
                    catch(FileNotFoundException ex){
                        System.out.println("Can't save the file zoo.dat");
                    }
                    catch(IOException ex){}
                }
            }

            class Load implements ActionListener{
                public void actionPerformed(ActionEvent e){
                    try{
                        ObjectInputStream os = new ObjectInputStream(new FileInputStream("zoo.dat"));
                        animal = (ArrayList<ANIMAL>) os.readObject();
                        os.close();
                        text.setText("");
                        for(ANIMAL a:animal){
                            text.append(a.name+"\t"+a.ID+"\t"+a.country+"\n");
                        }
                    }
                    catch(FileNotFoundException ex){
                        System.out.println("Can't find the file zoo.dat");
                    }
                    catch(IOException ex){
                        ex.printStackTrace();//?
                    } 
                    catch (ClassNotFoundException e1) {
                        e1.printStackTrace();//?
                    }
                }
            }

            public static void main(String arg[]){
                new GUI_ZOO();
            }
        }

这是界面:

 package GUI_Try;

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

public class GUI_ADD implements ActionListener{

    String sNamel;
    TextField namel;
    String sIDl;
    TextField IDl;
    String sCountryl;
    TextField countryl;
    JTextArea textl;
    ArrayList <ANIMAL> animall;

    public GUI_ADD(String sName,TextField name,String sNumber,TextField ID,
            JTextArea text,ArrayList <ANIMAL> animal,String sCountry,TextField country){

        sNamel = sName;
        sCountryl = sCountry;
        IDl = ID;
        namel = name;
        sIDl = sNumber;
        countryl = country;
        textl = text;
        animall = animal;
    }


    public void actionPerformed(ActionEvent e){
        sNamel=namel.getText(); 
        sIDl=IDl.getText();
        sCountryl=countryl.getText();
        if((sNamel.length()==0)||(sNamel=="Name")){
            JOptionPane.showMessageDialog(null, "Type a name, please.");
            return;
        }
        if(sIDl.length()==0){
            JOptionPane.showMessageDialog(null, "Type an ID, please.");
            return;
        }
        if(sCountryl.length()==0){
            JOptionPane.showMessageDialog(null, "Type a country of origin, please.");
            return;
        }
        animall.add(new ANIMAL(sNamel,sIDl,sCountryl));
        textl.append(sNamel+"\t"+sIDl+"\t"+sCountryl+"\n");
        namel.setText("");
        IDl.setText("");
        countryl.setText("");
    }
}

2 个答案:

答案 0 :(得分:0)

您的问题(众多问题之一)是混合AWT和Swing组件。

您创建的名称,ID和国家/地区为TextField,然后尝试在JTextField收听者中将其转换为Focus。但是,你可以通过压制它来防范ClassCastException,所以你甚至都看不到这个问题的证据了。

try {
    JTextField src = (JTextField) e.getSource();
    // ... omitted
} catch(ClassCastException ignored) {
    // No-op
}

您希望(需要)在Swing程序中使用JTextFieldJButton Swing对象,而不是AWT对象。

focusLost方法的其他问题。如果失去焦点的JTextField为空,则会覆盖您的nameIDcountry文字字段...而不仅仅是空白字段!

答案 1 :(得分:0)

您不需要在这两个课程中implements FocusListener。这里多余。

public class GUI_ZOO extends JFrame implements FocusListener{

class Focus implements FocusListener{

FocusGainedFocusLost的原因无效:

TextField name,ID,country;

您使用过java.awt.TextField

JTextField src = (JTextField)e.getSource();

但您正试图从JTextField获取来源。在你的程序中,你没有任何JTextField的权利!

将其更改为:

TextField src = (TextField)e.getSource();