[英]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 :有关情况的更多详细信息,对于那些想要一些的人:
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...例如...
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
“普通”按钮只是一个普通的旧JButton
, Hacked
将rollOver
设置为启用,并且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.