I am currently working on a course project and have been assigned to write a GUI. I've written it and it's about 2000 lines of code. It will be bigger when I add the SQL codes and new panels. So, I've successfully divided it into different classes for each screen. My question is: how do I improve that solution? Here is the first shape of code. I'll just write a small part of code for better clarity.
/**
*When the program starts, first I call passScreen. GUI class extends the JFrame by the way.
*/
public class GUI extends JFrame{
public GUI() {
passScreen();
setSize(700,700);
setResizable(true);
setTitle("KARGO");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
/**
*When a user clicks login button, I simply making panelG invisible, calling client method and
*then removing it. I am not writing user_id and password control codes for the sake of clarity
*/
private void passScreen(){
final JPanel panelG = new JPanel();
...
...
JButton login = new JButton("Login");
login.addActionListener(new ActionListener){
panelG.setVisible(false);
client();
getContentPane().remove(panelG);
panelG.removeAll();
}};
setContentPane(panelG);
}
/**
*When the user clicks exit, I make the current pane invisible, calling
*passScreen method.
*/
private void client(){
final JPanel panelG = new JPanel();
...
...
JButton exit = new JButton();
exit.addActionListener(new ActionListener){
panelG.setVisible(false);
passScreen();
getContentPane().remove(panelG);
panelG.removeAll();
}};
setContentPane(panelG);
}
}
Then, I put passScreen()
and client()
methods to separate classes and modified GUI and the methods slightly:
GUI:
public class GUI extends JFrame{
public GUI() {
new PassScreen().passScreen(this);// I sent current frame object to passScreen method
setSize(700,700);
setResizable(true);
setTitle("KARGO");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
}
PassScreen:
public class PassScreen{
/**
*I made changes and set the content pane via JFrame object coming from GUI class.
*/
public void passScreen(final JFrame g){
final JPanel panelG = new JPanel();
...
...
JButton login = new JButton("Login");
login.addActionListener(new ActionListener){
panelG.setVisible(false);
new Client().client(g);// Sending current frame object to Client class-->
g.getContentPane().remove(panelG);//for changing screen
panelG.removeAll();
}};
g.setContentPane(panelG);
}
}
Client:
public class Client{
public void client(final JFrame g){
final JPanel panelG = new JPanel();
...
...
JButton exit = new JButton();
exit.addActionListener(new ActionListener){
panelG.setVisible(false);
new PassScreen().passScreen(g);
g.getContentPane().remove(panelG);
panelG.removeAll();
}};
g.setContentPane(panelG);
}
}
I don't like it because, for access, I have to make passScreen
and client methods public. Is there any better way of separating GUI parts?