Java 按钮悬停
Java button hovering
我是 Java 编程的新手,我想知道如何使用按钮,我创建了一个 "Home Screen" 和一些按钮以及文本,但它不是就像我想要的那样先进,我想知道如何创建图像效果之类的东西,所以假设我将鼠标悬停在一个按钮上,我希望它在它后面显示一个发光的动画,或者因为我没有那个动画,如果没有简单的方法来创建它,只需在它后面显示一个图像就可以了,而且我在按下按钮时没有任何反应 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 [...]
为此,您必须处理 JLayeredPane
和 MouseListener
。 Here 是我创建的示例 "on the run";布局很脏,必须改进。无论如何,您会注意到,一旦将鼠标悬停在按钮上,按钮后面就会出现一个包含 LA- -LL!
的黑边框。那是一个 JLabel
,您可以使用它来显示图像等,方法是 JLabel.setIcon
。
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
这不是那么容易,它需要跳过一大堆箍,尤其是当按钮不是矩形时。
我之前做了一个 "validation highlighter" 的原型,它突出显示了无效字段,它使用了 JXLayer 库,但它应该可以转换为使用核心库中包含的 JLayer 库
有关详细信息,请参阅 How can I change the highlight color of a focused JComboBox
我们的另一种可能性是创建自定义 JPanel
并覆盖其 paintComponent
方法并在那里绘制您的效果。您可以将按钮放在它上面,并结合布局管理器和边框,您应该能够将按钮定位在您需要的位置。
这样做的问题是,它会影响表单的过度布局,因为在布局主要表单时会考虑这种影响
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
我是 Java 编程的新手,我想知道如何使用按钮,我创建了一个 "Home Screen" 和一些按钮以及文本,但它不是就像我想要的那样先进,我想知道如何创建图像效果之类的东西,所以假设我将鼠标悬停在一个按钮上,我希望它在它后面显示一个发光的动画,或者因为我没有那个动画,如果没有简单的方法来创建它,只需在它后面显示一个图像就可以了,而且我在按下按钮时没有任何反应 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 [...]
为此,您必须处理 JLayeredPane
和 MouseListener
。 Here 是我创建的示例 "on the run";布局很脏,必须改进。无论如何,您会注意到,一旦将鼠标悬停在按钮上,按钮后面就会出现一个包含 LA- -LL!
的黑边框。那是一个 JLabel
,您可以使用它来显示图像等,方法是 JLabel.setIcon
。
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
这不是那么容易,它需要跳过一大堆箍,尤其是当按钮不是矩形时。
我之前做了一个 "validation highlighter" 的原型,它突出显示了无效字段,它使用了 JXLayer 库,但它应该可以转换为使用核心库中包含的 JLayer 库
有关详细信息,请参阅 How can I change the highlight color of a focused JComboBox
我们的另一种可能性是创建自定义 JPanel
并覆盖其 paintComponent
方法并在那里绘制您的效果。您可以将按钮放在它上面,并结合布局管理器和边框,您应该能够将按钮定位在您需要的位置。
这样做的问题是,它会影响表单的过度布局,因为在布局主要表单时会考虑这种影响
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