Is it normal to put all Java Swing GUIs in one class?
I just started swing development and had problems Is it normal to put the whole GUI into a single class? The application I'm building has a JFrame that displays multiple different "pages" For example, if a user clicks a button, they will be taken to a completely different page with a different layout I have configured the card layout, and the card I have built so far uses the gridbag layout
Well, my question is 1 Should each page have its own class? 2. How do they communicate between the GUI controller running the card layout and the pages? 3. Or I should put all GUIs into the GUI controller and let it run like that
Here's my code so far. I'm a novice and really want to be good at it, so if you find any major problems I missed, please point them out at any time
Code for a single page:
public class HomePage extends JPanel implements ActionListener{
private GridBagLayout gl;
private JPanel frm;
JButton newPersonalContact;
HomePage(){
frm=new JPanel();
gl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
frm.setLayout(gl);
newPersonalContact=new JButton("New Personal Contact");
JButton newBusinessContact=new JButton("New Business Contact");
JButton showAllContacts=new JButton("Show All Contacts");
JButton saveAndQuit=new JButton("Save and Quit");
JPanel top=new JPanel();
top.setBackground(new Color(218,165,32));
top.add(new JLabel("Western Governers University Presents:"));
JPanel middle=new JPanel();
middle.setBackground(new Color(43,37,85));
GridLayout ge=new GridLayout(4,4);
middle.setLayout(ge);
middle.add(new JLabel(""));
middle.add(new JLabel(""));
middle.add(new JLabel(""));
middle.add(new JLabel(""));
middle.add(new JLabel(""));
middle.add(newPersonalContact);
middle.add(newBusinessContact);
middle.add(new JLabel(""));
middle.add(new JLabel(""));
middle.add(showAllContacts);
middle.add(saveAndQuit);
middle.add(new JLabel(""));middle.add(new JLabel(""));
middle.add(new JLabel(""));
middle.add(new JLabel(""));
JPanel bottom=new JPanel();
bottom.setBackground(new Color(218,32));
gbc.fill=GridBagConstraints.BOTH;
gbc.weightx=1.0;
gbc.weighty=2.0;
gbc.gridx=0;
gbc.gridy=0;
frm.add(top,gbc);
gbc.weighty=6.0;
gbc.gridx=0;
gbc.gridy=1;
frm.add(middle,gbc);
gbc.weighty=1.0;
gbc.gridx=0;
gbc.gridy=2;
frm.add(bottom,gbc);
newPersonalContact.addActionListener(this);
newBusinessContact.addActionListener(this);
showAllContacts.addActionListener(this);
saveAndQuit.addActionListener(this);
}
public void actionPerformed(ActionEvent ae){
if (ae.getSource()==newPersonalContact){
//What goes here?
}
}
public JPanel getFrame(){
return frm;
}
}
GUI controller code:
public class GUIController {
JFrame frm;
cardlayout cl;
Container pane;
GUIController(){
frm=new JFrame();
frm.setSize(800,600);
frm.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
JPanel card2=new JPanel();
card2.setBackground(Color.black);
JPanel cards=new JPanel(new cardlayout());
cards.add(new HomePage().getFrame(),"Home");
cards.add(card2,"New Personal Contact");
pane=frm.getContentPane();
pane.add(cards,BorderLayout.CENTER);
}
public void start(){
this.frm.setVisible(true);
}
public void showCard(){
cl.show(pane,"Card2");
}
}
Although this is for the school project, the GUI interface is not just the learning content I am interested in Thanks for your help.
Solution
No, I don't recommend putting all GUI code in class - at least if it gets big and does a few things
In this case, use mediator mode:
>Place the subcomponents in the self defined subclass of JPanel > this subclass listens to the events of its child nodes and updates other child nodes > if necessary, you can trigger custom events
Also note: if you want to learn GUI programming and do not have specific requirements for using swing, consider using the newer GUI toolkit JavaFX
