gpt4 book ai didi

Java 按钮悬停

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:41:20 26 4
gpt4 key购买 nike

我是 Java 编程的新手,我想知道如何使用按钮,我创建了一个“主屏幕”和一些按钮以及文本,但是它没有我想要的那么高级它是,我想知道如何创建图像效果之类的东西,假设我将鼠标悬停在一个按钮上,我希望它在它后面显示一个发光的动画,或者因为我没有那个动画,如果没有简单的创建它的方法,只需在它后面显示一个图像就可以了,按下按钮时我也没有任何反应 bcs IDK 如何做到这一点,所以如果你能帮忙,那就太棒了!这是我目前拥有的代码:

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 [...]

您需要向您的按钮添加一个ActionListener。还有多种其他方法可以检测按钮是否被按下,但这是(在我看来)最简单的方法。这是你如何做的:

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 [...]

为此,您必须处理 JLayeredPaneMouseListenerHere是我“在运行中”创建的示例;布局很脏,必须改进。无论如何,您会注意到,一旦您将鼠标悬停在按钮上,一个包含 LA--LL! 的黑框框将出现在按钮后面。这是一个 JLabel,您可以通过 JLabel.setIcon 方法使用它来显示图像等。

关于Java 按钮悬停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33178060/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com