Results 1 to 12 of 12
Thread: Linking Multiple GUIs
- 04-23-2011, 12:01 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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!
-
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.
- 04-23-2011, 06:27 AM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
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.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(); } }
- 04-23-2011, 08:27 AM #4
Senior Member
- Join Date
- Jan 2011
- Location
- Rizal Province, Philippiines
- Posts
- 167
- Rep Power
- 0
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(); } }
- 04-23-2011, 08:30 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 04-23-2011, 08:34 AM #6
Senior Member
- Join Date
- Jan 2011
- Location
- Rizal Province, Philippiines
- Posts
- 167
- Rep Power
- 0
Assumptions that has been proven that was a working code.
- 04-23-2011, 09:07 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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 09:10 AM.
- 04-23-2011, 07:22 PM #8
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
I appreciate the quick reply and I'll begin redesigning the code now. I really appreciate the help!
- 04-24-2011, 01:36 AM #9
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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);
- 04-24-2011, 01:44 AM #10
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
What happens If you set the visibility in the main GUI constructor?
-
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.
- 04-24-2011, 05:59 AM #12
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
-
Problem with GUIS
By sunde887 in forum New To JavaReplies: 1Last Post: 02-17-2011, 07:18 AM -
NOOB needs help with buttons and events, GUIs.
By simonsays415 in forum New To JavaReplies: 12Last Post: 07-22-2010, 05:02 AM -
Can you create two GUIs?
By SNPTT in forum New To JavaReplies: 4Last Post: 05-18-2010, 11:08 AM -
guis and php
By paddala in forum Advanced JavaReplies: 1Last Post: 02-18-2010, 02:43 AM -
Linking of exe files
By archu2friends in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 02-06-2008, 06:08 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks