I am very new in making JAVA swing applications..
My problem statement is to make a library management application,for which i designed classes as below.
Please tell me whether my design of classes are right, and possible corrections and improvements.
Requrements:
- Register new students.
- Search with ISBN of book.(Number)
- Borrow a book.
As we can have only 1 JFrame and add and remove panels to it, my idea is to have a singleton class named Frame.java to return a JFrame.
Frame.java
public class Frame extends JFrame {
JFrame app_main_frame=new Frame();
private Frame()
{
}
pubic static getJFrame()
{
return app_main_frame;
}
}
Book.java class:
public class Book {
private String name;
private long int ISBN;
private String author_name;
private boolean borrowed;
private String borrower_id;
public getName()
{
}
public setName(String book_name)
{
name=book_name;
}
// and all other get and set methods are written
}
Library.java ( singleton class)
public class Library {
Library lib = new Library();
private Library()
{
}
public static getLibrary()
{
return lib;
}
public static void Main(String args[])
{
JFrame frame = frame.getJFrame(); // Get JFrame
LoginPanel login = new LoginPanel(); // Get Login Panel object
login_panel=login.getPanel(); // Get Panel of login.
frame.add(login_panel); // Add panel to JFrame
}
private HashMap<int,Book> books;
public Book addBook(long int id,String Name,String author_name)
{
Book book = new Book(id,Name,author_name);
books.put(int,book);
}
public Book find(int id)
{
return books.get(id);
}
}
LoginPanel.java
public class LoginPanel {
JPanel login_panel;
JLabel username,password;
JTextField username_text,password_text;
Button button;
public LoginPanel()
{
login_panel.add(username);
login_panel.add(username_text);
login_panel.add(password);
login_panel.add(password_text);
login_panel.add(button);
button.addActionListener(this);
}
public getPanel()
{
return login_panel;
}
public void ActionPerformed(Event e)
{
JFrame frame = frame.getJFrame(); // Get JFrame
HomePanel home = new HomePanel(); // Get Login Panel object
frame.add(home.getPanel());
}
}
HomePanel.java
public class HomePanel {
JPanel home_panel;
Button b;
JTextField search;
public HomePanel()
{
home_panel.add(b);
home_panel.add(search);
b.addActionListener(this); // add a text field and a button to panel
}
public getPanel()
{
return home_panel;
}
public void ActionPerformed(Event e)
{
String search_text = search.getText();
Library lib = Library.getLibrary();
book b=lib.find(search_text);
// Do something with the book found...
}
}
Please help me improve design of classes.
Thanks in advance. :)
Don't look after compilation errors...didn't compile.
didn't compile.This a bad. You should have a working starting point. – tb- Feb 1 at 19:22As we can have only 1 JFrame. Even if that's true in this case, it isn't in general, and as such you won't find the class useful in the future (and also, Singletons should be final or it can be overridden which defeats the purpose). – MrLore Feb 1 at 19:35JFrameis not a good idea. However using a singleton on aJPanelwould not be a bad idea. You then could keep your JFrame alive the entire time. Have theJPanelhandle its own button presses so that you can just dispose of that panel, and create a new one and set it visible. Let us know. – Robert Snyder Feb 1 at 20:25