📄 intchooser.java
字号:
package cn.bz.util.swing;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import java.util.Vector;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import cn.bz.util.IntEvent;
class IntChooser extends JPanel implements Java3DExplorerConstants {
JComboBox combo;
String[] choiceNames;
int[] choiceValues;
int current;
Vector listeners = new Vector();
IntChooser(String name, String[] initChoiceNames, int[] initChoiceValues,
int initValue) {
if ((initChoiceValues != null)
&& (initChoiceNames.length != initChoiceValues.length)) {
throw new IllegalArgumentException(
"Name and Value arrays must have the same length");
}
choiceNames = new String[initChoiceNames.length];
choiceValues = new int[initChoiceNames.length];
System
.arraycopy(initChoiceNames, 0, choiceNames, 0,
choiceNames.length);
if (initChoiceValues != null) {
System.arraycopy(initChoiceValues, 0, choiceValues, 0,
choiceNames.length);
} else { //构造一个数组.将对应的序列号存入对应的索引里.
for (int i = 0; i < initChoiceNames.length; i++) {
choiceValues[i] = i;
}
}
// Create the combo box, select the init value
combo = new JComboBox(choiceNames);
combo.setSelectedIndex(current);
combo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox) e.getSource();
int index = cb.getSelectedIndex();
setValueIndex(index);
}
});
// set the initial value
current = 0;
setValue(initValue);
// layout to align left
setLayout(new BorderLayout());
Box box = new Box(BoxLayout.X_AXIS);
add(box, BorderLayout.WEST);
box.add(new JLabel(name));
box.add(combo);
}
IntChooser(String name, String[] initChoiceNames, int[] initChoiceValues) {
this(name, initChoiceNames, initChoiceValues, initChoiceValues[0]);
}
IntChooser(String name, String[] initChoiceNames, int initValue) {
this(name, initChoiceNames, null, initValue);
}
IntChooser(String name, String[] initChoiceNames) {
this(name, initChoiceNames, null, 0);
}
public void addIntListener(IntListener listener) {
listeners.add(listener);
}
public void removeIntListener(IntListener listener) {
listeners.remove(listener);
}
public void setValueByName(String newName) {
boolean found = false;
int newIndex = 0;
for (int i = 0; (!found) && (i < choiceNames.length); i++) {
if (newName.equals(choiceNames[i])) {
newIndex = i;
found = true;
}
}
if (found) {
setValueIndex(newIndex);
}
}
public void setValue(int newValue) {
boolean found = false;
int newIndex = 0;
for (int i = 0; (!found) && (i < choiceValues.length); i++) {
if (newValue == choiceValues[i]) {
newIndex = i;
found = true;
}
}
if (found) {
setValueIndex(newIndex);
}
}
public int getValue() {
return choiceValues[current];
}
public String getValueName() {
return choiceNames[current];
}
public void setValueIndex(int newIndex) {
boolean changed = (newIndex != current);
current = newIndex;
if (changed) {
combo.setSelectedIndex(current);
valueChanged();
}
}
private void valueChanged() {
// notify the listeners
IntEvent event = new IntEvent(this, choiceValues[current]);
for (Enumeration e = listeners.elements(); e.hasMoreElements();) {
IntListener listener = (IntListener) e.nextElement();
listener.intChanged(event);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -