- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我通过扩展 JPanel 并使用 OverlayLayout 在彼此之上添加 2 个 JLabel 制作了一个按钮。 JLabels 之一是图标,它只是一个字体很棒的图标。另一种是悬停文本。我希望 JPanel 的背景是透明的,因此它没有白色边框。我使用 AlphaContainer 方法做到了这一点:
这就是我所说的缓慢悬停的意思:
我现在只将 setOpaque(false) 属性添加到图标的下半部分。它看起来是这样的:
这里我添加了 6 个按钮:
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
HoverButtonTest forum = new HoverButtonTest("\uf086", "Forum", Settings.iconSize);
add(forum);
HoverButtonTest hs = new HoverButtonTest("\uf080", "Highscores", Settings.iconSize);
add(hs);
HoverButtonTest shop = new HoverButtonTest("\uf07a", "Store", Settings.iconSize);
add(shop);
HoverButtonTest vote = new HoverButtonTest("\uf046", "Vote for Us", Settings.iconSize);
vote.setOpaque(false);
add(vote);
HoverButtonTest discord = new HoverButtonTest("\uf392", "Discord", Settings.iconSize, HoverButtonTest.Type.BRAND);
discord.setOpaque(false);
add(discord);
HoverButtonTest web = new HoverButtonTest("\uf0ac", "Web", Settings.iconSize, HoverButtonTest.Type.SOLID);
web.setOpaque(false);
add(web);
这是我正在使用的按钮类:
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
/**
* @author Ruud.
*/
public class HoverButtonTest extends JPanel implements MouseListener {
public enum Type {
NORMAL("Font Awesome.ttf"),
REGULAR("Font Awesome Regular.ttf"),
SOLID("Font Awesome Solid.ttf"),
BRAND("Font Awesome Brands.ttf");
String font;
Type(String font) {
this.font = font;
}
String getFont() {
return font;
}
}
private JLabel icon;
private JLabel text;
public HoverButtonTest(String icon, String text, int size) {
this(icon, text, size, Type.NORMAL);
}
public HoverButtonTest(String icon, String text, int size, Type type) {
setLayout(new OverlayLayout(this));
addMouseListener(this);
setOpaque(false);
this.text = new JLabel(text);
this.text.setForeground(Color.WHITE);
this.text.setAlignmentX(0.5f);
this.text.setAlignmentY(0.5f);
Utils.setFont(this.text, "OpenSans-Light.ttf", 13);
this.text.setVisible(false);
add(this.text);
this.icon = new JLabel(icon);
this.icon.setForeground(Settings.primaryColor);
this.icon.setAlignmentX(0.5f);
this.icon.setAlignmentY(0.5f);
Utils.setFont(this.icon, type.getFont(), size);
add(this.icon);
}
@Override
public void paintComponent(Graphics g) {
g.setColor(new Color(0, 0, 0, 0));
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
text.setVisible(true);
icon.setForeground(Settings.primaryColor.darker().darker().darker().darker());
}
@Override
public void mouseExited(MouseEvent e) {
text.setVisible(false);
icon.setForeground(Settings.primaryColor);
}
}
我已经编辑了 RadioDef 的代码,以更符合我的需求,但奇怪的是,它没有出现在这段代码中
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URL;
public class MouseoverPanels {
public static void main(String[] args) {
SwingUtilities.invokeLater(MouseoverPanels::new);
}
public MouseoverPanels() {
JFrame frame = new JFrame("Mouseover Panels");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel background = new Background();
JPanel buttons = new Buttons();
//JPanel buttons = new JPanel();
buttons.setOpaque(false);
buttons.add(new MouseoverPanel("Queen", QUEEN_IMG, QUEEN_IMG_H));
buttons.add(new MouseoverPanel("King", KING_IMG, KING_IMG_H));
buttons.add(new MouseoverPanel("Rook", ROOK_IMG, ROOK_IMG_H));
buttons.add(new MouseoverPanel("Knight", KNIGHT_IMG, KNIGHT_IMG_H));
buttons.add(new MouseoverPanel("Pawn", PAWN_IMG, PAWN_IMG_H));
background.add(buttons);
frame.setContentPane(background);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
class MouseoverPanel extends JPanel {
JLabel button;
JLabel hoverImage;
JLabel icon;
MouseoverPanel(String text, Image img, Image hImg) {
setLayout(new OverlayLayout(this));
setOpaque(false);
button = new JLabel(text);
button.setOpaque(false);
button.setAlignmentX(0.5f);
button.setAlignmentY(0.5f);
button.setVisible(false);
hoverImage = new JLabel(new ImageIcon(hImg));
hoverImage.setOpaque(false);
hoverImage.setAlignmentX(0.5f);
hoverImage.setAlignmentY(0.5f);
hoverImage.setVisible(false);
icon = new JLabel(new ImageIcon(img));
icon.setOpaque(false);
icon.setAlignmentX(0.5f);
icon.setAlignmentY(0.5f);
add(button);
add(hoverImage);
add(icon);
MouseoverListener ml = new MouseoverListener();
addMouseListener(ml);
button.addMouseListener(ml);
icon.addMouseListener(ml);
}
class MouseoverListener extends MouseAdapter {
@Override
public void mouseEntered(MouseEvent e) {
button.setVisible(true);
hoverImage.setVisible(true);
}
@Override
public void mouseExited(MouseEvent e) {
button.setVisible(false);
hoverImage.setVisible(false);
}
}
}
class Buttons extends JPanel {
public Buttons() {
setOpaque(false);
setBorder(new LineBorder(Color.BLACK));
}
@Override
protected void paintComponent(Graphics g) {
g.setColor(new Color(0, 0, 0, 100));
g.fillRect(0, 0, getWidth(), getHeight());
}
}
class Background extends JPanel {
private BufferedImage sprite = null;
public Background() {
try {
sprite = ImageIO.read(new URL("/image/XZ4V5.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Image scaledImage = sprite.getScaledInstance(getWidth(), getHeight(), Image.SCALE_REPLICATE);
g.drawImage(scaledImage, getWidth() / 2 - scaledImage.getWidth(this) / 2, getHeight() / 2 - scaledImage.getHeight(this) / 2, this);
}
}
static final Image QUEEN_IMG,
QUEEN_IMG_H,
KING_IMG,
KING_IMG_H,
ROOK_IMG,
ROOK_IMG_H,
KNIGHT_IMG,
KNIGHT_IMG_H,
PAWN_IMG,
PAWN_IMG_H;
static {
try {
// source for sprite sheet: https://stackoverflow.com/a/19209651/2891664
BufferedImage sprites = ImageIO.read(new URL("/image/memI0.png"));
int n = 64;
QUEEN_IMG = sprites.getSubimage(0 * n, 0, n, n);
QUEEN_IMG_H = sprites.getSubimage(0 * n, 64, n, n);
KING_IMG = sprites.getSubimage(1 * n, 0, n, n);
KING_IMG_H = sprites.getSubimage(1 * n, 64, n, n);
ROOK_IMG = sprites.getSubimage(2 * n, 0, n, n);
ROOK_IMG_H = sprites.getSubimage(2 * n, 64, n, n);
KNIGHT_IMG = sprites.getSubimage(3 * n, 0, n, n);
KNIGHT_IMG_H = sprites.getSubimage(3 * n, 64, n, n);
PAWN_IMG = sprites.getSubimage(4 * n, 0, n, n);
PAWN_IMG_H = sprites.getSubimage(4 * n, 64, n, n);
} catch (IOException x) {
throw new UncheckedIOException(x);
}
}
}
我用悬停做的小项目:https://www.dropbox.com/s/wq4ggufbxrfcvb8/HoverButton.zip?dl=0
最佳答案
最好的方法可能是使用 CardLayout
这已经是为了良好的行为而设置的。
这是一个 MCVE。
package mcve;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.net.*;
import java.io.*;
public class MouseoverPanels {
public static void main(String[] args) {
SwingUtilities.invokeLater(MouseoverPanels::new);
}
MouseoverPanels() {
JFrame frame = new JFrame("Mouseover Panels");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel buttons = new JPanel();
buttons.setBackground(Color.WHITE);
buttons.add(new MouseoverPanel("Queen", QUEEN_IMG));
buttons.add(new MouseoverPanel("King", KING_IMG));
buttons.add(new MouseoverPanel("Rook", ROOK_IMG));
buttons.add(new MouseoverPanel("Knight", KNIGHT_IMG));
buttons.add(new MouseoverPanel("Pawn", PAWN_IMG));
frame.setContentPane(buttons);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
class MouseoverPanel extends JPanel {
static final String BUTTON_KEY = "BUTTON";
static final String ICON_KEY = "ICON";
final CardLayout layout = new CardLayout();
MouseoverPanel(String text, Image img) {
setLayout(layout);
setOpaque(false);
JButton button = new JButton(text);
button.setBorderPainted(false);
button.setOpaque(false);
JLabel icon = new JLabel(new ImageIcon(img));
icon.setOpaque(false);
add(button, BUTTON_KEY);
add(icon, ICON_KEY);
layout.show(this, ICON_KEY);
MouseoverListener ml = new MouseoverListener();
addMouseListener(ml);
button.addMouseListener(ml);
icon.addMouseListener(ml);
}
class MouseoverListener extends MouseAdapter {
@Override
public void mouseEntered(MouseEvent e) {
recomputeView(e);
}
@Override
public void mouseExited(MouseEvent e) {
recomputeView(e);
}
void recomputeView(MouseEvent e) {
Component comp = e.getComponent();
Point loc = SwingUtilities.convertPoint(comp, e.getPoint(), MouseoverPanel.this);
String key = contains(loc) ? BUTTON_KEY : ICON_KEY;
layout.show(MouseoverPanel.this, key);
}
}
}
static final Image QUEEN_IMG,
KING_IMG,
ROOK_IMG,
KNIGHT_IMG,
PAWN_IMG;
static {
try {
// source for sprite sheet: https://stackoverflow.com/a/19209651/2891664
BufferedImage sprites = ImageIO.read(new URL("/image/memI0.png"));
int n = 64;
QUEEN_IMG = sprites.getSubimage(0 * n, 0, n, n);
KING_IMG = sprites.getSubimage(1 * n, 0, n, n);
ROOK_IMG = sprites.getSubimage(2 * n, 0, n, n);
KNIGHT_IMG = sprites.getSubimage(3 * n, 0, n, n);
PAWN_IMG = sprites.getSubimage(4 * n, 0, n, n);
} catch (IOException x) {
throw new UncheckedIOException(x);
}
}
}
我认为您所看到的性能下降可能是因为调用 setVisible
may cause revalidate
to be called ,它会调用布局管理器,但很难确定,尤其是没有适当的 MCVE ( see )。
这是另一个仅通过自定义绘画来完成此操作的示例。像这样的方法应该更容易制作动画。这通常是不可行的,除非我们在这里处理的唯一“真实”组件是文本本身。
package mcve;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.net.*;
import java.io.*;
import java.util.*;
public class MouseoverPanels2 {
public static void main(String[] args) {
SwingUtilities.invokeLater(MouseoverPanels2::new);
}
MouseoverPanels2() {
JFrame frame = new JFrame("Mouseover Panels");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel buttons = new JPanel();
buttons.setBackground(Color.WHITE);
buttons.add(new MouseoverLabel("Queen", QUEEN_IMG, QUEEN_IMG_H));
buttons.add(new MouseoverLabel("King", KING_IMG, KING_IMG_H));
buttons.add(new MouseoverLabel("Rook", ROOK_IMG, ROOK_IMG_H));
buttons.add(new MouseoverLabel("Knight", KNIGHT_IMG, KNIGHT_IMG_H));
buttons.add(new MouseoverLabel("Pawn", PAWN_IMG, PAWN_IMG_H));
frame.setContentPane(buttons);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
class MouseoverLabel extends JComponent {
final JLabel label;
final Image image, hover;
final Dimension imageMaxSize;
boolean isHovering = false;
MouseoverLabel(String text, Image image, Image hover) {
setLayout(new BorderLayout());
setOpaque(false);
this.image = Objects.requireNonNull(image);
this.hover = Objects.requireNonNull(hover);
imageMaxSize =
new Dimension(Math.max(image.getWidth(this), hover.getWidth(this)),
Math.max(image.getHeight(this), hover.getHeight(this)));
label = new JLabel(text);
label.setForeground(Color.RED);
label.setOpaque(false);
label.setHorizontalAlignment(JLabel.CENTER);
add(label, BorderLayout.CENTER);
MouseoverListener ml = new MouseoverListener();
addMouseListener(ml);
label.addMouseListener(ml);
}
private Dimension getMax(Dimension size) {
size.width = Math.max(size.width, imageMaxSize.width);
size.height = Math.max(size.height, imageMaxSize.height);
return size;
}
@Override
public Dimension getPreferredSize() {
return getMax(super.getPreferredSize());
}
@Override
public Dimension getMinimumSize() {
return getMax(super.getMinimumSize());
}
@Override
public Dimension getMaximumSize() {
return getMax(super.getMaximumSize());
}
@Override
protected void paintChildren(Graphics g) {
// Note that the label is always "visible",
// so it will e.g. receive mouse events even
// while we aren't painting it.
// If the label needs to receive, say, mouse
// clicks, then you need to check isHovering
// in the mouse click listener.
// If the label is a JButton, then you could
// call button.setEnabled(isHovering) in the
// MouseoverListener.
if (isHovering) {
paintImage(g, hover);
super.paintChildren(g);
} else {
paintImage(g, image);
}
}
private void paintImage(Graphics g, Image image) {
int w = image.getWidth(this);
int h = image.getHeight(this);
int x = (getWidth() - w) / 2;
int y = (getHeight() - h) / 2;
g.drawImage(image, x, y, w, h, this);
}
class MouseoverListener extends MouseAdapter {
@Override
public void mouseEntered(MouseEvent e) {
recomputeView(e);
}
@Override
public void mouseExited(MouseEvent e) {
recomputeView(e);
}
void recomputeView(MouseEvent e) {
Component comp = e.getComponent();
Point loc = SwingUtilities.convertPoint(comp, e.getPoint(), MouseoverLabel.this);
isHovering = contains(loc);
repaint();
}
}
}
static final Image QUEEN_IMG,
QUEEN_IMG_H,
KING_IMG,
KING_IMG_H,
ROOK_IMG,
ROOK_IMG_H,
KNIGHT_IMG,
KNIGHT_IMG_H,
PAWN_IMG,
PAWN_IMG_H;
static {
try {
// source for sprite sheet: https://stackoverflow.com/a/19209651/2891664
BufferedImage sprites = ImageIO.read(new URL("/image/memI0.png"));
int n = 64;
QUEEN_IMG = sprites.getSubimage(0 * n, 0, n, n);
QUEEN_IMG_H = sprites.getSubimage(0 * n, n, n, n);
KING_IMG = sprites.getSubimage(1 * n, 0, n, n);
KING_IMG_H = sprites.getSubimage(1 * n, n, n, n);
ROOK_IMG = sprites.getSubimage(2 * n, 0, n, n);
ROOK_IMG_H = sprites.getSubimage(2 * n, n, n, n);
KNIGHT_IMG = sprites.getSubimage(3 * n, 0, n, n);
KNIGHT_IMG_H = sprites.getSubimage(3 * n, n, n, n);
PAWN_IMG = sprites.getSubimage(4 * n, 0, n, n);
PAWN_IMG_H = sprites.getSubimage(4 * n, n, n, n);
} catch (IOException x) {
throw new UncheckedIOException(x);
}
}
}
关于java setOpaque(false) 使悬停速度变慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49615105/
class test { public static void main(String[] args){ Object o1 = new Object(); O
我以为我理解了 Python 中的这两个单例值,直到我看到有人在代码中使用 return l1 or l2,其中 l1 和 l2 都是链表对象,并且(s)他想如果不为 None 则返回 l1,否则返回
这个问题在这里已经有了答案: Why does the expression 0 >> (True == False) is False True >>> True == (False is Fals
为什么在 Python 中它是这样评估的: >>> False is False is False True 但是当用括号尝试时表现如预期: >>> (False is False) is False
我有一个名为“apple”的表,我编写了以下查询: select name, count(name), case when istasty is null then fal
python boolean 逻辑中的运算符优先级 print(False==True or False) #answer is True print(False==(False or True))#
请不要看条件,因为它们在这里是为了便于理解行为 为什么 result 等于 true ? boolean result = false && (false)?false:true; 我知道我们可以通过
乍一看,这篇文章可能看起来像是重复的,但事实并非如此。相信我,我已经查看了所有 Stack Overflow,但都无济于事。 无论如何,我从 Html.CheckBoxFor 得到了一些奇怪的行为。
这个问题在这里已经有了答案: python operator precedence of in and comparison (4 个答案) 关闭 6 年前。 我的一位前辈演示了它,我想知道这是否是
我最近参加了 Java 的入门测试,这个问题让我很困惑。完整的问题是: boolean b1 = true; boolean b2 = false; if (b2 != b1 != b2) S
为什么 {} == false 评估为 false 而 [] == false 评估为 true在 javascript 中? 最佳答案 这是根据 Abstract Equality Comparis
这个问题在这里已经有了答案: Why does (1 in [1,0] == True) evaluate to False? (1 个回答) 关闭7年前。 为什么使用括号时这些语句按预期工作: >>
我试过搜索这个,但我真的不知道如何表达它以查看是否有其他人发布了答案。 但是,我正在制作一个国际象棋游戏和一个人工智能来配合它,这是非常困难的,我的问题是当我检查两个棋子是否在同一个团队时我必须做 (
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
为什么 为 false || null 返回与 null || 不同的结果错误? 我可以安全地依赖 return myVar || false 如果 myVar 为 null 或 false,则返回
我正在尝试遵循 NHibernate 教程,“你的第一个基于 NHibernate 的应用程序:修订 #4”在 NHibernate Forge。 但线路:new SchemaExport(cfg).
这个问题在这里已经有了答案: Empty list boolean value (3 个答案) 关闭 4 年前。 我是 Python 的新手,不理解以下行为: 为什么要声明 [] == False
以下函数循环访问对象的值。如果值为空this.hasInvalidValue设置为true ,如果不为空 this.hasInvalidValue设置为false : user: { email:
所以我正在玩 java.lang.reflect 东西并尝试制作类似 this 的东西。这是我的问题(可能是一个错误): 将字段设置为 true 的方法的代码: private static void
当我在编程时,我的 if 语句出现了意想不到的结果。 这个代码警报怎么会是真的?我在 W3S 没有找到任何可以帮助我的东西,我真的很想知道为什么这些警报是“正确的” window.alert(fals
我是一名优秀的程序员,十分优秀!