java字体选择对话框

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

字符集为中文是的情况英文状态下的选择字符集为数字importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclassfontdialogimplementsActionListener,ItemListener{Listfontname,fontshape,fontsize;JPanelpname,pshape,psize,pmodel,prb;JTextFieldtxtname,txtshape,txtsize;JTextAreatxtmodel;JDialogdialog;JButtonbtnok,btncancel;JRadioButtonrbzw,rbyw,rbsz;publicFontmodelfont;publicfontdialog(JFramef,intx,inty){GraphicsEnvironmentg=GraphicsEnvironment.getLocalGraphicsEnvironment();Stringname[]=g.getAvailableFontFamilyNames();Stringshape[]={常规,斜体,粗体倾斜,粗体};fontname=newList();fontshape=newList();fontsize=newList();rbzw=newJRadioButton(中文);rbyw=newJRadioButton(英文);rbsz=newJRadioButton(数字);ButtonGroupgroup=newButtonGroup();prb=newJPanel();group.add(rbzw);group.add(rbyw);group.add(rbsz);txtmodel=newJTextArea();txtmodel.setText(中华人民共和国);rbzw.setSelected(true);btnok=newJButton(确定);btncancel=newJButton(取消);//添加监听器fontname.addItemListener(this);fontshape.addItemListener(this);fontsize.addItemListener(this);btnok.addActionListener(this);btncancel.addActionListener(this);rbzw.addItemListener(this);rbyw.addItemListener(this);rbsz.addItemListener(this);for(inti=0;iname.length;i++)fontname.addItem(name[i]);for(inti=0;i200;i++)fontsize.addItem(Integer.toString(i+10));for(inti=0;ishape.length;i++)fontshape.addItem(shape[i]);fontname.select(175);fontshape.select(1);fontsize.select(22);pname=newJPanel();pshape=newJPanel();psize=newJPanel();pmodel=newJPanel();txtname=newJTextField(12);txtshape=newJTextField(12);txtsize=newJTextField(20);txtname.setText(fontname.getSelectedItem());txtshape.setText(fontshape.getSelectedItem());txtsize.setText(fontsize.getSelectedItem());dialog=newJDialog(f,字体,true);dialog.setLayout(null);pname.setBorder(BorderFactory.createTitledBorder(字体));pshape.setBorder(BorderFactory.createTitledBorder(字形));psize.setBorder(BorderFactory.createTitledBorder(大小));pmodel.setBorder(BorderFactory.createTitledBorder(示例));prb.setBorder(BorderFactory.createTitledBorder(字符集));pname.setBounds(10,10,190,170);pshape.setBounds(210,10,160,170);psize.setBounds(380,10,80,170);pmodel.setBounds(150,200,300,120);prb.setBounds(10,200,100,120);rbzw.setBounds(25,220,60,25);rbyw.setBounds(25,250,60,25);rbsz.setBounds(25,280,60,25);txtname.setBounds(30,30,160,22);txtshape.setBounds(220,30,140,22);txtsize.setBounds(390,30,60,22);fontname.setBounds(30,65,160,100);fontshape.setBounds(220,65,140,100);fontsize.setBounds(390,65,60,100);txtmodel.setBounds(175,220,250,85);btnok.setBounds(140,340,65,30);btncancel.setBounds(250,340,65,30);//pshape.setBounds();dialog.add(txtname);dialog.add(txtshape);dialog.add(fontname);dialog.add(fontshape);dialog.add(fontsize);dialog.add(txtsize);dialog.add(pname);dialog.add(pshape);dialog.add(psize);dialog.add(txtmodel);dialog.add(pmodel);dialog.add(rbzw);dialog.add(rbyw);dialog.add(rbsz);dialog.add(prb);dialog.add(btnok);dialog.add(btncancel);dialog.setSize(480,440);dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);dialog.show();}publicvoidactionPerformed(ActionEvente){}publicvoiditemStateChanged(ItemEvente){try{if(e.getSource()==rbzw)txtmodel.setText(中华人民共和国);if(e.getSource()==rbyw)txtmodel.setText(ABCDEFabcdef);if(e.getSource()==rbsz)txtmodel.setText(0123456789);if(fontshape.getSelectedItem()==粗体){modelfont=newFont(fontname.getSelectedItem(),Font.BOLD,Integer.parseInt(fontsize.getSelectedItem()));txtmodel.setFont(modelfont);}if(fontshape.getSelectedItem()==斜体){modelfont=newFont(fontname.getSelectedItem(),Font.ITALIC,Integer.parseInt(fontsize.getSelectedItem()));txtmodel.setFont(modelfont);}if(fontshape.getSelectedItem()==粗体倾斜){modelfont=newFont(fontname.getSelectedItem(),Font.BOLD|Font.ITALIC,Integer.parseInt(fontsize.getSelectedItem()));txtmodel.setFont(modelfont);}if(fontshape.getSelectedItem()==常规){modelfont=newFont(fontname.getSelectedItem(),Font.PLAIN,Integer.parseInt(fontsize.getSelectedItem()));txtmodel.setFont(modelfont);}txtname.setText(fontname.getSelectedItem());txtshape.setText(fontshape.getSelectedItem());txtsize.setText(fontsize.getSelectedItem());}catch(Exceptionex){}}//主函数测试publicstaticvoidmain(Stringargs[]){JFramef=newJFrame();f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);fontdialogfd=newfontdialog(f,20,20);}}

1 / 6
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功