Just studying swing. To implement your ideas you need a window that will display the data entered using the buttons. To give you a concrete idea of ​​what I want: it is something like the windows calculator. (I don’t touch the implementation of typing with the keyboard symbol yet). So what is the name of this window \ command?

  • This object is called JFrame - plesser
  • The window should immediately display the entered characters (as in the Windows calculator) - Vlad
  • but generally I recommend to read Schildt’s book "Swing for beginners" first and only then do something of my own - plesser
  • I understand that jframe, but I'm particularly interested in this window, because in jframe there are a lot of different commands - Vlad
  • one
    this does not happen, you insert a button - you hang a listener on it that already fills the text field - plesser

1 answer 1

Actually, the program code that does something similar (create the Main class in the IDE, copy there, click to execute - run):

import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class Main extends JFrame { private int number = 0; //создаем переменную number и присваиваем ей значение 0 private JLabel label; // создаем ссылку для объектов класса JLabel, лейбл - поле для отображения текста в нашем случае private JButton button; // кнопка - тут думаю понятно public Main(){ super("Test frame"); // Устанавливаем имя окна класса Main с помощью конструктора его предка JFrame label = new JLabel("Input:" + number); // создаем объект класса Jlabel и сохраняем в переменную label ссылку на него button = new JButton("increment"); // аналогично с кнопкой JPanel panel = new JPanel(new FlowLayout()); //Создаем временные компоненты - панель panel.add(label); panel.add(button); // добавляем кнопку в панель add(panel, BorderLayout.SOUTH); // добавляем панель на окно setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //устанавливаем завершение процесса при закрытии окна } public static void main(String[] args) { Main frame = new Main(); //создаем экземпляр класса Main frame.setVisible(true); //делаем его видимым frame.pack(); //устанавливаем оптимальный размер окна frame.setLocationRelativeTo(null);//устанавливаем позицию окна на экране frame.button.addActionListener(new ActionListener() { //привязываем инстанс класса ActionListener(слушатель действий) к кнопке @Override public void actionPerformed(ActionEvent e) { frame.number+=1; //увеличиваем переменную на 1 frame.label.setText("Input:" + frame.number); // обновляем наш лейбл } }); } } 

And now in order:

  1. What you want to implement is not a window or a command. This is a program that uses the standard Java library classes: awt and swing. With their help, you can really implement a window on which there will be an element displaying data, and a button with which you can change this data.
  2. What you need to know in order to independently implement this:

- what are classes and inheritance
- primitive and reference types - what are such methods
-read about each class used in the program above (highlighted in blue)

In short, learn the basics, nowhere without them.