私は、ユーザーが動物園の動物のファイルやその情報をファイルに追加、検索、削除、読み込みできるようにするプログラムを作りたかったのです。これまでのボタンは削除され、検索は機能しないので、注意を払う必要はありません。問題はfocusGained
とfocusLost
が機能していないようです。私は今までに見つけたすべての解決策を試しましたが、修正できませんでした。だから、すべての助けに感謝します。検索と削除のクラスを書く方法についてご意見がありましたら、お勧めします。focusGainedとfocusLostは機能しませんか?
これは、メインプログラムである:
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("");
}
}
そして、これはインタフェースである:クラス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.io.*;
class ANIMAL implements Serializable {
String name, ID, country;
public ANIMAL(String name, String ID, String country){
this.name = name;
this.ID = ID;
this.country = country;
}
}
あなたが "動作しない" とはどういう意味ですか?また、[検索]や[削除]ボタンを見る人がいなくても、なぜそれらを質問に含めるのですか?[mcve]の作成方法を学んでください。代わりに、コードから削除して、問題を小さくし、理解しやすくして助けてください。 – AJNeufeld
なぜAWTとSwingをミックスしていますか? Swingプログラムの作成をお考えの場合は、 'Button'と' TextField'を使用せず、 'JButton'と' JTextField'を使用してください。 – AJNeufeld
答えが無関係になるので、質問全体を変更しないでください。代わりに新しい質問をしてください – Blasanka