有没有办法删除 JOptionPane 中按钮上的焦点?
Is there a way to remove that focus on the button in a JOptionPanel?
有没有办法移除 JOptionPanel
中按钮上的焦点?我真的很想删除它。我看起来很碍眼。 JOptionPanel
有我的代码
JOptionPane.showMessageDialog(
null,
mainPanel,
"CREDITS (づ ̄ ³ ̄)づ ",
JOptionPane.PLAIN_MESSAGE);
在这种情况下,您可能需要一个全局事件侦听器,以便在消息对话框出现时清除焦点。这是一个例子:
import java.awt.AWTEvent;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.FocusEvent;
import javax.swing.FocusManager;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
public class TestFocus {
private final AWTEventListener focusListener = this::removeFocus;
public static void main(String[] args) {
SwingUtilities.invokeLater(new TestFocus()::initUI);
}
private void initUI() {
Toolkit.getDefaultToolkit().addAWTEventListener(focusListener, AWTEvent.FOCUS_EVENT_MASK);
JOptionPane.showMessageDialog(null, "Here is no Focus on button", "No focus!", JOptionPane.PLAIN_MESSAGE);
Toolkit.getDefaultToolkit().removeAWTEventListener(focusListener); // to be safe remove it twice.
}
private void removeFocus(AWTEvent e) {
if (e instanceof FocusEvent && ((FocusEvent) e).getID() == FocusEvent.FOCUS_GAINED) {
Toolkit.getDefaultToolkit().removeAWTEventListener(focusListener);
FocusManager.getCurrentKeyboardFocusManager().clearFocusOwner();
}
}
}
有没有办法移除 JOptionPanel
中按钮上的焦点?我真的很想删除它。我看起来很碍眼。 JOptionPanel
JOptionPane.showMessageDialog(
null,
mainPanel,
"CREDITS (づ ̄ ³ ̄)づ ",
JOptionPane.PLAIN_MESSAGE);
在这种情况下,您可能需要一个全局事件侦听器,以便在消息对话框出现时清除焦点。这是一个例子:
import java.awt.AWTEvent;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.FocusEvent;
import javax.swing.FocusManager;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
public class TestFocus {
private final AWTEventListener focusListener = this::removeFocus;
public static void main(String[] args) {
SwingUtilities.invokeLater(new TestFocus()::initUI);
}
private void initUI() {
Toolkit.getDefaultToolkit().addAWTEventListener(focusListener, AWTEvent.FOCUS_EVENT_MASK);
JOptionPane.showMessageDialog(null, "Here is no Focus on button", "No focus!", JOptionPane.PLAIN_MESSAGE);
Toolkit.getDefaultToolkit().removeAWTEventListener(focusListener); // to be safe remove it twice.
}
private void removeFocus(AWTEvent e) {
if (e instanceof FocusEvent && ((FocusEvent) e).getID() == FocusEvent.FOCUS_GAINED) {
Toolkit.getDefaultToolkit().removeAWTEventListener(focusListener);
FocusManager.getCurrentKeyboardFocusManager().clearFocusOwner();
}
}
}