下面列出了javax.swing.AbstractButton#setMnemonic ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Wrapper for AbstractButton.setMnemonic and JLabel.setDisplayedMnemonic
* @param item AbstractButton/JLabel
* @param mnem Mnemonic char to set, latin [a-z,A-Z], digit [0-9], or any VK_ code
*/
private static void setMnemonic(Object item, int mnem) {
if (isAquaLF()) {
// there shall be no mnemonics on macosx.
//#55864
return;
}
if ((mnem >= 'a') && (mnem <= 'z')) {
mnem = mnem + ('A' - 'a');
}
if (item instanceof AbstractButton) {
AbstractButton b = (AbstractButton) item;
b.putClientProperty(PROP_MNEMONIC, mnem);
b.setMnemonic(mnem);
} else {
((JLabel) item).setDisplayedMnemonic(mnem);
}
}
/**
* @noinspection ALL
*/
private void $$$loadButtonText$$$(AbstractButton component, String text) {
StringBuffer result = new StringBuffer();
boolean haveMnemonic = false;
char mnemonic = '\0';
int mnemonicIndex = -1;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == '&') {
i++;
if (i == text.length()) break;
if (!haveMnemonic && text.charAt(i) != '&') {
haveMnemonic = true;
mnemonic = text.charAt(i);
mnemonicIndex = result.length();
}
}
result.append(text.charAt(i));
}
component.setText(result.toString());
if (haveMnemonic) {
component.setMnemonic(mnemonic);
component.setDisplayedMnemonicIndex(mnemonicIndex);
}
}
protected SingleTargetProductDialog(AppContext appContext, String title, int buttonMask, String helpID, TargetProductSelectorModel model, boolean alwaysWriteOutput) {
super(appContext.getApplicationWindow(), title, buttonMask, helpID);
this.appContext = appContext;
targetProductSelector = new TargetProductSelector(model, alwaysWriteOutput);
String homeDirPath = SystemUtils.getUserHomeDir().getPath();
String saveDir = appContext.getPreferences().getPropertyString(SaveProductAsAction.PREFERENCES_KEY_LAST_PRODUCT_DIR, homeDirPath);
targetProductSelector.getModel().setProductDir(new File(saveDir));
if (!alwaysWriteOutput) {
targetProductSelector.getOpenInAppCheckBox().setText("Open in " + appContext.getApplicationName());
}
targetProductSelector.getModel().getValueContainer().addPropertyChangeListener(evt -> {
if (evt.getPropertyName().equals("saveToFileSelected") ||
evt.getPropertyName().equals("openInAppSelected")) {
updateRunButton();
}
});
AbstractButton button = getButton(ID_APPLY);
button.setText("Run");
button.setMnemonic('R');
updateRunButton();
}
/**
* @noinspection ALL
*/
private void $$$loadButtonText$$$(AbstractButton component, String text) {
StringBuffer result = new StringBuffer();
boolean haveMnemonic = false;
char mnemonic = '\0';
int mnemonicIndex = -1;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == '&') {
i++;
if (i == text.length()) break;
if (!haveMnemonic && text.charAt(i) != '&') {
haveMnemonic = true;
mnemonic = text.charAt(i);
mnemonicIndex = result.length();
}
}
result.append(text.charAt(i));
}
component.setText(result.toString());
if (haveMnemonic) {
component.setMnemonic(mnemonic);
component.setDisplayedMnemonicIndex(mnemonicIndex);
}
}
/**
* @noinspection ALL
*/
private void $$$loadButtonText$$$(AbstractButton component, String text) {
StringBuffer result = new StringBuffer();
boolean haveMnemonic = false;
char mnemonic = '\0';
int mnemonicIndex = -1;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == '&') {
i++;
if (i == text.length()) break;
if (!haveMnemonic && text.charAt(i) != '&') {
haveMnemonic = true;
mnemonic = text.charAt(i);
mnemonicIndex = result.length();
}
}
result.append(text.charAt(i));
}
component.setText(result.toString());
if (haveMnemonic) {
component.setMnemonic(mnemonic);
component.setDisplayedMnemonicIndex(mnemonicIndex);
}
}
/**
* @noinspection ALL
*/
private void $$$loadButtonText$$$(AbstractButton component, String text) {
StringBuffer result = new StringBuffer();
boolean haveMnemonic = false;
char mnemonic = '\0';
int mnemonicIndex = -1;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == '&') {
i++;
if (i == text.length()) break;
if (!haveMnemonic && text.charAt(i) != '&') {
haveMnemonic = true;
mnemonic = text.charAt(i);
mnemonicIndex = result.length();
}
}
result.append(text.charAt(i));
}
component.setText(result.toString());
if (haveMnemonic) {
component.setMnemonic(mnemonic);
component.setDisplayedMnemonicIndex(mnemonicIndex);
}
}
/**
* @noinspection ALL
*/
private void $$$loadButtonText$$$(AbstractButton component, String text) {
StringBuffer result = new StringBuffer();
boolean haveMnemonic = false;
char mnemonic = '\0';
int mnemonicIndex = -1;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == '&') {
i++;
if (i == text.length()) break;
if (!haveMnemonic && text.charAt(i) != '&') {
haveMnemonic = true;
mnemonic = text.charAt(i);
mnemonicIndex = result.length();
}
}
result.append(text.charAt(i));
}
component.setText(result.toString());
if (haveMnemonic) {
component.setMnemonic(mnemonic);
component.setDisplayedMnemonicIndex(mnemonicIndex);
}
}
/**
* @noinspection ALL
*/
private void $$$loadButtonText$$$(AbstractButton component, String text) {
StringBuffer result = new StringBuffer();
boolean haveMnemonic = false;
char mnemonic = '\0';
int mnemonicIndex = -1;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == '&') {
i++;
if (i == text.length()) break;
if (!haveMnemonic && text.charAt(i) != '&') {
haveMnemonic = true;
mnemonic = text.charAt(i);
mnemonicIndex = result.length();
}
}
result.append(text.charAt(i));
}
component.setText(result.toString());
if (haveMnemonic) {
component.setMnemonic(mnemonic);
component.setDisplayedMnemonicIndex(mnemonicIndex);
}
}
/**
* @noinspection ALL
*/
private void $$$loadButtonText$$$(AbstractButton component, String text) {
StringBuffer result = new StringBuffer();
boolean haveMnemonic = false;
char mnemonic = '\0';
int mnemonicIndex = -1;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == '&') {
i++;
if (i == text.length()) break;
if (!haveMnemonic && text.charAt(i) != '&') {
haveMnemonic = true;
mnemonic = text.charAt(i);
mnemonicIndex = result.length();
}
}
result.append(text.charAt(i));
}
component.setText(result.toString());
if (haveMnemonic) {
component.setMnemonic(mnemonic);
component.setDisplayedMnemonicIndex(mnemonicIndex);
}
}
/**
* Apply a shortcut to a button.
*
* @param button Button.
* @param shortcut Shortcut.
* @param message Related message.
*/
private static void setShortcut(
AbstractButton button,
ShortcutProperties shortcut,
String message) {
if ((shortcut == null) || (shortcut.useMnemonic())) {
int mnemonic = getMnemonic(message);
if ((mnemonic == -1) && (shortcut != null)) {
mnemonic = shortcut.getKey();
}
if (mnemonic != -1) {
button.setMnemonic(mnemonic);
}
} else if (shortcut.getEnabled()) {
String actionName = "action_" + shortcut.getName();
InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(KeyStroke.getKeyStroke(shortcut.getKey(), shortcut.getModifiers()), actionName);
button.getActionMap().put(actionName, new ActionClick(button));
if (message != null) {
int index = message.indexOf(shortcut.getKey());
if ((index >= 0) && (index < button.getText().length())) {
button.setDisplayedMnemonicIndex(index);
}
}
}
}
/** Parse text that has the mnemonic marked with a preceding'&'
(like in Qt) and set the text and mnemonic of the button. */
public static void setTextAndMnemonic(AbstractButton button, String text)
{
int pos = text.indexOf('&');
text = text.replace("&", "");
button.setText(text);
if (pos >= 0 && pos < text.length())
{
String mnemonic = text.substring(pos, pos + 1).toUpperCase();
KeyStroke keyStroke = KeyStroke.getKeyStroke(mnemonic);
int code = keyStroke.getKeyCode();
button.setMnemonic(code);
button.setDisplayedMnemonicIndex(pos);
}
}
public void propertyChange(PropertyChangeEvent evt) {
AbstractButton b = (AbstractButton) evt.getSource();
if (b.getDisplayedMnemonicIndex() == -1) {
Integer mnemonic = (Integer) b.getClientProperty(PROP_MNEMONIC);
Integer index = (Integer) b.getClientProperty(PROP_DISPLAYED_MNEMONIC_INDEX);
if (mnemonic != null && index != null && Utilities.compareObjects(b.getText(), b.getClientProperty(PROP_TEXT))) {
b.setMnemonic(mnemonic);
b.setDisplayedMnemonicIndex(index);
}
}
}
/**
* Actual setter of the text & mnemonics for the AbstractButton or
* their subclasses. We must copy necessary code from org.openide.awt.Mnemonics
* because org.openide.awt module is not available yet when this code is called.
* @param item AbstractButton
* @param text new label
*/
static void setLocalizedText (AbstractButton button, String text) {
if (text == null) {
button.setText(null);
return;
}
int i = findMnemonicAmpersand(text);
if (i < 0) {
// no '&' - don't set the mnemonic
button.setText(text);
button.setMnemonic(0);
} else {
button.setText(text.substring(0, i) + text.substring(i + 1));
if (Utilities.isMac()) {
// there shall be no mnemonics on macosx.
//#55864
return;
}
char ch = text.charAt(i + 1);
// it's latin character or arabic digit,
// setting it as mnemonics
button.setMnemonic(ch);
// If it's something like "Save &As", we need to set another
// mnemonic index (at least under 1.4 or later)
// see #29676
button.setDisplayedMnemonicIndex(i);
}
}
/**
* Set mnemonic on button in a platform dependant manner.
*
* @param button
* Button
* @param mnemonic
* Mnemonic
* @param index
* Index of string to underline
*/
public static void setMnemonic(AbstractButton button, char mnemonic, int index) {
/*
* Only set mnemonic if not using macOS - they are not recommended by
* the style guidelines there and clash with established commands
*/
if (!OperatingSystem.isMacOs()) {
button.setMnemonic(mnemonic);
if (index >= 0) {
button.setDisplayedMnemonicIndex(index);
}
}
}
/**
* @param m the button item to change the text, short description and mnemonic
* @param substitutes substitutes to use in a message format
* @param prop key bundle to use
*/
public static void name(AbstractButton m, String prop, Object... substitutes)
{
m.setText(getName(prop, substitutes));
String shortDesc = getShortDesc(prop, substitutes);
if (shortDesc != null && !shortDesc.isEmpty())
{
m.setToolTipText(shortDesc);
}
m.setMnemonic(getMnemonic(prop));
}
private void $$$loadButtonText$$$( AbstractButton component, String text )
{
StringBuffer result = new StringBuffer();
boolean haveMnemonic = false;
char mnemonic = '\0';
int mnemonicIndex = -1;
for( int i = 0; i < text.length(); i++ )
{
if( text.charAt( i ) == '&' )
{
i++;
if( i == text.length() )
{
break;
}
if( !haveMnemonic && text.charAt( i ) != '&' )
{
haveMnemonic = true;
mnemonic = text.charAt( i );
mnemonicIndex = result.length();
}
}
result.append( text.charAt( i ) );
}
component.setText( result.toString() );
if( haveMnemonic )
{
component.setMnemonic( mnemonic );
component.setDisplayedMnemonicIndex( mnemonicIndex );
}
}
/**
* Localise the given AbstractButton, setting the text and optionally
* mnemonic Note that AbstractButton includes menus and menu items.
*
* @param button
* The button to localise
* @param key
* The key to look up in resource bundle
* @param defaultString
* default String to use if key not found
* @param setMnemonic
* whether or not to set the mnemonic. According to Sun's
* guidelines, default/cancel buttons should not have mnemonics
* but instead should use Return/Escape
*/
public static void localiseButton(AbstractButton button, String key, String defaultString, boolean setMnemonic) {
AnnotatedString as = new AnnotatedString(L10N.getLocalString(key, defaultString));
button.setText(as.toString());
int mnemonic;
if (setMnemonic && (mnemonic = as.getMnemonic()) != KeyEvent.VK_UNDEFINED) {
button.setMnemonic(mnemonic);
button.setDisplayedMnemonicIndex(as.getMnemonicIndex());
}
}
/**
* Updates/sets the text of an abstract button based on the key of its text.
*
* <p>The button's text will be taken from the {@link Language} specified by the <code>textKey</code>.<br>
* If the text of the button designates a mnemonic, it will be set properly.</p>
*
* @param button abstract button whose text to be updated
* @param textKey key of the button's text
*/
public static void updateButtonText( final AbstractButton button, final String textKey, Object... arguments ) {
final Pair< String, Character > textAndMnemonic = Language.getTextAndMnemonic( textKey, arguments );
button.setText( textAndMnemonic.value1 );
if ( textAndMnemonic.value2 != null )
button.setMnemonic( textAndMnemonic.value2 );
}
/**
* Sets the resources on a subclass of {@link AbstractButton}. The common
* classes are {@link javax.swing.JRadioButton}, {@link javax.swing.JButton},
* and {@link javax.swing.JCheckBox}
* <p/>
* This method sets the text and mnemonic.
*
* @param button The button on which to set the text and mnemonoic
* @param labelText the text which contains the displayed text and mnemonic
* @see AbstractButton#setText(String)
* @see AbstractButton#setMnemonic(int)
*/
public static void resButton(AbstractButton button, String labelText) {
button.setText(stripMnemonic(labelText));
if (Spark.isWindows()) {
button.setMnemonic(getMnemonicKeyCode(labelText));
}
}
/**
* Shortcut function to translate a button type component.
* @param btn Button type component.
* @param key Key of the button text string.
* @param args Text string arguments values.
*/
public static void tButton(AbstractButton btn, String key, Object[] args) {
btn.setText(t(key, args));
btn.setMnemonic(mnemonic(key));
}