0
私は、ユーザーが入力したデータを格納して取得する簡単なEmployeeクラスを作成しました。保存部分については、ファイルを保存するために別のクラスを作成しました。従業員クラスの内部で読み書き機能を作成する必要がありますか、それともそれらを独自のクラスとして保持する必要がありますか?保存ボタンの作成方法
はここにGUIのための私のコードです:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class EmployeeGui2 extends JFrame{
private Employee employee;
private FileRead read;
private FileWrite write;
private JButton storeEmployee;
private JButton retrieveEmployee;
private JTextField field;
private static final int FRAME_WIDTH = 500;
private static final int FRAME_HEIGHT = 500;
public EmployeeGui2(){
createComponents();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
private void createComponents(){
storeEmployee = new JButton("Store");
storeEmployee.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try {
write.write(employee);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
retrieveEmployee = new JButton("Retrieve");
field = new JTextField(20);
JPanel panel = new JPanel();
panel.add(storeEmployee);
panel.add(retrieveEmployee);
panel.add(field);
add(panel);
}
public static void main(String[] args){
JFrame frame = new EmployeeGui2();
frame.setTitle("Employee GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}