Advertisement
janevim

Generování a vypsání čísla v okně

Jan 24th, 2023 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4.  
  5. public class GUI extends JFrame {
  6.     private JTextField textField1;
  7.     private JButton generujButton;
  8.     private JButton kopírujButton;
  9.     private JTextArea textArea1;
  10.     private JPanel panelMain;
  11.  
  12.  
  13.     public GUI() {
  14.         generujButton.addActionListener(new ActionListener() {
  15.             @Override
  16.             public void actionPerformed(ActionEvent e) {
  17.             double randomDouble = Math.random()*100;
  18.             int random = (int) Math.round(randomDouble);
  19.  
  20.             textField1.setText(String.valueOf(random));
  21.             }
  22.         });
  23.         kopírujButton.addActionListener(new ActionListener() {
  24.             @Override
  25.             public void actionPerformed(ActionEvent e) {
  26.             textArea1.append(textField1.getText()+"\n");
  27.             }
  28.         });
  29.  
  30.     }
  31.  
  32.     public static void main(String[] args) {
  33.         GUI gui = new GUI();
  34.         gui.setVisible(true);
  35.         gui.setTitle("náhodné číslo");
  36.         gui.setContentPane(gui.panelMain);
  37.         gui.setSize(300, 200);
  38.         gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39.     }
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement