Не обрабатывется нажатие на кнопку

При нажатии на кнопку ничего не происходит

import javax.swing.*;
import javax.swing.border.TitledBorder;

public class RegisterFrame extends JFrame {
private JButton btnReg;
private JTextField txLogin;
private JTextField txPassword;
private JTextField txEmail;

private Box mainBox;
private Box firstBox;
private Box secondBox;
private Box threeBox;

private Boolean isRegFrame;

public RegisterFrame() {
    super("Регистрация");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setBounds(300, 260, 300, 260);


    mainBox = Box.createVerticalBox();
    txLogin = new JTextField();
    txLogin.setBorder(new TitledBorder("Введите логин"));

    txPassword = new JTextField();
    txPassword.setBorder(new TitledBorder("Введите пароль"));

    txEmail = new JTextField();
    txEmail.setBorder(new TitledBorder("Введите email"));

    btnReg = new JButton("Добавить");

    firstBox = new Box(BoxLayout.PAGE_AXIS);
    firstBox.add(txLogin);
    firstBox.add(Box.createVerticalStrut(30));

    secondBox = new Box(BoxLayout.PAGE_AXIS);
    secondBox.add(txPassword);
    secondBox.add(Box.createVerticalStrut(30));

    threeBox = new Box((BoxLayout.PAGE_AXIS));
    threeBox.add(txEmail);
    threeBox.add(btnReg);
    threeBox.add(Box.createHorizontalStrut(80));
    threeBox.add(Box.createVerticalStrut(30));


    mainBox.add(firstBox);
    mainBox.add(secondBox);
    mainBox.add(threeBox);

    setContentPane(mainBox);

    setLocationRelativeTo(null);
    setResizable(false);
}

public JButton getBtnReg() {
    return btnReg;
}

public void setBtnReg(JButton btnReg) {
    this.btnReg = btnReg;
}

public JTextField getTxLogin() {
    return txLogin;
}

public void setTxLogin(JTextField txLogin) {
    this.txLogin = txLogin;
}

public JTextField getTxPassword() {
    return txPassword;
}

public void setTxPassword(JTextField txPassword) {
    this.txPassword = txPassword;
}

public JTextField getTxEmail() {
    return txEmail;
}

public void setTxEmail(JTextField txEmail) {
    this.txEmail = txEmail;
}

public Box getMainBox() {
    return mainBox;
}

public void setMainBox(Box mainBox) {
    this.mainBox = mainBox;
}

public Box getFirstBox() {
    return firstBox;
}

public void setFirstBox(Box firstBox) {
    this.firstBox = firstBox;
}

public Box getSecondBox() {
    return secondBox;
}

public void setSecondBox(Box secondBox) {
    this.secondBox = secondBox;
}

public Box getThreeBox() {
    return threeBox;
}

public void setThreeBox(Box threeBox) {
    this.threeBox = threeBox;
}

public Boolean getRegFrame() {
    return isRegFrame;
}

public void setRegFrame(Boolean regFrame) {
    isRegFrame = regFrame;
}

//    private void initializeAllListeners() {
//        btnReg.addActionListener(e -> {
//            String firstName = txLogin.getText();
//            String lastName = txPassword.getText();
//            String email = txEmail.getText();
//            //Дальше логика
//
//        });
//    }

//    public static void main(String[] args) {
//        RegisterFrame registerFrame = new RegisterFrame();
//        registerFrame.setVisible(true);
//    }
}//CREATE TABLE users(id SERIAL PRIMARY KEY,login VARCHAR,password VARCHAR,email VARCHAR);

Класс Controller

package Controller;

import Frames.ChatFrame;
import Frames.ChoseFrame;
import Frames.LoginFrame;
import Frames.RegisterFrame;
import Settings.RegisterSettings;
import com.google.gson.Gson;

import javax.swing.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

 public class Controller {
//REG
private JButton btnReg;
private JTextField txLogin;
private JTextField txPassword;
private JTextField txEmail;
//REG

//Login
private JTextField loginField;
private JPasswordField passwordField;
private JButton btnOK;
private JButton btnCancel;
//Login

//ChatFrame
private JTextField jtfMessage;
private JTextArea jtaAllMessage;
private JButton jbSendMessage;
private JLabel jlCountOfClients;
//ChatFrame

//Sockets
private static final String SERVER_HOST = "192.168.0.40";
private static final int SERVER_PORT = 7788;
private Socket clientSocket;
private Scanner inMessage;
private PrintWriter outMessage;
private String clientName;

public Controller() {
    initializeFields();
    initializeListeners();
}

public void start() {
    ChoseFrame choseFrame = new ChoseFrame();
    choseFrame.setVisible(true);
}

public void startChat() {
    new Thread(() -> {
        while (true) {
            if (inMessage.hasNext()) {
                String message = inMessage.nextLine();
                String clientsInChat = "Clients in chat = ";
                if (clientName.indexOf(clientsInChat) == 0) {//indexOf возврощает инлекс в данной строке 1 вхождения указоной под строки
                    jlCountOfClients.setText(message);
                }else {
                    jtaAllMessage.append(message);
                    jtaAllMessage.append("\n");
                }
            }
        }
    });
}

private void initializeFields() {
    RegisterFrame registerFrame = new RegisterFrame();
    btnReg = registerFrame.getBtnReg();
    txLogin = registerFrame.getTxLogin();
    txPassword = registerFrame.getTxPassword();
    txEmail = registerFrame.getTxEmail();

    LoginFrame loginFrame = new LoginFrame();
    loginField = loginFrame.getLoginField();
    passwordField = loginFrame.getPasswordField();
    btnOK = loginFrame.getBtnOK();
    btnCancel = loginFrame.getBtnCancel();

    ChatFrame chatFrame = new ChatFrame();
    jtfMessage = chatFrame.getJtfMessage();
    jbSendMessage = chatFrame.getJbSendMessage();
    jlCountOfClients = chatFrame.getJlCountOfClients();
    jtaAllMessage = chatFrame.getJtaAllMessage();

    try {
        clientSocket = new Socket(SERVER_HOST, SERVER_PORT);
        inMessage = new Scanner(clientSocket.getInputStream());
        outMessage = new PrintWriter(clientSocket.getOutputStream());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void initializeListeners() {
    System.out.println("342");
    btnReg.addActionListener(e -> {
        System.out.println("gfhfghf");

        String login = txLogin.getText();
        String password = txPassword.getText();
        String email = txEmail.getText();
        int code = 1;

        RegisterSettings registerSettings = new RegisterSettings(login, password, email, code);
        Gson gson = new Gson();
        String jsonMessage = gson.toJson(registerSettings);
        System.out.println(jsonMessage);

        outMessage.println(jsonMessage);
        outMessage.flush();
    });
}
}

Ответы (1 шт):

Автор решения: alex9127

Добавьте следующее в свой код (прям в класс, наследующий фрейм):

<buttonname>.addActionListener(ActionEvent ae -> {
    // Тут код
});
→ Ссылка