D
D
Don_Conteo2021-01-16 03:52:20
Java
Don_Conteo, 2021-01-16 03:52:20

Why does the button only show up on hover?

The button is displayed only when hovering over. Why is this and how can I solve this problem? I want my buttons to show up right away.

public class Room extends JPanel{

    Image back = new ImageIcon("res/images/interior1.jpg").getImage();
    private JButton toLeft = new JButton(new ImageIcon("res/images/arrowLeftUnpressed.png"));
    private JButton toRight = new JButton(new ImageIcon("res/images/arrowRightUnpressed.png"));

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(back, 0, 0, 1920, 1080, null);
    }

    public Room() {
        setLayout(null);
        add(toLeft);
        toLeft.setBounds(50, 470, 170, 170);
        add(toRight);
        toRight.setBounds(1700, 470, 170, 170);
    }
}


public class Display {

    public Display() {
        JFrame frame = new JFrame("MindLoop");
        frame.getContentPane().add(new Room());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setUndecorated(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new Display();
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Erik Mironov, 2021-01-16
@Don_Conteo

Greetings. You don't need to override the top-level container's paint, instead override the paintComponent method, call the super constructor in it, and draw your button.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question