【问题标题】:Implementing a simple hover effect on a Jlabel在 Jlabel 上实现简单的悬停效果
【发布时间】:2014-01-17 01:56:30
【问题描述】:

我正在为一个辅助项目提出一些想法,我想使用 Java swing 创建一个 GUI,看起来不像来自 Windows95。我的想法之一是使用 JLabels 作为按钮而不是标准的 JButton。这将允许我根据自己的喜好自定义悬停、拖动和移动效果。

MouseAdapter class 的研究应该可以让我做我想做的一切,不幸的是我在实现悬停效果时遇到了一些麻烦,因为JLabel 似乎没有更新。我曾尝试通过调用frame.update(getGraphics()); 直接更新框架,但这似乎不像我认为的那样有效。

我能否就如何正确更新标签获得一些建议。

注意:这只是一个示例,没有付出任何努力来有效地组织代码

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);
    }
}

【问题讨论】:

  • JLabel 默认是透明的。考虑禁用按钮内容和birder...
  • 参考以下链接对你有帮助[link]stackoverflow.com/questions/11678722/…
  • 出于花哨的原因使用标签组件而不是按钮在 gui 开发中是一种不好的做法。想想那些喜欢键盘导航的人。您的所有要求都可以通过JButton 来实现。

标签: java swing colors jlabel mouselistener


【解决方案1】:

在 mKorbel 的回答之上...

我不知道你为什么要付出这么多努力,实际上你可以让一个按钮看起来像一个标签。

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class NotALabel {

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

    public NotALabel() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                final JButton btn = new JButton("Am I label or a button?");
                btn.setContentAreaFilled(false);
                btn.setBorderPainted(false);
                btn.setFocusPainted(false);
                btn.setOpaque(true);
                btn.getModel().addChangeListener(new ChangeListener() {
                    @Override
                    public void stateChanged(ChangeEvent e) {
                        ButtonModel model = (ButtonModel) e.getSource();
                        if (model.isRollover()) {
                            btn.setBackground(Color.CYAN);
                        } else {
                            btn.setBackground(null);
                        }
                    }
                });

                btn.addActionListener(new ActionListener() {
                    private int count = 0;

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        count++;
                        ((JButton) e.getSource()).setText("I'm a super button!! Or label...");
                    }
                });

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                frame.add(btn);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

您还应该考虑尝试Setting the look and feel 甚至可能Modifying the look and feel

【讨论】:

  • 这是一个非常有趣的答案,因为它包含了几个我以前没有遇到过的概念。我一直在自学 swing 包,并一直在使用 WindowBuilder 来协助创建 GUI。什么是 EventQueue.invokeLater,为什么要使用它?
  • +1 ,但是 setBackground(null) 使用 UIManager 而不是 woodoo 的 -1k,也许 getModel.isRollover 比(然后无用且适得其反)MouseListener 更好
  • @mKorbel 什么setBackground(null)?我同意,该模型可能是一个更好的解决方案,但最好的解决方案是使用自定义 UI 委托;)
  • @JME 看看Initial Threads
  • JButton.setBackground(null) 将背景重置为实际的 L&F 颜色数组(用于 JButton)
【解决方案2】:
  1. Windowawt.Window 的保留名称,将此名称更改为例如MyWindow

  2. JPanel 实现了FlowLayout,你不能使用NullLayout 使用内置的LayoutManager,然后在JFrame.setVisible 之前使用JFrame.pack() 以在屏幕上正确调整大小

    李>
  3. JLabel 是透明的,使用 JLabel.setOpaque(true); 更改它

  4. 如果没有JLabel.repaint() 作为具体mouse_event 中的最后一个代码行,则无法从Mouse over/hover 刷新Backgroung Colorrepaint()JLabel API 中丢失

【讨论】:

    猜你喜欢
    • 2011-08-30
    • 2012-06-25
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 2014-11-08
    • 2016-11-08
    • 2012-06-05
    • 1970-01-01
    相关资源
    最近更新 更多