データ管理アプリ
ごくごく簡単なデータ保存アプリです。データはカレントディレクトリにsystem.dataファイルに保存します。
整合性チェックや入力チェックなどが入っていないので
利用時は注意してください!(今後改善してきます!笑)
メインクラス
package application;
import javax.swing.JFrame;
public class MainFrame {
private JFrame frame;
private MenuPanel menuPanel;
public MainFrame() {
this.frame = new JFrame();
this.menuPanel = new MenuPanel();
this.frame.setTitle("データ管理");
this.frame.setBounds(100, 100, 300, 300);
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.frame.getContentPane().setLayout(null);
this.frame.getContentPane().add(menuPanel, null);
this.frame.setVisible(true);
}
public static void main(String args[]) {
new MainFrame();
}
}
トップ画面クラス
package application;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class MenuPanel extends JPanel implements ActionListener {
public MenuPanel() {
this.setBounds(0, 0, 300, 300);
this.setLayout(null);
JButton addBtn = new JButton("追加");
addBtn.setBounds(5, 5, 120, 25);
addBtn.addActionListener(this);
JButton viewBtn = new JButton("表示");
viewBtn.setBounds(130, 5, 120, 25);
viewBtn.addActionListener(this);
this.add(addBtn);
this.add(viewBtn);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand() == "追加") {
System.out.println("追加クリック");
new AddDataPage();
}else if(e.getActionCommand() == "表示") {
System.out.println("表示クリック");
JScrollPane scroll = new JScrollPane(this.viewList());
scroll.setBounds(5, 35, 280, 230);
this.add(scroll);
this.repaint();
}
}
private JTable viewList() {
String colName[] = {"No", "日付", "データ"};
String viewData[][] = new String[31][3];
try {
FileReader file = new FileReader("system.data");
BufferedReader br = new BufferedReader(file);
String tmp;
int i = 0;
while((tmp = br.readLine()) != null) {
String td[] = tmp.split(",");
System.out.println("td[0]=" + td[0] + ",td[1]=" + td[1]);
viewData[i] = new String[]{String.valueOf(i + 1), td[0], td[1]};
i++;
}
br.close();
file.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
JTable tableList = new JTable(viewData, colName);
return tableList;
}
}
データ追加クラス
package application;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class AddDataPage extends JFrame implements ActionListener {
private JPanel panel;
private JTextField dateField;
private JTextField dataField;
public AddDataPage() {
this.setTitle("追加");
this.setBounds(100, 100, 300, 200);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setLayout(null);
this.getContentPane().add(viewAddPanel());
this.setVisible(true);
}
private JPanel viewAddPanel() {
this.panel = new JPanel();
this.panel.setLayout(null);
this.panel.setBounds(0, 0, 300, 200);
JLabel titleLabel = new JLabel("データ登録");
titleLabel.setBounds(5, 5, 200, 30);
JLabel dateLabel = new JLabel("日付");
dateLabel.setBounds(5, 35, 100, 25);
JLabel dataLabel = new JLabel("データ");
dataLabel.setBounds(5, 65, 100, 25);
this.dateField = new JTextField();
this.dateField.setBounds(115, 35, 150, 25);
this.dataField = new JTextField();
this.dataField.setBounds(115, 65, 150, 25);
JButton registBtn = new JButton("登録");
registBtn.setBounds(190, 105, 75, 25);
registBtn.addActionListener(this);
this.panel.add(titleLabel);
this.panel.add(dateLabel);
this.panel.add(dataLabel);
this.panel.add(this.dateField);
this.panel.add(this.dataField);
this.panel.add(registBtn);
return this.panel;
}
public void actionPerformed(ActionEvent ae) {
if(ae.getActionCommand() == "登録") {
System.out.println("登録クリック");
try {
File file = new File("system.data");
FileWriter fw = new FileWriter(file, true);
System.out.println("日付=" + this.dateField.getText() + ",データ=" + this.dataField.getText());
fw.write(this.dateField.getText() + "," + this.dataField.getText() + "\n");
fw.close();
this.setVisible(false);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}