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 }
setText(), you need to use something like thissetText(String.valueOf(a)), orsetText(""+a)- ermak0ffи хочешь -не хочешь должна быть строковая переменнаяbut what are you saying, do you want to say that if you leave intom and do not write such a recordsetText(""+a)? - ermak0ff"" + aconvertsainto a string. So either the variable must be of typeString, or it needs to be converted to a string. - Alexey Ivanov