Results 1 to 12 of 12
  1. #1
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default Linking Multiple GUIs

    I'm working on a simple educational game based on genetics. Its a group project and my partner designed the main GUIs. The game has multiple GUIs which link to one another but because he didn't know how to pass control from one GUI class to another he essentially made a single GUI class and just manipulated the visibility of the different components. This works, but its fairly difficult to work with.

    My question is, how do you pass control between GUI classes and do you think re-designing the program into multiple separate GUI classes would be a better option than keeping them together.

    Thanks for any help!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    In my mind this is a general design issue more than a GUI-specific issue. You want one class to be responsible for one thing and one thing only, be it a database integration task, a file I/O task, or a specific GUI task, so yeah, I think that refactoring is in order.

    You would probably have a master class, perhaps a control class that would control all and have references to any pertinent GUI classes.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Like fubar said, it would work best to have one GUI launch when the main method is entered and then when you want it to switch you would launch the new GUI similar to how you did it in the main method.

    Java Code:
    public class MainGUI{
      MainGUI(){
        JButton x = new JButton("hi");
        x.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e){
            GUI2 g = new GUI2();
          }
        });
      }
      public static void main(String[] args){
        MainGUI mg = new MainGUI();
      }
    }
    There are plenty of assumptions here and this isn't a compilable example but this is a way to allow you to open up a new GUI when the button named x is clicked.

  4. #4
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Like fubar said, it would work best to have one GUI launch when the main method is entered and then when you want it to switch you would launch the new GUI similar to how you did it in the main method.

    Java Code:
    public class MainGUI{
      MainGUI(){
        JButton x = new JButton("hi");
        x.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e){
            GUI2 g = new GUI2();
          }
        });
      }
      public static void main(String[] args){
        MainGUI mg = new MainGUI();
      }
    }
    There are plenty of assumptions here and this isn't a compilable example but this is a way to allow you to open up a new GUI when the button named x is clicked.
    THE M
    Java Code:
    public class MainGUI{
      MainGUI(){
        JButton x = new JButton("hi");
        x.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e){
            GUI2 g = new GUI2();
             g.setVisible(true);
          }
        });
      }
      public static void main(String[] args){
        MainGUI mg = new MainGUI();
      }
    }

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Haha, as I said, lots of assumptions here, one of them being that the frame is set to visible in the constructor. The real point is to show the op the concept. Create each GUI separately and link them through action listeners.

  6. #6
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    Assumptions that has been proven that was a working code.

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    To nitpick, it still doesn't work, all the code does is create a button, it never creates a frame, panel, or adds anything into them. It also doesn't define the extra GUI class.
    Last edited by sunde887; 04-23-2011 at 10:10 AM.

  8. #8
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    I appreciate the quick reply and I'll begin redesigning the code now. I really appreciate the help!

  9. #9
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    Can someone explain to me why nothing happens when i run this code? No errors, just runs for about a second then stops. Nothing appears, nothing shows up.

    package evolve;

    import java.awt.BorderLayout;
    import javax.swing.JApplet;

    public class MainGUI extends JApplet {

    MainMenu mainMenu;

    public MainGUI() {
    setLayout(new BorderLayout());

    MainMenu mainMenu = new MainMenu(this);

    add(mainMenu);

    mainMenu.setVisible(true);
    }

    public static void main(String[] args) {

    MainGUI main = new MainGUI();

    }
    }


    package evolve;

    import javax.swing.*;
    import java.awt.BorderLayout;
    import java.awt.image.*;
    import java.awt.event.*;
    import java.io.*;

    public class MainMenu extends JPanel implements MouseListener {

    ImageBackgroundPanel imp;
    BufferedImage image;
    MainGUI parent;

    MainMenu(MainGUI x) {

    parent = x;
    setLayout(new BorderLayout());
    setSize(631, 489);
    setVisible(true);
    image = null;

    try {
    image = javax.imageio.ImageIO.read(new File("src\\ProteusMainMenu.jpg"));
    } catch (IOException e) {
    e.printStackTrace();
    }

    imp = new ImageBackgroundPanel(image);
    add(imp, BorderLayout.CENTER);
    }

    public void mousePressed(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}

    public void mouseClicked(MouseEvent e) {

    int x = e.getX();
    int y = e.getY();

    GameMenu gameMenu = new GameMenu(parent);
    gameMenu.setVisible(true);
    setVisible(false);
    }
    }


    package evolve;

    import javax.swing.*;
    import java.awt.BorderLayout;
    import java.awt.image.*;
    import java.awt.event.*;
    import java.io.*;

    public class GameMenu extends JPanel implements MouseListener {

    ImageBackgroundPanel imp;
    BufferedImage image;
    MainGUI parent;

    GameMenu(MainGUI x) {

    parent = x;
    setLayout(new BorderLayout());
    setSize(631, 489);
    setVisible(true);
    image = null;

    try {
    image = javax.imageio.ImageIO.read(new File("src\\ProteusGameMenu.jpg"));
    } catch (IOException e) {
    e.printStackTrace();
    }

    imp = new ImageBackgroundPanel(image);
    add(imp, BorderLayout.CENTER);
    }

    public void mousePressed(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}

    public void mouseClicked(MouseEvent e) {

    int x = e.getX();
    int y = e.getY();

    parent.mainMenu.setVisible(true);
    setVisible(false);
    }
    }

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What happens If you set the visibility in the main GUI constructor?

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    If you're creating an applet you need to do so in an init method, not a main method as your main is never called. You will want to read the Oracle Java tutorials on how to create and run applets for the details.

  12. #12
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    You are extending JApplet so this must be run as an applet, to do so listen to Fubarable.. If you want to run this as a normal desktop app, you need to extend JFrame.

Similar Threads

  1. Problem with GUIS
    By sunde887 in forum New To Java
    Replies: 1
    Last Post: 02-17-2011, 08:18 AM
  2. NOOB needs help with buttons and events, GUIs.
    By simonsays415 in forum New To Java
    Replies: 12
    Last Post: 07-22-2010, 06:02 AM
  3. Can you create two GUIs?
    By SNPTT in forum New To Java
    Replies: 4
    Last Post: 05-18-2010, 12:08 PM
  4. guis and php
    By paddala in forum Advanced Java
    Replies: 1
    Last Post: 02-18-2010, 03:43 AM
  5. Linking of exe files
    By archu2friends in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-06-2008, 07:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •