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.