JFontChooser java 自定义的字体选择器(四)
;
//布局控件_结束
//listener.....
/*用户选择字体*/
lstFont.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
current_fontName = (String) lstFont.getSelectedValue();
txtFont.setText(current_fontName);
showTF.setFont(new Font(current_fontName, current_fontStyle, current_fontSize));
}
});
/*用户选择字型*/
lstStyle.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
String value = (String) ((JList) e.getSource()).getSelectedValue();
if (value.equals("常规")) {
current_fontStyle = Font.PLAIN;
}
if (value.equals("斜休")) {
current_fontStyle = Font.ITALIC;
}
if (value.equals("粗休")) {
current_fontStyle = Font.BOLD;
}
if (value.equals("粗斜休")) {
current_fontStyle = Font.BOLD | Font.ITALIC;
}
txtStyle.setText(value);
showTF.setFont(new Font(current_fontName, current_fontStyle, current_fontSize));
}
});
/*用户选择字体大小*/
lstSize.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
current_fontSize = (Integer) sizeMap.get(lstSize.getSelectedValue());
txtSize.setText(String.valueOf(current_fontSize));
showTF.setFont(new Font(current_fontName, current_fontStyle, current_fontSize));
}
});
/*用户选择字体颜色*/
cbColor.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
current_color = (Color) colorMap.get(cbColor.getSelectedItem());
showTF.setForeground(current_color);
}
});
/*其它颜色*/
otherColor.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Color col_temp = new JColorChooser().showDialog(null, null, Color.pink);
if (col_temp != null) {
current_color = col_temp;
showTF.setForeground(current_color);
super.mouseClicked(e);
}
}
});
/*用户确定*/
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
/*用户用户选择的字体设置*/
setSelectedfont(new Font(current_fontName, current_fontStyle, current_fontSize));
/*用户用户选择的颜色设置*/
setSelectedcolor(current_color);
dialog.dispose();
dialog = null;
}
});
/*用户取消*/
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog.dispose();
dialog = null;
}
});
}
/*显示字体选择器对话框(x,y表示窗体的启动位置)*/
public void showDialog(Frame parent,int x,int y) {
String title = "字体";
dialog = new JDialog(parent, title,true);
dialog.add(this);