package com.company; public class LoginForm implements ActionListener { JButton button; JTextField yourLogin; JTextField result; String login = null; public void log() { JFrame frame = new JFrame(); yourLogin = new JTextField(); result = new JTextField(); button = new JButton("Login"); button.addActionListener(this);// frame.getContentPane().add(BorderLayout.NORTH, button); frame.getContentPane().add(BorderLayout.SOUTH, yourLogin); frame.getContentPane().add(BorderLayout.CENTER, result); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 300); frame.setVisible(true); } // Создает фрейм с кнопкой и текстовыми полями, в котором при нажатии кнопки значение из поля передается в String login public void actionPerformed(ActionEvent event) { login = yourLogin.getText(); result.setText(login); System.out.println(login); // Работает, выдает результат в консоль } public String getLogin(){ return login; } // Геттер, нужно чтобы так же передавал результат в консоль. Но он передает null. То есть использует переменную класса а не метода } 

MAIN

 import javax.swing.*; public class Main { public static void main(String[] args) { LoginForm logg = new LoginForm(); logg.log(); System.out.println(logg.getLogin()); // Выдает null. Берет переменную класса а не метода } } 
  • Do you need to press the button to work out the event? - Herrgott
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

Here it is a matter of asynchronous code. You initialize the LoginForm further. The .log() method, you can say, acts as a constructor:

  • initializes the variables.
  • hangs an event on a button.

Then you immediately called .getLogin() . But login still null, as far as we remember.

And only after clicking on the button, login gets the value from our login field.

In general, to solve your problem, you need to know exactly what you are trying to get and where to send

  • It is necessary to extract the entered information from the textField by pressing the button and invoke it with the login variable. Next, it is necessary to work with login in other classes, which can, for example, enter information into a database, or enter a value into an array. It is impossible to pull out this variable from the method. Seichas specifically I want to give it to Main. - Morozov Viktor
  • Yes you are right, added to get Thread.sleep (5000); Now if you have time to press the button in 5 seconds, then everything will work correctly. But it is a megatup crutch. I will think either over an additional event, or how to abandon Geta, or over a crutch more elegantly. - Morozov Viktor
  • You can also call a button to call a method of another class, which would write the login to the database. Just do not need to get a login at the wrong time at the wrong place :) The whole problem is this. - Pleshevskiy
  • On the example of androids, I can say this approach: we create activations with the login form. The person presses the button, waiting for the server to process our form. If everything is ok, then close the activation with the login form and go to the main screen. Hope this helps you - Pleshevskiy

The login variable is not initialized in the constructor and the log() method, so null returns