Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Multiple JFrames

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Multiple JFrames

    Hi,

    I am building a GUI for a small game currently run on the command line. It is a client server game. I currently have two different JFrames in two different methods. One to connect to the server and then another to play the game. Currently I run the first method (guiconnect). The user types hostname,port etc and then clicks connect. Connect passes the variables and starts the game (a third method called clientstart). At this point I want to close the first JFrame and open the next one (in guigame method).

    At the moment. The connect button seems to stay pushed and when the guigame starts no buttons appear. Also none of the gui works after it runs clientstart. However when run separately the buttons are there! Is this because I am running clientstart().

    public static void guigame(){
    		JFrame g = new JFrame("Game");
    		g.setSize(600, 200);
    		g.setLocation(10,10);
    		Container contentgame = new JPanel();
    		JButton mnorth = new JButton("North");
    		contentgame.add(mnorth);
    		mnorth.addActionListener(new java.awt.event.ActionListener(){
    			public void actionPerformed(ActionEvent e){
    				try {
    					processServerMessage("MOVE N");
    				} catch (IOException e1) {
    					// TODO Auto-generated catch block
    					e1.printStackTrace();
    				}
    			}
    		});
    		JButton pickup = new JButton("PICKUP");
    		contentgame.add(pickup);
    		mnorth.addActionListener(new java.awt.event.ActionListener(){
    			public void actionPerformed(ActionEvent e){
    				try {
    					processServerMessage("PICKUP");
    				} catch (IOException e1) {
    					// TODO Auto-generated catch block
    					e1.printStackTrace();
    				}
    			}
    		});
    		g.setContentPane(contentgame);
    		g.pack();
    		g.setVisible(true);
    		g.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    	}
    public static void guiconnect(){
    		final JFrame f = new JFrame("Connect");
    		f.setSize(200, 200);
    		f.setLocation(10,10);
    		Container content = new JPanel();
    		content.setLayout(new GridLayout(4, 2));
    		content.add(new JLabel("Host"));
    		final JTextField host = new JTextField("localhost");
    		content.add(host);
    		content.add(new JLabel("Port"));
    		final JTextField portex = new JTextField("24101");
    		content.add(portex);
    		content.add(new JLabel("Username"));
    		final JTextField username = new JTextField("Player 1");
    		content.add(username);
    		JButton connect = new JButton("Connect");
    		content.add(connect);
    		connect.addActionListener(new java.awt.event.ActionListener(){
    			public void actionPerformed(ActionEvent e) {
    				String address;
    				int port;
    				String name;
    				try {
    					// get address
    					address = getAddress(host.getText());
    					port = getPort(portex.getText());
    					name = getName(username.getText());
    				} catch (IOException e1) {
    					System.out.println("Error reading input");
    					return;
    				}
    				// try connecting
    				System.out.println("Connecting...");
    				try {
    					Socket sock = new Socket(address, port);
    					// we connected, enter main client
    					try {
    						LODClient client = new LODClient(sock.getInputStream(), sock.getOutputStream(), name);
    						guirun();
    						client.start();
    					} finally {
    						sock.close();
    					}
    				} catch (UnknownHostException e1) {
    					System.out.println("Could not find host: " + e1.getMessage());
    				} catch (IOException e1) {
    					System.out.println(e1.getMessage());
    				}
    			}
    		});
    		f.setContentPane(content);
    		f.pack();
    		f.setVisible(true);
    		f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
     
    	}
    	public static void main(String[] args) {
    		guiconnect();
    	}

    Any help appreciated.


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Multiple JFrames

    Recommended reading: (link on next line)
    Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db

Similar Threads

  1. Implementing Multiple Interfaces with Generics
    By darkestfright in forum Collections and Generics
    Replies: 5
    Last Post: February 10th, 2010, 08:44 PM
  2. Multiple Queues
    By fh84 in forum Threads
    Replies: 1
    Last Post: December 3rd, 2009, 02:28 PM
  3. traversing multiple jTabbedPane?
    By dewboy3d in forum AWT / Java Swing
    Replies: 3
    Last Post: October 2nd, 2009, 07:26 PM
  4. Breaking out of multiple loops
    By helloworld922 in forum Loops & Control Statements
    Replies: 4
    Last Post: July 1st, 2009, 02:52 PM
  5. Which interface gives more control on serialization of an object?
    By abhishekraok2003 in forum Java Theory & Questions
    Replies: 1
    Last Post: May 16th, 2009, 10:17 AM