JFontChooser java 自定义的字体选择器(三)
消");
//布局控件
//字体框
this.setLayout(null); //不用布局管理器
add(lblFont);
lblFont.setBounds(12, 10, 30, 20);
txtFont.setEditable(false);
add(txtFont);
txtFont.setBounds(10, 30, 155, 20);
txtFont.setText("宋体");
lstFont.setSelectedValue("宋体", true);
if (font != null) {
txtFont.setText(font.getName());
lstFont.setSelectedValue(font.getName(), true);
}
add(spFont);
spFont.setBounds(10, 50, 155, 100);
//样式
add(lblStyle);
lblStyle.setBounds(175, 10, 30, 20);
txtStyle.setEditable(false);
add(txtStyle);
txtStyle.setBounds(175, 30, 130, 20);
lstStyle.setBorder(javax.swing.BorderFactory.createLineBorder(Color.gray));
add(lstStyle);
lstStyle.setBounds(175, 50, 130, 100);
txtStyle.setText("常规"); //初始化为默认的样式
lstStyle.setSelectedValue("常规",true); //初始化为默认的样式
if(font != null){
lstStyle.setSelectedIndex(font.getStyle()); //初始化样式list
if (font.getStyle() == 0) {
txtStyle.setText("常规");
} else if (font.getStyle() == 1) {
txtStyle.setText("粗体");
} else if (font.getStyle() == 2) {
txtStyle.setText("斜体");
} else if (font.getStyle() == 3) {
txtStyle.setText("粗斜体");
}
}
//大小
add(lblSize);
lblSize.setBounds(320, 10, 30, 20);
txtSize.setEditable(false);
add(txtSize);
txtSize.setBounds(320, 30, 60, 20);
add(spSize);
spSize.setBounds(320, 50, 60, 100);
lstSize.setSelectedValue("9", false);
txtSize.setText("9");
if (font != null) {
lstSize.setSelectedValue(Integer.toString(font.getSize()), false);
txtSize.setText(Integer.toString(font.getSize()));
}
//颜色
add(lblColor);
lblColor.setBounds(18, 220, 30, 20);
cbColor.setBounds(18, 245, 100, 22);
cbColor.setMaximumRowCount(5);
add(cbColor);
otherColor.setForeground(Color.blue);
otherColor.setBounds(130, 245, 60, 22);
otherColor.setCursor(new Cursor(Cursor.HAND_CURSOR));
add(otherColor);
//展示框
showTF = new JTextField();
showTF.setFont(new Font(current_fontName, current_fontStyle, current_fontSize));
showTF.setBounds(10, 10, 300, 50);
showTF.setHorizontalAlignment(JTextField.CENTER);
showTF.setText(showStr);
showTF.setBackground(Color.white);
showTF.setEditable(false);
showPan.setBorder(javax.swing.BorderFactory.createTitledBorder("示例"));
add(showPan);
showPan.setBounds(13, 150,370, 80);
showPan.setLayout(new BorderLayout());
showPan.add(showTF);
if (font != null) {
showTF.setFont(font); // 设置示例中的文字格式
}
if (font != null) {
showTF.setForeground(color);
}
//确定和取消按钮
add(ok);
ok.setBounds(230, 245, 60, 20);
add(cancel);
cancel.setBounds(300, 245, 60, 20)