Advertisement
sergAccount

Untitled

Oct 18th, 2020
2,326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package game;
  7.  
  8. import java.awt.event.KeyEvent;
  9. import java.awt.event.KeyListener;
  10. import javax.swing.JFrame;
  11.  
  12. public class Okno extends JFrame implements KeyListener{
  13.    
  14.     int slogn;
  15.     Pole pole;
  16.    
  17.     public Okno(int slogn){
  18.         this.slogn = slogn;
  19.         // вызываем метод addKeyListener для обработки событий от клавиатуры
  20.         addKeyListener(this);
  21.         //
  22.         setTitle("Игра1");
  23.         // установка фокуса  
  24.         setFocusable(true);
  25.         // запрет на изменение размеров главного окна
  26.         setResizable(false);
  27.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28.         // позиция и размеры окна
  29.         this.setBounds(10, 10, 800, 620);
  30.         // устанавливаем панель для главного окна
  31.         pole = createPanel(slogn);
  32.         getContentPane().add(pole);
  33.         //
  34.         setVisible(true);
  35.     }
  36.     //
  37.     public Pole createPanel(int slogn){        
  38.         return new Pole(slogn);
  39.     }
  40.  
  41.     // методы для обработки событий от клавиатуры
  42.     @Override
  43.     public void keyTyped(KeyEvent e) {        
  44.     }
  45.  
  46.     // обработка нажатия на опред клавиши
  47.     @Override
  48.     public void keyPressed(KeyEvent e) {
  49.         int key = e.getKeyCode();
  50.         //
  51.         System.out.println("keyPressed.key=" + key);
  52.         // при нажатии на клавишу esc - выполняем выход из программы
  53.         if(key == KeyEvent.VK_ESCAPE){
  54.             //  выполняем выход из программы
  55.             System.exit(0);
  56.         }        
  57.     }
  58.  
  59.     @Override
  60.     public void keyReleased(KeyEvent e) {        
  61.     }
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement