Methapolis  0.27
 All Classes Namespaces Files Functions Variables Enumerator
JoinGameScreen.java
Go to the documentation of this file.
1 package micropolisj.gui;
2 
3 import java.awt.FlowLayout;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6 import java.awt.event.KeyEvent;
7 import java.awt.event.KeyListener;
8 import java.rmi.RemoteException;
9 
10 import javax.swing.JButton;
11 import javax.swing.JFrame;
12 import javax.swing.JTextField;
13 
14 import micropolisj.network.ClientMicropolis;
15 
16 public class JoinGameScreen extends JFrame implements ActionListener {
17  private JButton loginBtn;
18  private JTextField textfield;
19 
20  public JoinGameScreen() {
21  super("Join game");
22 
23  textfield = new JTextField("IPv4 address");
24  loginBtn = new JButton("Login");
25 
26  add(textfield);
27  add(loginBtn);
28 
29  setLayout(new FlowLayout());
30 
31  textfield.addKeyListener(new KeyListener() {
32  @Override
33  public void keyTyped(KeyEvent e) {
34  if(e.getKeyCode() == KeyEvent.VK_ENTER) {
35  joinGame();
36  }
37  }
38 
39  @Override
40  public void keyReleased(KeyEvent e) {
41  }
42 
43  @Override
44  public void keyPressed(KeyEvent e) {
45  if(e.getKeyCode() == KeyEvent.VK_ENTER) {
46  joinGame();
47  }
48  }
49  });
50  loginBtn.addActionListener(this);
51  pack();
52 
53  // center window
54  setLocationRelativeTo(null);
55  setVisible(true);
56  }
57 
58  @Override
59  public void actionPerformed(ActionEvent ev) {
60  joinGame();
61  }
62 
63  private void joinGame() {
64  setVisible(false);
65  try {
66  MainWindow win = new MainWindow(new ClientMicropolis(textfield.getText()));
67  win.setVisible(true);
68  }
69  catch(RemoteException e) {
70  // TODO Auto-generated catch block
71  e.printStackTrace();
72  }
73  closeThis();
74  }
75 
76  private void closeThis() {
77  setVisible(false);
78  dispose();
79  }
80 }