在Jlabel上实现一个简单的悬停效果 [英] Implementing a simple hover effect on a Jlabel

查看:228
本文介绍了在Jlabel上实现一个简单的悬停效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为一个侧面项目搞乱了一些想法,我想创建一个GUI使用Java swing,看起来不像是从Windows95。我开始的想法之一是使用JLabels作为按钮,而不是标准的JButton。这将允许我自定义悬停,拖动和移动效果,因为我喜欢。

I am messing around with some ideas for a side project and I would like to create a GUI using Java swing that doesn't look like it is from Windows95. One of the ideas I was kicking around was to use JLabels as buttons instead of the standard JButton. This would allow me to customize hover, drag, and movement effects as I like.

研究 MouseAdapter类应该允许我做我想做的一切,不幸的是我在实现悬停效果,因为我想要的 JLabel 没有更新。我已经尝试通过调用 frame.update(getGraphics()); 直接更新框架,但是这似乎不工作,我认为它。

Research into the MouseAdapter class should allow me to do everything I intend, unfortunately I am having some trouble implementing the hover effect as I wanted as the JLabel does not appear to update. I have tried updating the Frame directly by calling frame.update(getGraphics()); but that does not appear to work as I think it does.

我可以得到一些关于如何正确更新标签的建议。

Can I get some advice on how to update the label properly.

注意:这只是一个例子,不必花费时间来组织代码。

Note: This is just an example with no effort put in to organize the code efficiently

public class Window extends JFrame {
    /**
     * 
     */
    private static final long serialVersionUID = 5259700796854880162L;
    private JTextField textField;
    private JLabel lblNewLabel;
    static Window frame;
    int i = 0;

    public Window() {

        JPanel panel = new JPanel();
        getContentPane().add(panel, BorderLayout.CENTER);
        panel.setLayout(null);

        lblNewLabel = new JLabel("New label");
        lblNewLabel.setBackground(Color.LIGHT_GRAY);
        lblNewLabel.setBounds(137, 38, 114, 70);
        panel.add(lblNewLabel);
        lblNewLabel.addMouseListener(new LabelAdapter());

        textField = new JTextField();
        textField.setBounds(122, 119, 86, 20);
        panel.add(textField);
        textField.setColumns(10);

    }

    private class LabelAdapter extends MouseAdapter {
        @Override
        public void mouseClicked(MouseEvent e) {
            textField.setText(String.valueOf(i));
            i++;
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            lblNewLabel.setBackground(Color.CYAN);

        }

        @Override
        public void mouseExited(MouseEvent e) {
            lblNewLabel.setBackground(Color.LIGHT_GRAY);
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        frame = new Window();

        frame.setSize(900, 700);
        frame.setVisible(true);
    }
}


推荐答案


  1. 窗口 awt.Window 的保留者名称,将此名称更改为 MyWindow

  1. Window is reserver name for awt.Window, change this name to e.g. MyWindow

JPanel c $ c> FlowLayout ,您不能使用 NullLayout 使用built_in LayoutManager,然后使用 JFrame.pack

JPanel has implemented FlowLayout, you can't to use NullLayout use built_in LayoutManager, then to use JFrame.pack() before JFrame.setVisible for proper sizing on the screen

刷新背景颜色鼠标悬停/悬停 c $ c> JLabel.repaint()作为混凝土中的最后一行代码 mouse_event repaint()缺少 JLabel API

refresh of Backgroung Color from Mouse over/hover isn't possible without JLabel.repaint() as last code line in concrete mouse_event, repaint() missing in JLabel API

这篇关于在Jlabel上实现一个简单的悬停效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆