📄 comboboxrenderer.java
字号:
package hartech.kids.grapher;
import java.awt.*;
import javax.swing.*;
/**
* <p>
* Title:
* </p>
*
* <p>
* Description:
* </p>
*
* <p>
* Date: 2006-09-09
* </p>
*/
// 该类为了显示图标下拉菜单选项
public class ComboBoxRenderer extends JLabel implements ListCellRenderer {
/**
*
*/
private static final long serialVersionUID = 4503154021526895793L;
private Font uhOhFont;
private ImageIcon[] images;
public ComboBoxRenderer() {
setOpaque(true);
setHorizontalAlignment(CENTER);
setVerticalAlignment(CENTER);
images = new ImageIcon[Main.icon_num];
for (int i = 0; i < Main.icon_num; i++) {
images[i] = createImageIcon("images/smiles/" + i + ".gif");
}
}
static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = Main.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
/*
* This method finds the image and text corresponding to the selected value
* and returns the label, set up to display the text and image.
*/
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
// Get the selected index. (The index param isn't
// always valid, so just use the value.)
int selectedIndex = ((Integer) value).intValue();
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
// Set the icon and text. If icon was null, say so.
ImageIcon icon = images[selectedIndex];
setIcon(icon);
return this;
}
// Set the font and text when no image was found.
protected void setUhOhText(String uhOhText, Font normalFont) {
if (uhOhFont == null) { // lazily create this font
uhOhFont = normalFont.deriveFont(Font.ITALIC);
}
setFont(uhOhFont);
setText(uhOhText);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -