简体   繁体   English

如何使 JButton 显示为“翻转”

[英]How to make a JButton appear "as rolled over"

A JButton has a different appearance when rolled over. JButton 在翻转时具有不同的外观。 That appearance is different from the "selected" appearance.该外观与“选定”外观不同。

I want to display my button "as if" it was rolled over, so that user understands that if he hits the Return key, that button will be triggered.我想显示我的按钮“好像”它被翻转了,这样用户就会明白,如果他按下 Return 键,该按钮将被触发。

The problem is not the same as setting the default button, because I am in a situation where I really want to get the user to understand that although he wouldn't expect it, if he hits enter that button will be activated.问题与设置默认按钮不同,因为我处于这样一种情况,我真的想让用户明白,尽管他不期望它,但如果他按下回车键,该按钮将被激活。 More details below for those who want some.下面为那些想要一些的人提供更多详细信息。 Setting button as default would make button the default one, but wouldn't be significantly signaling to the user.将按钮设置为默认值将使按钮成为默认按钮,但不会向用户发出明显的信号。

In my case the strong enough signal is the appearance that the button has when it is rolled over.在我的情况下,足够强的信号是按钮滚动时的外观。

How to do that ?怎么做 ?

More details on the situation, for those who want some :有关情况的更多详细信息,对于那些想要一些的人:

  • I have a list of buttons representing options, and a text box at the top, which acts as a filter on the buttons我有一个代表选项的按钮列表,顶部有一个文本框,用作按钮上的过滤器
  • when filter is such that only one option remains, hitting return directly clicks that option's button当过滤器只剩下一个选项时,直接按回车点击该选项的按钮
  • in reality user would have had to select the button with tab or arrow, and then hit enter.实际上,用户必须选择带有选项卡或箭头的按钮,然后按 Enter。
  • since that shortcut is not obvious I want to signal it to user由于该快捷方式并不明显,因此我想向用户发出信号

Based on your question, what you "really" want, is the JRootPane#setDefaultButton , which will highlight the button, in a OS specific manner and if the user presses the default "action" key (Enter in most cases) will call it's ActionListener根据您的问题,您“真正”想要的是JRootPane#setDefaultButton ,它将以操作系统特定的方式突出显示按钮,并且如果用户按下默认的“操作”键(在大多数情况下为 Enter)将调用它的ActionListener

For example...例如...

macOS 示例

The "normal" button is just a plain old JButton , the Hacked sets the rollOver to enabled and Default has been set as the default button for the JRootPane “普通”按钮只是一个普通的旧JButtonHackedrollOver设置为启用,并且Default已设置为JRootPane的默认按钮

As you can see, you're suggest fix does nothing on MacOS, don't know what it might do on other platforms正如你所看到的,你建议 fix 在 MacOS 上什么都不做,不知道它在其他平台上会做什么

I suggest you have a look at How to Use Root Panes for more details我建议您查看如何使用根窗格以获取更多详细信息

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

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

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

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

    public class TestPane extends JPanel {

        private JButton asDefault = new JButton("Default");

        public TestPane() {
            JButton hack = new JButton("Hacked");
            hack.getModel().setRollover(true);
            hack.setRolloverEnabled(true);
            asDefault.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Defaulted");
                }
            });
            add(new JButton("Normal"));

            add(hack);
            add(asDefault);
        }

        @Override
        public void addNotify() {
            super.addNotify();
            JRootPane rootPane = SwingUtilities.getRootPane(this);
            if (rootPane != null) {
                rootPane.setDefaultButton(asDefault);
            }
        }

    }
}

So using button.getModel().setRollover(true);所以使用button.getModel().setRollover(true); doesn't work on all platforms and on those platforms it does work on, I suspect the user will simply need to move the mouse through it to return it to normal不适用于所有平台,并且在它确实适用的那些平台上,我怀疑用户只需要通过它移动鼠标即可将其恢复正常

button.getModel().setRollover(true);

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

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