import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class Кнопка {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setLocation(700, 300);
frame.setSize(700, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null); // важно!
frame.setVisible(true);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBounds(0, 0, 700, 300);
JTextField textField = new JTextField("ВВОД", 20);
textField.setBounds(50, 20, 200, 30);
textField.setBackground(Color.YELLOW);
panel.add(textField);
JLabel label = new JLabel();
label.setBounds(50, 60, 200, 30);
label.setForeground(Color.RED);
panel.add(label);
JButton button = new JButton("Добавить");
button.setBackground(Color.BLUE);
button.setForeground(Color.YELLOW);
button.setBounds(50, 100, 150, 30);
panel.add(button);
frame.add(panel);
// список (чтобы не создавался каждый раз заново)
ArrayList<String> list = new ArrayList<>();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
String name = textField.getText();
list.add(name);
System.out.println(list);
label.setText(name);
}
});
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class Кнопка {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setLocation(700, 300);
frame.setSize(700, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.setVisible(true);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBounds(0, 0, 700, 300);
JTextField textField = new JTextField("0", 20);
textField.setBounds(50, 20, 200, 30);
panel.add(textField);
JButton button = new JButton("Добавить");
button.setBounds(50, 60, 150, 30);
panel.add(button);
frame.add(panel);
ArrayList<Integer> list = new ArrayList<>();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
int number = Integer.parseInt(textField.getText());
list.add(number);
System.out.println(list);
}
});
}
}