'Java Collision Detection with JFrame Border

I am trying to create a box (for now) and moving them around using arrow keys. When they hit the edge of JFrame (in this case, label), the box needs to be stopped. When it hits the JFrame border, it should either stop immediately or teleported with the furthest X value touching the border (so that the right side of the box hits the right side of border and teleports to 0 when it hits the left side of the border). The code somehow works when going left, but it doesn't work when going right. The box goes a little bit out of bounds. Speaking of "out of bounds", my Y value of the box seems to be out of box too and I need the maximum Y value of the box to be touching the border (so the bottom part of the box touches the border)

How do I do this?

This is my code:

package UAS;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Player extends JFrame implements KeyListener {
    
    JLabel label;
    ImageIcon imageIcon;

    private int boundX;
    private int boundY;

    Player(int boundX, int boundY){

        this.setX(boundX);
        this.setY(boundY);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(boundX, boundY);
        this.setLayout(null);
        this.addKeyListener(this);
        this.setResizable(false);

        int height = boundY/5;

        // int x = 300, y = 300;
        // int boundX = 300, boundY = 500;

        label = new JLabel();
        // label.setBounds(boundH/2, boundV, boundH, boundV);
        label.setBackground(new Color(55,125,155));
        label.setOpaque(true);
        label.setBounds(0, (boundY-height), boundX/5, height);
        
        // imageIcon = new ImageIcon("UAS/HI3Icon.png"); // load the image to a imageIcon
        // Image image = imageIcon.getImage(); // transform it 
        // Image newimg = image.getScaledInstance(x, y,  java.awt.Image.SCALE_FAST); // scale it the smooth way  
        // imageIcon = new ImageIcon(newimg);  // transform it back
        
        //label.setIcon(imageIcon);

        this.add(label);
        this.setVisible(true);
    }

    public void setX(int boundX){
        this.boundX = boundX;
    }
    public void setY(int boundY){
        this.boundY = boundY;
    }
    public int getXBound(){
        return boundX;
    }
    public int getYBound(){
        return boundY;
    }

    @Override
    public void keyTyped(KeyEvent e){
        //vertical
        if(e.getKeyChar()=='a')
            label.setLocation(label.getX()-10, label.getY());
        else if(e.getKeyChar()=='d')
            label.setLocation(label.getX()+10, label.getY());
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(label.getX()==0||label.getX()<0)
        {
            label.setLocation(0, label.getY());
            switch(e.getKeyCode()) //convert to ASCII
            {
                //horizontal
                case 39: label.setLocation(label.getX()+10, label.getY()); break;
            }
        }
        else if(label.getX()+(getXBound()/5)==getXBound()||label.getX()>getXBound())
        {
            label.setLocation(getXBound()-(getXBound()/5), label.getY());
            switch(e.getKeyCode())
            {
                case 37: label.setLocation(label.getX()-10, label.getY()); break;
            }
        }
        else
        {
            switch(e.getKeyCode())
            {
                case 37: label.setLocation(label.getX()-10, label.getY()); break;
                case 39: label.setLocation(label.getX()+10, label.getY()); break;
            }
        }
        
    }

    @Override
    public void keyReleased(KeyEvent e) {
        System.out.println("You released key char: " + e.getKeyChar());
        System.out.println("You released key char: " + e.getKeyCode());
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source