I am just starting out with swing development and I have a problem. Is it okay to translate the entire GUI into one class? The application I create has one JFrame that displays several different “pages”. For example, if a user clicks a button, they go to a completely different page with a different layout. I have set up a map layout, and one map that I have created so far uses the GridBag layout.
The question I have is
1. should each page have its own class? 2. If they do, how can I communicate between a GUI controller that launches a map layout and individual pages?
3. Or should I just put the entire graphical interface in a GUI controller and let it work like that.
Below is the code for what I have so far. I am new to this and would really like to get good results from him, so if you notice any serious problems that I missed, feel free to mention them.
Code for a separate 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,165,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){
}
}
public JPanel getFrame(){
return frm;
}
}
for graphics controller:
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 a school project, a GUI is not required just for what interests me. Thanks for any help.