Advertisement
mullerdaniil

JPanel template #1

Oct 1st, 2023 (edited)
794
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.77 KB | Source Code | 0 0
  1. package org.example;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7. public class Panel extends JPanel {
  8.     private static final Dimension WINDOW_SIZE = new Dimension(800, 600);
  9.     private static final Color WINDOW_BACKGROUND_COLOR = new Color(0, 0, 64);
  10.     private static final String WINDOW_NAME = "WINDOW_NAME";
  11.     private static final int TIMER_DELAY = 10;
  12.  
  13.     public static void main(String[] args) {
  14.         new Panel();
  15.     }
  16.  
  17.     public Panel() {
  18.         setupFrameAndPanel();
  19.         new Timer(TIMER_DELAY, new TimerListener()).start();
  20.     }
  21.  
  22.  
  23.     private class TimerListener implements ActionListener {
  24.         @Override
  25.         public void actionPerformed(ActionEvent e) {
  26.             //
  27.         }
  28.     }
  29.  
  30.     private class KeyListener extends KeyAdapter {
  31.         @Override
  32.         public void keyPressed(KeyEvent e) {
  33.             int keyCode = e.getKeyCode();
  34.  
  35.             //
  36.         }
  37.  
  38.         @Override
  39.         public void keyReleased(KeyEvent e) {
  40.             int keyCode = e.getKeyCode();
  41.  
  42.             //
  43.         }
  44.     }
  45.  
  46.     private class MouseListener extends MouseAdapter {
  47.         //
  48.     }
  49.  
  50.     @Override
  51.     protected void paintComponent(Graphics g) {
  52.         super.paintComponent(g);
  53.        
  54.         //
  55.     }
  56.  
  57.     private void setupFrameAndPanel() {
  58.         var frame = new JFrame(WINDOW_NAME);
  59.         frame.setSize(WINDOW_SIZE);
  60.         setFocusTraversalKeysEnabled(true);
  61.         setFocusable(true);
  62.         addKeyListener(new KeyListener());
  63.         addMouseListener(new MouseListener());
  64.         frame.add(this);
  65.         setBackground(WINDOW_BACKGROUND_COLOR);
  66.         frame.setResizable(true);
  67.         frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  68.         frame.setVisible(true);
  69.     }
  70. }
Tags: Java swing jpanel
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement