这是设置字体的代码。窗口的设计代码我省略了,这些是事件处理
//字体窗口中JTextFiled获得选中的字符
classxianshi1implementsActionListener{
publicvoidactionPerformed(ActionEvente){
fonttext.setText((String)((JComboBox)e.getSource())
.getSelectedItem());
}
}
classxianshi2implementsActionListener{
publicvoidactionPerformed(ActionEvente){
styletext.setText((String)((JComboBox)e.getSource())
.getSelectedItem());
}
}
classxianshi3implementsActionListener{
publicvoidactionPerformed(ActionEvente){
sizetext.setText((String)((JComboBox)e.getSource())
.getSelectedItem());
}
}
classquedingimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==ok){
flg3=1;
ss();
fontdialog.dispose();
}
if(e.getSource()==cancel){
fontdialog.dispose();
}
}
}
//字体事件方法
publicvoidss(){
Stringziti,zixing,zihao;
//为字形定义一个变量
intstyle=Font.PLAIN;
//得到JComboBox中字符
ziti=(String)combo[0].getSelectedItem();
zixing=(String)combo[1].getSelectedItem();
zihao=(String)combo[2].getSelectedItem();
if(zixing.equals("斜体")){
style=Font.ITALIC;
}
if(zixing.equals("粗体")){
style=Font.BOLD;
}
if(zixing.equals("粗斜体")){
style=Font.ITALIC;
style=Font.BOLD;
}
sample.setFont(newFont(ziti,style,Integer.parseInt(zihao)));//应用修改后的字体
if(flg3==1){
text.setFont(newFont(ziti,style,Integer.parseInt(zihao)));
flg3=0;
}
}
}