【问题标题】:Creating an invisible button in Java Swing在 Java Swing 中创建一个不可见的按钮
【发布时间】:2011-06-02 02:53:34
【问题描述】:

我在 Java Swing GUI 上多次看到看起来像图像的按钮,直到鼠标滑过它们。他们似乎没有填满他们的内容区域。当鼠标滚过它们时,内容区域会填满,但按钮看起来好像鼠标没有悬停在它上面。一个例子是用于激活 SwingSet2 中每个演示的按钮工具栏。

这种效果怎么可能?

编辑:以下是说明我的问题的图片:

默认状态

悬停状态

点击(活动)状态

这些是在 Vista 上使用 Window Look&Feel。这些图像是从 SwingSet2 演示工具栏捕获的。

谢谢!

【问题讨论】:

标签: java user-interface swing button


【解决方案1】:

这是一个简单的方法。此代码将向 JButton 添加调整大小的图像并删除按钮的边框和填充区域,除非鼠标在按钮上。

 public JPanel hooverImages(){
        JPanel panel;
        ImageIcon icon;
        Image img;
        JButton button;
        String name = "image"


        icon = new ImageIcon(getClass().getResource("/gui/resources/"
                + name+".png"));
        img = icon.getImage(); 

        button = new JButton(new ImageIcon(img.getScaledInstance(30, 30
                , Image.SCALE_SMOOTH))); //adds resized image to button.
        button.setBounds(20, 0, 40, 40);
        button.setContentAreaFilled(false); //this is the piece of code you needed
        button.addMouseListener(new MouseAdapter(){
            @Override
            public void mouseEntered(MouseEvent event){
                JButton buton = (JButton)event.getSource();
                buton.setContentAreaFilled(true);  //when hoovered it will show borders and fill area.
            }

            @Override
            public void mouseExited(MouseEvent event){
                JButton buton = (JButton)event.getSource();
                buton.setContentAreaFilled(false); //when mouse is not on button then it will look the same.
            }
        });
        panel.add(button);

     return panel;
}

【讨论】:

    【解决方案2】:

    如果您将任何带有图标的 JToggleButton 添加到 JToolBar,它的行为将如下所示。如果您不希望它出现在工具栏中,请尝试setBorderPainted(false)。我知道它适用于 GTK LaF,请在 windows 上尝试。

    【讨论】:

    • 谢谢,这回答了我的问题。
    【解决方案3】:

    我已经有一段时间没有玩过这些了,但我相信它来自 JButton 的 setRolloverIcon,它继承自 AbstractButton。我相信您也必须启用翻转功能。

    【讨论】:

    • 但是我会传递什么图标作为参数?
    • 鼠标悬停时显示不同的图像。运行 SwingSet2 并转到按钮演示(工具栏上的第二个)。确保您正在查看按钮子选项卡,然后查看源代码选项卡。那里有几个针对不同状态使用不同图标的示例:按下、翻转、禁用等。每个都从不同的 .gif 文件中获得一个新的 imageIcon。下载它的源代码,您应该能够打开各个 .gif 文件,看看它们对每个文件做了什么。
    【解决方案4】:

    更新 这就是你想要的。 Flat Button

    我知道它说的是 Windows L&F,您可以在 SwingSet2 中设置它,但这并不意味着 SwingSet2 没有替换一些底层 UI 处理程序来获得它们在使用 Windows L&F 时所需的效果。

    原始回复

    没有图片的粗略版本。更改边框和颜色以满足您的需求。

     public static void main(String[] args) {
        JFrame frame = new JFrame("Example");
        frame.setLayout(null);
        frame.setSize(800, 600);
        JButton button = new JButton("Hover Me");
        button.setBackground(Color.LIGHT_GRAY);
        button.setBounds(10, 10, 60, 40);
        button.setBorder(BorderFactory.createEmptyBorder());
        button.addMouseListener(new MouseAdapter() {
    
            @Override
            public void mouseEntered(MouseEvent e) {
                JButton button = (JButton) e.getComponent();
                button.setBackground(Color.LIGHT_GRAY.brighter());
                button.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
            }
    
            @Override
            public void mouseExited(MouseEvent e) {
                JButton button = (JButton) e.getComponent();
                button.setBackground(Color.LIGHT_GRAY);
                button.setBorder(BorderFactory.createEmptyBorder());
            }
        });
        frame.add(button);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
    }
    

    【讨论】:

    • 这是另一种方法,如果您想做一些更花哨或更复杂的事情,而不仅仅是将按钮图像换成另一个处于不同状态的图像。一旦你深入听众,你可以做各种各样的戏剧。
    • 但是如何让这个效果看起来像现在的 LAF?如果您查看 Windows 上程序的工具栏,您会发现您所看到的只是图标,直到您将它们翻过来,此时它们看起来就像按钮。我会在我的问题中添加图片。
    • 这种“扁平按钮”效果几乎是我想要的;我会调查一下。我不知道这些按钮叫什么,所以我称它们为隐形按钮。
    【解决方案5】:

    右键单击 jButton,单击属性并将 Opacity 设置为 false。 要么 将此添加到您的代码中。 JButton.setvisible(false);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-25
      • 1970-01-01
      • 1970-01-01
      • 2022-11-13
      • 1970-01-01
      • 1970-01-01
      • 2020-05-15
      • 1970-01-01
      相关资源
      最近更新 更多