Freddy
Freddy

Reputation: 867

How to generate an event on the click of an image?

I am designing a GUI, where I have added logo's to several frames. I want to make the logo's functional so that when they're clicked upon, it returns then to the main menu frame.

For example; I have the following logo created here (In controller class):

    LogoTitlePanel = new JPanel(new GridLayout(1, 1));
    LogoTitlePanel.setBackground(new java.awt.Color(255, 255, 255));
    frame.add(LogoTitlePanel, BorderLayout.PAGE_START);
    frame.setVisible(true);

    LogoImage = new ImageIcon(getClass().getResource("Acme.png"));
    ImageContainer = new JLabel(LogoImage);
    LogoTitlePanel.add(ImageContainer);
    wholeFramePanel.add(LogoTitlePanel);
    frame.add(wholeFramePanel, BorderLayout.CENTER);
    frame.setVisible(true);

... and I want the Acme.png image to take them to this frame upon being clicked (In MainMenu class):

    frame = new JFrame("Aston Cruise & Maritime Enterprise");

Is it possible to accomplish this? if so, how?

Upvotes: 0

Views: 110

Answers (2)

camickr
camickr

Reputation: 324128

You can use a JButton instead of a JLabel and then just add an ActionListener to the button.

You can make the button look like a label by using:

button.setBorderPainted( false );
button.setFocusPainted( false );

Upvotes: 2

Aaron Esau
Aaron Esau

Reputation: 1123

Instead of using ActionListener, try MouseListener...

private MouseListener listener = new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
        //Code...
    }
}

LogoImage.addMouseListener(listener);

Upvotes: 0

Related Questions