Home > Net >  My actionPerformed method(Java) is not working and I have no clue why
My actionPerformed method(Java) is not working and I have no clue why

Time:01-24

Here is my whole program, don't wonder about the words I am using, I am German. Down from l. 95 to l. 103 is the action performed method, (I only did the System.out.println() to see wether it is working or not). I wrote other programs, where I never had any such problems, and I tried so many things, but I did not find the problem, maybe it is a total simple one... So I would appreciate it if you help me!

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GameFrame implements ActionListener {

    String wochentag;
    int itag = 0;
    int imonat = 0;
    int ijahrhundert = 0;
    int ijahr = 0;

    String stag;
    String smonat;
    String sjahr;
    String sJahrhundert;
    boolean rechnen = false;

    JFrame frame = new JFrame();
    JButton start = new JButton("Wochentag errechnen");
    JButton update = new JButton("Datum anzeigen");

    JPanel alles = new JPanel();

    JLabel Tag = new JLabel("Tag");
    JLabel Monat = new JLabel("Monat");
    JLabel Jahr = new JLabel("Jahr");
    JLabel Jahrhundert = new JLabel("Jahrhundert");

    JTextField Datum = new JTextField(" Welchen Wochentag hatte der --.--.---- ");
    JTextField Output = new JTextField(" Dieser Tag war ein ------tag");

    JTextField tag = new JTextField();
    JTextField monat = new JTextField();
    JTextField jahr = new JTextField();
    JTextField jahrhundert = new JTextField();

    GameFrame(){

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);
        frame.getContentPane().setBackground(Color.WHITE);
        frame.setLayout(new BorderLayout());
        frame.setVisible(true);
        frame.setFocusable(false);
        frame.setResizable(false);

        alles.setBounds(0,0,500,500);

        Datum.setBounds(75,40,280,40);
        Datum.setEditable(false);
        Tag.setBounds(175,100,45,25);
        Monat.setBounds(175,150,55,25);
        Jahr.setBounds(175,200,50,25);
        Jahrhundert.setBounds(175,250,75,25);
        Output.setBounds(118,400,202,40);
        Output.setEditable(false);

        tag.setBounds(250,100,75,25);
        tag.setEditable(true);
        monat.setBounds(250,150,75,25);
        jahr.setBounds(250,200,75,25);
        jahrhundert.setBounds(250,250,75,25);

        start.setBounds(165,350,170,25);
        start.setFocusable(false);

        update.setBounds(175,300,150,25);
        update.setFocusable(false);

        frame.add(alles);

        alles.add(Datum);
        alles.add(Tag);
        alles.add(Monat);
        alles.add(Jahr);
        alles.add(Jahrhundert);

        alles.add(Output);

        alles.add(tag);
        alles.add(monat);
        alles.add(jahr);
        alles.add(jahrhundert);

        alles.add(start);
        alles.add(update);
    }

    public static void main(String[] args) {

        GameFrame gameframe = new GameFrame();
    }
    @Override
    public void actionPerformed(ActionEvent e){

        if(e.getSource() == update){
            stag = tag.getText();
            tag.setText("");
            System.out.println(stag);
        }
    }
}

CodePudding user response:

As the guys mentioned in the comments, you have not added the listener to your component (update button). If you are using a button, you might consider using a MouseListener instead

public class GameFrame implements MouseListener {

JButton update = new JButton("Datum anzeigen");
GameFrame()
{

  //other code
  update.addMouseListener(this);

}
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }

    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }

    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }

    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }

    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }
}

If you want to have multiple listeners for your separate buttons you can also create them within your constructor

update.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e){
        System.out.println("Start button wurde geklickt");
    }
};
start.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e){
        System.out.println("Update button wurde geklickt");
    }
};
  •  Tags:  
  • Related