简体   繁体   中英

Java button hovering

I'm new to Java programming and I'd like to know how to mess around with buttons, I have created a "Home Screen" and some buttons as well as text, however it's not as advanced as I'd like it to be, I want to know how to create things like image effects, so let's say I hover over a button, I want it to display a glowing animation behind it, or since I don't have that animation, if there's no easy way to create it, just displaying an image behind it is alright, also I don't have anything happening when pressing the button bcs IDK how to do that yet so if you could help with that it'd be awesome! Here's the code I currently have:

package Menu;

import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class BackgroundPanel extends JFrame {
private BufferedImage image;
public BackgroundPanel() {
    ImageIcon icon = new ImageIcon("image_path.png");
    JButton btn = new JButton(icon);
    btn.setOpaque(false);
    btn.setContentAreaFilled(false);
    btn.setBorderPainted(false);
    btn.setFocusPainted(false);
    try {
        image = ImageIO.read(new File("image_path.jpg"));
        // Set your Image Here.
        this.setContentPane(new JLabel(new ImageIcon(image)));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    JPanel panel = new JPanel();
    panel.add(new JLabel("Username:"));
    panel.add(new JTextField("",20));
    panel.add(new JLabel("Password:"));
    panel.add(new JTextField("",20));
    panel.add(btn);

    //Adding components to Panel and JFrame
    this.add(panel);    
    // JFrame Properties
    this.setSize(1280,720);
    this.setLayout(new FlowLayout());
    this.setResizable(true);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("Panel");
    this.setVisible(true);

}

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

}

[...] also I don't have anything happening when pressing the button bcs IDK how to do that yet so if you could help with that it'd be awesome [...]

You need to add an ActionListener to your button. There are various other ways to detect if the button was pressed, but this one is the (in my opinion) easiest. This is how you do it:

btn.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent event) {
        // code you write in here will be executed if the button was pressed
    }
});

[...] let's say I hover over a button, I want it to display a glowing animation behind it, or since I don't have that animation, if there's no easy way to create it, just displaying an image behind it is alright [...]

For this, you'll have to deal with JLayeredPane s and MouseListener s. Here is an example that I created "on the run"; the layouting is very dirty and has to be improved. Anyhow, you'll notice that once you hover over the button, a black-bordered box containing LA- -LL! will appear behind the button. That's a JLabel and you can use it to display images and such by using the JLabel.setIcon method.

let's say I hover over a button, I want it to display a glowing animation behind it, or since I don't have that animation, if there's no easy way to create it, just displaying an image behind it is alright

This is not that easy, it requires jumping through a whole bunch of hoops, especially when the button isn't rectangle.

A while I ago a did a prototype of a "validation highlighter" which highlighted invalid fields, it makes use of JXLayer library, but which should be convertible to use the included JLayer library on the core libraries

See How can I change the highlight color of a focused JComboBox for more details

The other possibility we'd be to create a custom JPanel and override its paintComponent method and paint your effect there. You would place your button it and with a combination of aa layout manager and borders you should be able to get the button positioned where you need it.

The problem with this, is it will effect the over layout of your form, as the effect will be considered while the primary form is laid out

I really don't have anything happening when pressing the button bcs IDK how to do that yet so if you could help with that it'd be awesome

I suggest you have a look at How to use buttons and How to write ActionListeners

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM