前言:沒事在看《Java Swing
nd Edition》
看到有個東西還是蠻有趣的說
就是Accelerator key
呵呵
好像要配合Alt一起用才能起到作用
蠻多東西是看了程序後才發現原文的意思的
這次算是插曲
不在MapsEditor的范圍之內
順便提一下
中午起來有
到
個人加了群
蠻開心的
高手也好初學也罷
大家重要的是交流
大膽說出自己的想法
錯了就錯了
誰沒錯過啊……
關於Accelerator key
在做GUI的時候還是比較重要的吧
至少以前認為那些不用鼠標的人蠻高手的
呵呵
自己也會去刻意記些常用的快捷鍵
在Windows下用用還算湊合那
呵呵
程序是從書上搬下來的
覺得蠻有意思的
大家可以看看

// MnemonicLabels
java

//

import javax
swing
*;

import java
awt
*;


// Shows how displayedMnemonic and labelFor properties work together


public class MnemonicLabels
{


public static void main(String[] args)
{

JTextField firstField = new JTextField(
);

JTextField middleField = new JTextField(
);

JTextField lastField = new JTextField(
);

// Create labels and mnemonics

JLabel firstLabel = new JLabel(
First Name
JLabel
RIGHT);

firstLabel
setDisplayedMnemonic(
F
);

firstLabel
setLabelFor(firstField);

JLabel middleLabel = new JLabel(
Middle Initial
JLabel
RIGHT);

middleLabel
setDisplayedMnemonic(
I
);

middleLabel
setDisplayedMnemonicIndex(
); // Requires

middleLabel
setLabelFor(middleField);

JLabel lastLabel = new JLabel(
Last Name
JLabel
RIGHT);

lastLabel
setDisplayedMnemonic(
L
);

lastLabel
setLabelFor(lastField);

// Layout and display

JPanel p = new JPanel( );

p
setLayout(new GridLayout(
));

p
add(firstLabel);

p
add(firstField);

p
add(middleLabel);

p
add(middleField);

p
add(lastLabel);

p
add(lastField);

JFrame f = new JFrame(
MnemonicLabels
);

f
setDefaultCloseOperation(JFrame
EXIT_ON_CLOSE);

f
setContentPane(p);

f
pack( );

f
setVisible(true);

}

}
有Java環境的可以拷貝去然後編譯運行看看
然後分別按Alt+F
Alt+I
Alt+L
Foucs就會在三個TextField之間轉換
關鍵的是firstLabel
setDisplayedMnemonic(
F
)和firstLabel
setLabelFor(firstField)
前一個setDisplayedMnemonic(char)選定哪個character為快捷鍵
就像VC裡面的
&
一樣的功能
而後面這個setLabelFor(comp)則表明你按下相應的快捷鍵(加速鍵)後焦點定位到哪個控件上(當然要有焦點的控件)
需要注意的是第二個快捷鍵的設置
middleLabel
setDisplayedMnemonicIndex(
);
這個需要SDK
的支持
這個的作用就是設置顯示的下劃線為第幾個字符
很明顯
我們注意到對於
I
來說
Middle Initial
中前後有兩個
I
而這裡是部分大小寫的
所以按照第一個或者第三個的做法的話
程序會默認JLabel中文字滿足的第一個字符加下劃線
也就是這個樣子了
Middle Initial
我們要的是
Middle Initial
的效果
所以把下劃線設置到第七個字符
I
上面
其他也沒什麼好玩的了
除了這個應該都能摸索懂吧
呵呵
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26000.html