简体   繁体   English

在Java Swing中创建一个不可见的按钮

[英]Creating an invisible button in Java Swing

I have seen many times on Java Swing GUIs buttons that look like images until the mouse rolls over them. 我已经多次看到Java Swing GUI按钮上的按钮看起来像图像,直到鼠标滑过它们为止。 They appear to not have their content area filled. 他们似乎没有填充其内容区域。 When the mouse rolls over them, the content area fills in but the button looks as though the mouse is not hovering over it. 当鼠标滑过它们时,内容区域将填充,但按钮看起来好像鼠标没有悬停在其上。 An example of this would be the toolbar of buttons used to activate each demo in SwingSet2. 例如,用于激活SwingSet2中每个演示的按钮工具栏。

How is this effect possible? 这怎么可能?

EDIT: Here are images that illustrate my question: 编辑:这是说明我的问题的图像:

Default State 默认状态
button_default.png

Hover State 悬停状态
button_hover.png

Clicked (Active) State 点击(活动)状态
button_active.png

These are using the Window Look&Feel on Vista. 这些正在Vista上使用Window Look&Feel。 The images were captured from the SwingSet2 demo toolbar. 图像是从SwingSet2演示工具栏中捕获的。

Thanks! 谢谢!

Here is an easy way to do it. 这是一种简单的方法。 This code will add a resized image to the JButton and remove the button's borders and filled area except when the mouse is on the button. 此代码将为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;
}

right click on the jButton, click properties and set Opacity to false. 右键单击jButton,单击“属性”,然后将“不透明度”设置为false。 OR add this to your code. 或将此添加到您的代码。 JButton.setvisible(false); JButton.setvisible(假);

It has been a while since I've played with any of that, but I believe it is from JButton's setRolloverIcon inherited from AbstractButton. 自从我玩过一段时间以来,已经有一段时间了,但是我相信这是从JButton的setRolloverIcon继承的,而该setRolloverIcon是从AbstractButton继承的。 I believe you have to enable rollover as well. 我相信您也必须启用翻转。

Update This is what you want. 更新这就是您想要的。 Flat Button 平面按钮

I know it says the Windows L&F, which you can set in SwingSet2, but it doesn't mean SwingSet2 didn't replace some of the underlying UI handlers to gain the effect they needed along with using the Windows L&F. 我知道它说的是Windows L&F,您可以在SwingSet2中进行设置,但这并不意味着SwingSet2不会替换某些底层的UI处理程序来获得使用Windows L&F所需的效果。

Original Response 原始回应

Rough version with no pictures. 没有图片的粗糙版本。 Change border and colors to suite your needs. 更改边框和颜色以适合您的需求。

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

If you add any JToggleButton with an icon to a JToolBar it will behave like this. 如果将带有图标的JToggleButton添加到JToolBar,它将表现为以下行为。 If you don't want it to be in a tool bar, try setBorderPainted(false) . 如果您不希望它出现在工具栏中,请尝试setBorderPainted(false) I know it works on a GTK LaF, try it with windows. 我知道它可以在GTK LaF上运行,请与Windows一起尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM