Hey Leute,
haben hier nen "kleines Problem" und kommen nicht weiter
Geht um die Listener zu ner SWING GUI die wir mit WindowBuilder erstellt haben.
Key Listener, wie auch ActionPerformed Listener bleiben ohne Wirkung. Unter SWT haben
wir das Problem nicht, daher denke ich mal ist es was SWING spezifisches...
Controller:
GUI:
Grüße
haben hier nen "kleines Problem" und kommen nicht weiter
Geht um die Listener zu ner SWING GUI die wir mit WindowBuilder erstellt haben.
Key Listener, wie auch ActionPerformed Listener bleiben ohne Wirkung. Unter SWT haben
wir das Problem nicht, daher denke ich mal ist es was SWING spezifisches...
Controller:
Java:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.eclipse.swt.widgets.Display;
import uni.kassel.kira.gui.GameGUI;
import uni.kassel.kira.logic.ServerConnection;
import uni.kassel.kira.logic.ServerConnectionIngame;
import uni.kassel.kira.model.ingame.CSModel;
import uni.kassel.kira.model.lobby.User;
import uni.kassel.kira.model.message.MessageEvent;
import uni.kassel.kira.model.message.MessageListener;
import uni.kassel.kira.model.message.PublicMessage;
import uni.kassel.kira.model.message.TeamMessage;
public class GameController {
private GameGUI gameScreen;
private CSModel csm;
private ServerConnectionIngame sci;
private ServerConnection sc;
private MessageListener ml;
private KeyListener kl;
private ActionListener al;
public GameController(ServerConnection sc, ServerConnectionIngame sci, CSModel csm){
this.csm = csm;
this.sci = sci;
this.sc = sc;
}
public void start() {
Display display = Display.getDefault();
gameScreen = new GameGUI();
//TODO GameGUI starten
//String[] gameName = new String[1];
//gameName[0] = csm.getGame().getName();
GameGUI.main(null);
registerListeners();
while (!gameScreen.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
//Listeners bei dispose abmelden etc
removeListeners();
}
public void registerListeners() {
sc.addMessageListener(ml = new MessageListener() {
public void messageReceived(final MessageEvent e) {
final Display display = Display.getDefault();
display.asyncExec(new Runnable() {
@Override
public void run() {
JTextPane jtp = gameScreen.getTxtPnChat();
PublicMessage msg = e.getMessage();
String message = msg.getMessage().replaceAll("Ae", "Ä").replaceAll("ae", "ä").replaceAll("Oe", "Ö").replaceAll("oe", "ö").replaceAll("Ue", "Ü").replaceAll("ue", "ü").replaceAll("ss", "ß");
String msgLine = String.format("%s %s: %s", msg.getTimeStamp().toString().substring(11, 19), msg.getFrom(), message);
jtp.setText(jtp.getText().length() == 0 ? msgLine : jtp.getText() + "\r\n" + msgLine);
}});
}
});
gameScreen.getBtnSendMsg().addActionListener(al = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
User usr = sc.getCurrentUser();
JTextField txtLine = gameScreen.getTxtChat();
if(e.getSource() == gameScreen.getBtnSendMsg() && !txtLine.getText().trim().equals("")) {
sc.sendMessage((TeamMessage) new TeamMessage().withTo("Team-F").withFrom(usr.getNick()).withMessage(txtLine.getText()));
}
txtLine.setText("");
}
});
gameScreen.getTxtChat().addKeyListener(kl = new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
User usr = sc.getCurrentUser();
JTextField txtLine = gameScreen.getTxtChat();
if ((key == 13 || key == 16777296) && !txtLine.getText().trim().equals("")) {
sc.sendMessage((TeamMessage) new TeamMessage().withTo("Team-F").withFrom(usr.getNick()).withMessage(txtLine.getText()));
}
txtLine.setText("");
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
});
gameScreen.getBtn_Basictower().addActionListener(new ActionListener () {
@Override
public void actionPerformed(ActionEvent arg0) {
sci.chooseTile(csm.getTileByPosition("TOP_LEFT"), false);
System.out.println("button1");
}
});
gameScreen.getBtn_Slowtower().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sci.startGame();
System.out.println("button2");
}
});
}
public void removeListeners() {
sc.removeMessageListener(ml);
gameScreen.getTxtChat().removeKeyListener(kl);
gameScreen.getBtnSendMsg().removeActionListener(al);
}
}
GUI:
Java:
import java.awt.EventQueue;
public class GameGUI {
private boolean disposed = false;
public boolean isDisposed() {
return disposed;
}
private JFrame frame;
private JTextField txtChat;
private JTextPane txtpn_Chat;
private JButton btnSendMsg;
private JButton btn_Basictower;
private JButton btn_Slowtower;
private JButton btn_Splashtower;
private JButton btn_Rockettower;
private JButton btn_Speedtower;
private JButton btn_Ultimatetower;
public JTextField getTxtChat() {
return txtChat;
}
public JTextPane getTxtPnChat() {
return txtpn_Chat;
}
public JButton getBtnSendMsg() {
return btnSendMsg;
}
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Throwable e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GameGUI window = new GameGUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
@Override
protected void finalize() throws Throwable {
// TODO Auto-generated method stub
disposed = true;
super.finalize();
}
/**
* Create the application.
*/
public GameGUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 936, 750);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu mnSpiel = new JMenu("Spiel");
menuBar.add(mnSpiel);
JMenuItem mntmNewMenuItem = new JMenuItem("Hilfe");
mnSpiel.add(mntmNewMenuItem);
JMenuItem mntmSpielVerlassen = new JMenuItem("Spiel verlassen");
mnSpiel.add(mntmSpielVerlassen);
frame.getContentPane().setLayout(null);
JPanel panel_Bar = new JPanel();
panel_Bar.setBounds(662, 11, 250, 672);
frame.getContentPane().add(panel_Bar);
panel_Bar.setLayout(null);
JPanel panel_Tower = new JPanel();
panel_Tower.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
panel_Tower.setBounds(0, 0, 250, 117);
panel_Bar.add(panel_Tower);
panel_Tower.setLayout(null);
btn_Slowtower = new JButton("1");
btn_Slowtower.setBounds(133, 11, 45, 45);
panel_Tower.add(btn_Slowtower);
btn_Splashtower = new JButton("1");
btn_Splashtower.setBounds(195, 11, 45, 45);
panel_Tower.add(btn_Splashtower);
btn_Rockettower = new JButton("1");
btn_Rockettower.setBounds(68, 67, 45, 45);
panel_Tower.add(btn_Rockettower);
btn_Speedtower = new JButton("1");
btn_Speedtower.setBounds(133, 67, 45, 45);
panel_Tower.add(btn_Speedtower);
btn_Ultimatetower = new JButton("1");
btn_Ultimatetower.setBounds(195, 67, 45, 45);
panel_Tower.add(btn_Ultimatetower);
JTextPane txtpnTower = new JTextPane();
txtpnTower.setEnabled(false);
txtpnTower.setEditable(false);
txtpnTower.setText("Tower");
txtpnTower.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 13));
txtpnTower.setBounds(2, 2, 56, 20);
panel_Tower.add(txtpnTower);
btn_Basictower = new JButton("1");
btn_Basictower.setBounds(68, 11, 45, 45);
panel_Tower.add(btn_Basictower);
JPanel panel_Creep = new JPanel();
panel_Creep.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
panel_Creep.setBounds(0, 117, 250, 192);
panel_Bar.add(panel_Creep);
panel_Creep.setLayout(null);
JTextPane txtpnCreeps = new JTextPane();
txtpnCreeps.setEnabled(false);
txtpnCreeps.setEditable(false);
txtpnCreeps.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 13));
txtpnCreeps.setText("Creeps");
txtpnCreeps.setBounds(2, 2, 56, 20);
panel_Creep.add(txtpnCreeps);
JButton btn_Mercury = new JButton("1");
btn_Mercury.setBounds(65, 11, 40, 40);
panel_Creep.add(btn_Mercury);
JButton btn_Mako = new JButton("1");
btn_Mako.setBounds(110, 11, 40, 40);
panel_Creep.add(btn_Mako);
JButton btn_FastNova = new JButton("1");
btn_FastNova.setBounds(155, 11, 40, 40);
panel_Creep.add(btn_FastNova);
JButton btn_LargeManta = new JButton("1");
btn_LargeManta.setBounds(200, 11, 40, 40);
panel_Creep.add(btn_LargeManta);
JButton btn_BigToucan = new JButton("1");
btn_BigToucan.setBounds(200, 56, 40, 40);
panel_Creep.add(btn_BigToucan);
JButton btn_SpeedyRaider = new JButton("1");
btn_SpeedyRaider.setBounds(155, 56, 40, 40);
panel_Creep.add(btn_SpeedyRaider);
JButton btn_Ray = new JButton("1");
btn_Ray.setBounds(110, 56, 40, 40);
panel_Creep.add(btn_Ray);
JButton btn_Demeter = new JButton("1");
btn_Demeter.setBounds(65, 56, 40, 40);
panel_Creep.add(btn_Demeter);
JButton btn_Vulture = new JButton("1");
btn_Vulture.setBounds(65, 101, 40, 40);
panel_Creep.add(btn_Vulture);
JButton btn_Shark = new JButton("1");
btn_Shark.setBounds(110, 101, 40, 40);
panel_Creep.add(btn_Shark);
JButton btn_RacingMamba = new JButton("1");
btn_RacingMamba.setBounds(155, 101, 40, 40);
panel_Creep.add(btn_RacingMamba);
JButton btn_HugeTitan = new JButton("1");
btn_HugeTitan.setBounds(200, 101, 40, 40);
panel_Creep.add(btn_HugeTitan);
JButton btn_Zeus = new JButton("1");
btn_Zeus.setBounds(65, 146, 40, 40);
panel_Creep.add(btn_Zeus);
JButton btn_Phoenix = new JButton("1");
btn_Phoenix.setBounds(110, 146, 40, 40);
panel_Creep.add(btn_Phoenix);
JButton btn_ExpressRaptor = new JButton("1");
btn_ExpressRaptor.setBounds(155, 146, 40, 40);
panel_Creep.add(btn_ExpressRaptor);
JButton btn_FatColossus = new JButton("1");
btn_FatColossus.setBounds(200, 146, 40, 40);
panel_Creep.add(btn_FatColossus);
JPanel panel_info = new JPanel();
panel_info.setBackground(UIManager.getColor("PasswordField.disabledBackground"));
panel_info.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
panel_info.setBounds(0, 309, 250, 136);
panel_Bar.add(panel_info);
panel_info.setLayout(null);
JTextPane txtpnInfo = new JTextPane();
txtpnInfo.setBounds(2, 2, 51, 22);
txtpnInfo.setText("Info");
txtpnInfo.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 13));
txtpnInfo.setEnabled(false);
txtpnInfo.setEditable(false);
panel_info.add(txtpnInfo);
JButton btnUpgrade = new JButton("Upgrade: 5000");
btnUpgrade.setBounds(14, 111, 108, 23);
panel_info.add(btnUpgrade);
JButton btnVerkauf = new JButton("Verkauf: 10000");
btnVerkauf.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnVerkauf.setBounds(132, 111, 108, 23);
panel_info.add(btnVerkauf);
JTextPane txtpn_NameValue = new JTextPane();
txtpn_NameValue.setEditable(false);
txtpn_NameValue.setBackground(SystemColor.menu);
txtpn_NameValue.setText("SlowTower");
txtpn_NameValue.setBounds(63, 3, 157, 18);
panel_info.add(txtpn_NameValue);
JTextPane txtpnSchaden = new JTextPane();
txtpnSchaden.setText("Schaden:");
txtpnSchaden.setEditable(false);
txtpnSchaden.setBackground(SystemColor.menu);
txtpnSchaden.setBounds(12, 22, 86, 18);
panel_info.add(txtpnSchaden);
JTextPane txtpnReichweite = new JTextPane();
txtpnReichweite.setText("Reichweite:");
txtpnReichweite.setEditable(false);
txtpnReichweite.setBackground(SystemColor.menu);
txtpnReichweite.setBounds(12, 40, 86, 18);
panel_info.add(txtpnReichweite);
JTextPane txtpnAbklingzeit = new JTextPane();
txtpnAbklingzeit.setText("Abklingzeit:");
txtpnAbklingzeit.setEditable(false);
txtpnAbklingzeit.setBackground(SystemColor.menu);
txtpnAbklingzeit.setBounds(12, 58, 86, 18);
panel_info.add(txtpnAbklingzeit);
JTextPane txtpnKosten = new JTextPane();
txtpnKosten.setText("Kosten:");
txtpnKosten.setEditable(false);
txtpnKosten.setBackground(SystemColor.menu);
txtpnKosten.setBounds(12, 76, 86, 18);
panel_info.add(txtpnKosten);
JTextPane txtpnEffekt = new JTextPane();
txtpnEffekt.setText("Effekt:");
txtpnEffekt.setEditable(false);
txtpnEffekt.setBackground(SystemColor.menu);
txtpnEffekt.setBounds(12, 94, 86, 18);
panel_info.add(txtpnEffekt);
JTextPane txtpn_SchadenValue = new JTextPane();
txtpn_SchadenValue.setText("25");
txtpn_SchadenValue.setEditable(false);
txtpn_SchadenValue.setBackground(SystemColor.menu);
txtpn_SchadenValue.setBounds(100, 22, 140, 18);
panel_info.add(txtpn_SchadenValue);
JTextPane txtpn_ReichweiteValue = new JTextPane();
txtpn_ReichweiteValue.setText("1.75");
txtpn_ReichweiteValue.setEditable(false);
txtpn_ReichweiteValue.setBackground(SystemColor.menu);
txtpn_ReichweiteValue.setBounds(100, 40, 140, 18);
panel_info.add(txtpn_ReichweiteValue);
JTextPane txtpn_AbklingzeitValue = new JTextPane();
txtpn_AbklingzeitValue.setText("750 ms");
txtpn_AbklingzeitValue.setEditable(false);
txtpn_AbklingzeitValue.setBackground(SystemColor.menu);
txtpn_AbklingzeitValue.setBounds(100, 58, 140, 18);
panel_info.add(txtpn_AbklingzeitValue);
JTextPane txtpn_KostenValue = new JTextPane();
txtpn_KostenValue.setText("100");
txtpn_KostenValue.setEditable(false);
txtpn_KostenValue.setBackground(SystemColor.menu);
txtpn_KostenValue.setBounds(100, 76, 140, 18);
panel_info.add(txtpn_KostenValue);
JTextPane txtpn_EffektValue = new JTextPane();
txtpn_EffektValue.setText("(Slowrate/time) 0.3/2000");
txtpn_EffektValue.setEditable(false);
txtpn_EffektValue.setBackground(SystemColor.menu);
txtpn_EffektValue.setBounds(100, 94, 140, 18);
panel_info.add(txtpn_EffektValue);
JPanel panel_chat = new JPanel();
panel_chat.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
panel_chat.setBackground(SystemColor.text);
panel_chat.setBounds(0, 444, 250, 228);
panel_Bar.add(panel_chat);
panel_chat.setLayout(new GridLayout(0, 1, 0, 0));
txtpn_Chat = new JTextPane();
txtpn_Chat.setEditable(false);
txtpn_Chat.setBackground(Color.WHITE);
panel_chat.add(txtpn_Chat);
btnSendMsg = new JButton("Senden");
btnSendMsg.setBounds(563, 660, 89, 23);
frame.getContentPane().add(btnSendMsg);
txtChat = new JTextField();
txtChat.setText("Hey, viel Spaß !");
txtChat.setBounds(10, 661, 543, 20);
frame.getContentPane().add(txtChat);
txtChat.setColumns(10);
Canvas canvas = new Canvas();
canvas.setBackground(Color.BLACK);
canvas.setBounds(10, 10, 642, 642);
frame.getContentPane().add(canvas);
}
public JButton getBtn_Basictower() {
return btn_Basictower;
}
public JButton getBtn_Slowtower() {
return btn_Slowtower;
}
public JButton getBtn_Splashtower() {
return btn_Splashtower;
}
public JButton getBtn_Rockettower() {
return btn_Rockettower;
}
public JButton getBtn_Speedtower() {
return btn_Speedtower;
}
public JButton getBtn_Ultimatetower() {
return btn_Ultimatetower;
}
}
Grüße