So I have just got into java swing and cannot find how I would change the colour within a lambda function, I imagine it will have something to do with getting the source or maybe even just making the button variable at the top of the class so I can access it from anywhere in the code.
Here is my window class:
import javax.swing.*;
public class window {
JFrame frame;
window(String title){
this.frame = new JFrame(title);
JButton button = new JButton("Text");
button.addActionListener(x-> System.out.println(x.getSource()));
this.frame.setSize(550,550);
button.setBounds(550/2-75,550/2-75,150,50);
this.frame.add(button);
this.frame.setLayout(null);
this.frame.setVisible(true);
}
}
and my main class:
public class Main {
public static void main(String[] args) {
window win = new window("Hi");
}
}
I would like for the button to change colour on click.