Witam serdecznie na razie tworze w sumie szkielet projektu miniBanku .Nie chce pytać się jako tako o kod programu lecz o usprawnienie tego jak wygląda kod programu.
Kod jest trochę topornie napisany i chciałbym uzyskać informacje jak go troszke poprawić,by było łatwiej i sprawniej implementować do niego nowe rzeczy .
Kod pisałem trochę czasu i utknęłem na zmianie sposobu zapisu danych z formularza. Tutaj w kodzie jest to na razie zapis do pliku. W trakcie przerabiania tej funkcjonalności po prostu zacząłem się gubić trochę już w swoim programie,a co dopiero jak będę chciał potem dodać funkcjonalności jak w normalnym banku i zmianie suchej aplikacji na sieciowa.
Proszę o pomoc,chociaż w zarysie jak powinno to wyglądać.
Nie chciałem robić FlowLayout i ustawiałem przyciski i inne metoda setBounds,miałem to zmienić.
//*Klasa Aplikacja *//
package Bank;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Aplikacja extends JFrame implements ActionListener { // tworzenie
// ramki,notatka
// i
// przycisk
private BufferedImage image_g;
private PanelPassword panelPassword;
private PanelBank panelBank;
private PanelRegistration panelRegistration;
private JTextArea notepad;
private JScrollPane scrollPane;
private JButton bLogin;
private JButton bRegister;
JLabel picLabel1;
File imageFile;
public Aplikacja() {
setTitle(" Bank ");
setSize(800, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
notepad = new JTextArea();
notepad.setWrapStyleWord(true);
scrollPane = new JScrollPane(notepad);
scrollPane.setBounds(0, 0, 300, 200);
add(scrollPane);
bLogin = new JButton("Logowanie do bankowosci");
bLogin.setBounds(0, 230, 200, 20);
add(bLogin);
bLogin.addActionListener(this);
bRegister = new JButton("Rejestracja uzytkownika");
bRegister.setBounds(0, 200, 200, 20);
add(bRegister);
bRegister.addActionListener(this);
imageFile = new File("C:/Users/Wiktor/Desktop/pobrane.jpg");
if (image_g == null) {
try {
image_g = ImageIO.read(imageFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // czyta obrazek
picLabel1 = new JLabel(new ImageIcon(image_g)); // to co
// przeczytalismy
// wkladamy do
// obiektu(zdjecie)
// //ikonka
// ,oprawka do
// zdjecia
}
picLabel1.setBounds(0, 250, 150, 100);
add(picLabel1);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Aplikacja gui = new Aplikacja();
gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
gui.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object zrodlo = e.getSource();
if (zrodlo == bLogin) {
if (panelPassword == null) // Jesli nie ma takiego obiektu jak panel
// hasla
// to sie robi
panelPassword = new PanelPassword(this);
panelPassword.setVisible(true); // Jestemy w panelu hasla
panelPassword.setFocus();
if (panelPassword.isOK()) { // Sprawdzamy czy jest OK
notepad.append(panelPassword.getUser() + " , ");
notepad.append(panelPassword.getPassword() + "\n");
}
}
/*
else if (zrodlo == bRegister) {
if (panelBank == null) // Jesli nie ma takiego obiektu jak // panel hasla
panelBank =new PanelBank(this);
panelBank.setVisible(true); // Jestemy w panelu hasla
panelBank.setFocus();
if (panelBank.isOK()) { // Sprawdzamy czy jest OK
notepad.append("Name "+ panelBank.getName() + "\n");
notepad.append("Surname "+ panelBank.getSurname() +"\n");
notepad.append("City "+ panelBank.getCity() + "\n");
}
}
*/
else if (zrodlo == bRegister) {
if (panelRegistration == null) // Jesli nie ma takiego obiektu jak // panel hasla
panelRegistration =new PanelRegistration(this);
panelRegistration.setVisible(true); // Jestemy w panelu hasla
panelRegistration.setFocus();
if (panelRegistration.isOK()) { // Sprawdzamy czy jest OK
notepad.append("Name "+ panelRegistration.getName() + "\n");
notepad.append("Surname "+ panelRegistration.getSurname() +"\n");
notepad.append("City "+ panelRegistration.getCity() + "\n");
}
}
}
}
//Klasa PanelRejestracji
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class PanelRegistration extends JDialog implements ActionListener {
public static final String DRIVER = "org.sqlite.JDBC";
public static final String DB_URL = "jdbc:sqlite:rejestracja.db";
private Connection connect;
private Statement stat;
private JLabel lName, lSurname, lCity, lID_Person, lPassword;
private BufferedWriter bw, bw2;
private JTextField tName, tSurname, tCity, tID_Person;
private JPasswordField tPassword;
private JButton bSave, bCancel;
private boolean okData; // dla OK albo cancel
private String Name, Surname, City, Haslo, Number, Password;
static String Summary, Summary1;
public PanelRegistration(Aplikacja owner) {
super(owner, "Input Data to registration", true); // modalny dialog
setSize(300, 200);
setLayout(null);
lName = new JLabel("Name", JLabel.RIGHT);
lName.setBounds(0, 0, 100, 20);
add(lName);
tName = new JTextField();
tName.setBounds(110, 0, 100, 20);
add(tName);
lSurname = new JLabel("Surname", JLabel.RIGHT);
lSurname.setBounds(0, 20, 100, 20);
add(lSurname);
tSurname = new JTextField();
tSurname.setBounds(110, 20, 100, 20);
add(tSurname);
lCity = new JLabel("City", JLabel.RIGHT);
lCity.setBounds(0, 40, 100, 20);
add(lCity);
tCity = new JTextField();
tCity.setBounds(110, 40, 100, 20);
add(tCity);
lID_Person = new JLabel("Person_ID", JLabel.RIGHT);
lID_Person.setBounds(0, 60, 100, 20);
add(lID_Person);
tID_Person = new JTextField();
tID_Person.setBounds(110, 60, 100, 20);
add(tID_Person);
lPassword = new JLabel("Password", JLabel.RIGHT);
lPassword.setBounds(0, 80, 100, 20);
add(lPassword);
tPassword = new JPasswordField();
tPassword.setBounds(110, 80, 100, 20);
add(tPassword);
bSave = new JButton("OK");
bSave.setBounds(0, 140, 100, 20);
add(bSave);
bSave.addActionListener(this);
bCancel = new JButton("CANCEL");
bCancel.setBounds(150, 140, 100, 20);
add(bCancel);
bCancel.addActionListener(this);
}
public String getName() {
return tName.getText();
}
public String getSurname() {
return tSurname.getText();
}
public String getCity() {
return tCity.getText();
}
public int ID() {
return Integer.parseInt(tID_Person.getText());
}
public boolean isOK() {
return okData;
}
@Override
public void actionPerformed(ActionEvent e) {
Object z = e.getSource();
if (z == bSave) {
okData = true; // po ok zmienna na true
Name = (" ");
Surname = (" ");
City = (" ");
Number = (" ");
Password = (" ");
Name = tName.getText();
Surname = tSurname.getText();
City = tCity.getText();
Number = tID_Person.getText().toString();
Password = new String(tPassword.getPassword());
Summary = ("Name of Client " + Name) + (" Surname " + Surname)
+ (" City " + City) + (" Number " + Number);
Summary1 = (" Client Number " + Number)
+ (" Password " + Password);
String Data = PanelRegistration.Summary;
String Data1 = PanelRegistration.Summary1;
try {
bw2 = new BufferedWriter(new FileWriter(new File(
"C:/Users/Wiktor/Desktop/Pass.txt"), true));
bw = new BufferedWriter(new FileWriter(new File(
"C:/Users/Wiktor/Desktop/Data.txt"), true));
bw.write(Data);
bw.newLine();
bw.close();
bw2.write(Data1);
bw2.newLine();
bw2.close();
System.out.println(" Data saved ");
} catch (IOException E) {
System.out.println(" Error is " + E);
}
} else
okData = false; // po cancel na false;
setVisible(false); // sterowanie przekazywane do ramki
}
public void setFocus() {
// TODO Auto-generated method stub
tName.requestFocusInWindow();
}
}
///Panel Hasla
package Bank;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class PanelPassword extends JDialog implements ActionListener {
private JLabel lUser, lPassword;
private JTextField tUser;
private JPasswordField tPassword;
private JButton bOK, bCancel;
private boolean okData; // dla OK albo cancel
private String user,password;
private String usera,passworda;
public PanelPassword(JFrame ogien) {
super(ogien, "Input Password", true); // modalny dialog
setSize(300, 200);
setLayout(null);
lUser = new JLabel("user", JLabel.RIGHT);
lUser.setBounds(0, 0, 100, 20);
add(lUser);
tUser = new JTextField();
tUser.setBounds(110, 0, 100, 20);
add(tUser);
lPassword = new JLabel("password", JLabel.RIGHT);
lPassword.setBounds(0, 30, 100, 20);
add(lPassword);
tPassword = new JPasswordField();
tPassword.setBounds(110, 30, 100, 20);
add(tPassword);
bOK = new JButton("OK");
bOK.setBounds(0, 100, 100, 20);
add(bOK);
bOK.addActionListener(this);
bCancel = new JButton("CANCEL");
bCancel.setBounds(150, 100, 100, 20);
add(bCancel);
bCancel.addActionListener(this);
}
private String File(String string) {
// TODO Auto-generated method stub
return null;
}
public String getUser() {
return tUser.getText();
}
public String getPassword() {
return new String(tPassword.getPassword());
}
public boolean isOK() {
return okData;
}
public void setFocus() {
tUser.requestFocusInWindow();
}
@Override
public void actionPerformed(ActionEvent e) {
Object z = e.getSource();
if (z == bOK)
{
user=tUser.getText();
password=tUser.getText();
try {
BufferedReader is = new BufferedReader(new FileReader("C:/Users/Wiktor/Desktop/Data.txt"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
okData = true; // po ok zmienna na true
}
else
okData = false; // po cancel na false;
setVisible(false); // sterowanie przekazywane do ramki
}
}
package Bank;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class PanelBank extends JDialog implements ActionListener {
private JLabel lWithdraw, lDeposit;
private BufferedWriter bw;
private JButton tTransaction, tHistory, tInfo;
private JTextField tWithdraw,tDeposit;
private JButton bOK, bCancel,lHistory,lInfo,lTransaction;;
private boolean okData; // dla OK albo cancel
private String Name, Surname, City;
static String Summary;
private String ID;
public PanelBank(Aplikacja owner) {
// TODO Auto-generated constructor stub
super(owner, "Input Data to registration", true); // modalny dialog
setSize(300, 200);
setLayout(null);
lWithdraw = new JLabel("Withdraw", JLabel.RIGHT);
lWithdraw.setBounds(0, 0, 100, 20);
add(lWithdraw);
tWithdraw = new JTextField();
tWithdraw.setBounds(110, 0, 100, 20);
add(tWithdraw);
lDeposit = new JLabel("Deposit", JLabel.RIGHT);
lDeposit.setBounds(0, 30, 100, 20);
add(lDeposit);
tDeposit = new JTextField();
tDeposit.setBounds(110, 30, 100, 20);
add(tDeposit);
lTransaction = new JButton("Transaction");
lTransaction.setBounds(0, 50, 150, 20);
add(lTransaction);
lHistory = new JButton("History");
lHistory.setBounds(0, 70, 100, 20);
add(lHistory);
lInfo = new JButton("Info");
lInfo.setBounds(0, 90, 100, 20);
add(lInfo);
bOK = new JButton("Make");
bOK.setBounds(0, 200, 100, 20);
add(bOK);
bOK.addActionListener(this);
bCancel = new JButton("CANCEL");
bCancel.setBounds(150, 100, 100, 20);
add(bCancel);
bCancel.addActionListener(this);
}
public String getName() {
return tWithdraw.getText();
}
public String getSurname() {
return tDeposit.getText();
}
public String getCity() {
return tTransaction.getText();
}
public boolean isOK() {
return okData;
}
@Override
public void actionPerformed(ActionEvent e) {
Object z = e.getSource();
if (z == bOK) {
okData = true; // po ok zmienna na true
}
else
okData = false; // po cancel na false;
setVisible(false); // sterowanie przekazywane do ramki
}
public void setFocus() {
// TODO Auto-generated method stub
tWithdraw.requestFocusInWindow();
}
}