R
R0wiwan
Mitglied
Hallo zusammen!
Ich erstelle ein kleines interaktives Adventure, mit grafischer Oberfläche.
Nur leider funktioniert mein Actionlistener nicht. Es passiert einfach nichts, wenn ich die JButtons klicke.
Könnt ihr mir helfen?
LG
Ich erstelle ein kleines interaktives Adventure, mit grafischer Oberfläche.
Nur leider funktioniert mein Actionlistener nicht. Es passiert einfach nichts, wenn ich die JButtons klicke.
Window Klasse:
package main;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Window extends JFrame {
public static final int WITDH = 700;
public static final int HEIGHT = 700;
public static final String TITLE = "Namenlose Welt";
public Color background1 = new Color(125,97,97);
public Color background2 = new Color(95,74,74);
public Font fontNormal = new Font("Arial", 0, 25);
public Font fontFett = new Font("Arial", Font.BOLD, 40);
public static JLabel title = new JLabel();
public static JTextArea text = new JTextArea();
public static JButton buttonLeft = new JButton("Spiel starten");
public static JButton buttonMiddle = new JButton("-------");
public static JButton buttonRight = new JButton("Spiel verlassen");
public static JTextField textEingabe = new JTextField(20);
public Window (ActionListener listener) {
setSize(WITDH, HEIGHT);
setTitle(TITLE);
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel upperpanel = new JPanel();
upperpanel.setBackground(background2);
upperpanel.setPreferredSize(new Dimension(85,85));
add(upperpanel, BorderLayout.NORTH);
JPanel middlepanel = new JPanel();
middlepanel.setBackground(background1);
add(middlepanel);
JPanel lowerpanel = new JPanel();
lowerpanel.setBackground(background2);
lowerpanel.setPreferredSize(new Dimension(90,90));
add(lowerpanel, BorderLayout.SOUTH);
title.setText("Hauptmenü");
title.setFont(fontFett);
upperpanel.add(title);
text.setText("");
text.setFont(fontNormal);
text.setLineWrap(true);
text.setWrapStyleWord(true);
text.setBackground(background1);
text.setEditable(false);
text.setBounds(100, 100, 500, 500);
middlepanel.add(text);
lowerpanel.add(buttonLeft);
lowerpanel.add(buttonMiddle);
lowerpanel.add(buttonRight);
lowerpanel.add(textEingabe);
buttonLeft.setFont(fontNormal);
buttonMiddle.setFont(fontNormal);
buttonRight.setFont(fontNormal);
textEingabe.setFont(fontNormal);
buttonLeft.addActionListener(listener);
buttonMiddle.addActionListener(listener);
buttonRight.addActionListener(listener);
}
}
Controller Klasse:
package main;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Controller implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == Window.buttonLeft) {
System.out.println("Test");
}
if (e.getSource() == Window.buttonMiddle) {
}
if (e.getSource() == Window.buttonRight) {
System.exit(0);
}
}
}
Könnt ihr mir helfen?
LG
Start:
package main;
public class Start {
public static void main(String[] args) {
Window window = new Window(null);
window.setVisible(true);
}
}
Zuletzt bearbeitet: