Witam w swoim programie by większość była przejrzysta chciałem stworzyć osobną klasę ListenForButtomMAinFrame i tam umieścić co będzie się działo. Ale mam problem bo w osobnej klasie nie widać utworzonych w klasie MainFrame przycisku.
MainFrame.java
package com.kelthuzad.GUI;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainFrame extends JFrame {
JLabel labelLogin , labelPassword;
JButton button;
public MainFrame() {
this.setTitle("Log on");
this.setSize(400, 400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
JPanel panel = new JPanel();
labelLogin = new JLabel("Login: ");
labelPassword = new JLabel("Password: ");
button = new JButton("test");
ListenForButtonMainFrame listenForButtonMainFrame = new ListenForButtonMainFrame();
button.addActionListener(listenForButtonMainFrame);
panel.add(button);
panel.add(labelLogin);
panel.add(labelPassword);
this.add(panel);
this.setVisible(true);
}
}
ListenForButtomMAinFrame.java
package com.kelthuzad.GUI;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ListenForButtonMainFrame implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
System.out.println("dd");
}
}
}