I am switching from SI to Java ... Java is some kind of solid philology ... what’s the mistake here? When the button is pressed, the variable is output, but why does the handler function output the textual output, but not the variable? reports an error, what's the matter?

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.*; public class Firshtclass { int a=1; int b=2; private static void Eve() // createAndShowGUI { JPanel windowContent= new JPanel(); JLabel TextOut = new JLabel(); windowContent.add(TextOut); // JTextField field1 = new JTextField(20); // windowContent.add(field1); //................................................................... JFrame frame = new JFrame("My First Calculator"); frame.setContentPane(windowContent); //................................................................... JButton buttonUP = new JButton("Up"); windowContent.add(buttonUP); JButton buttonDOWN = new JButton("Down"); windowContent.add(buttonDOWN); //................................................................... frame.setSize(400,100); frame.setVisible(true); //................................................................... //Add action listener to button buttonUP.addActionListener(new ActionListener() // UP { public void actionPerformed(ActionEvent e) { TextOut.setText(a); } }); buttonDOWN.addActionListener(new ActionListener() // DOWN { public void actionPerformed(ActionEvent e) { TextOut.setText(b); } }); } // createAndShowGUI public static void main(String[] args) // main { Eve(); } // main } 
  • 2
    What kind of error is displayed? - Sergey Rufanov
  • one
    Well, I am certainly not familiar with awt, but by analogy with android, I suppose that you can't just stick an int into setText() , you need to use something like this setText(String.valueOf(a)) , or setText(""+a) - ermak0ff
  • 2
    And in C, how can you not call static content from a static method? - Vartlok
  • one
    @user198532 и хочешь -не хочешь должна быть строковая переменная but what are you saying, do you want to say that if you leave intom and do not write such a record setText(""+a) ? - ermak0ff
  • one
    @ ermak0ff "" + a converts a into a string. So either the variable must be of type String , or it needs to be converted to a string. - Alexey Ivanov

1 answer 1

I see several compilation errors.

one.

Firstclass.java:43: error: non-static variable a cannot be referenced from a static context

  TextOut.setText(a); ^ 

You can not use non-static in static context. This error is fixed by adding static a and b to the declaration:

 static int a=1; static int b=2; 

2

Firstclass.java:43: error: local variable TextOut is accessed from within inner class; needs to be declared final

  TextOut.setText(a); ^ 

To use (local) variables inside an anonymous class, you need to declare them with a final modifier:

 final JLabel TextOut = new JLabel(); 

Java 8 does not require a declaration with final .

3

Firstclass.java:43: error: method setText in class JLabel cannot be applied to given types;

  TextOut.setText(a); ^ 

required: String
found: int
can not be converted to a string by method invocation conversion

JLabel.setText accepts only strings, so an int must be explicitly converted to a string, for example, String.valueOf(a) :

 TextOut.setText(String.valueOf(a)); 

All operations with Swing components must be performed on the Event Dispatch Thread (EDT) , so the main should look like this:

 public static void main(String[] args) // main { SwingUtilities.invokeLater(new Runnable() { public void run() { Eve(); } }); } // main