问题描述
我的源码,关键调用的方法在最后几行,我在前面加了*号,就是font_mode()里面该如何编写,只要弹出那个JDialog即可,importjavax.swing.*;importjava.awt.event.*;importjava.awt.*;importjava.io.*;publicclassNoteBookextendsJFrame{publicJTextAreata;privateJMenuBarmb;privateJMenufile,edit,form,help;privateJMenuItemnewfile,open,save,exit;privateJMenuItemcut,copy,paste,select_all;privateJMenuItemfont,about;privateJScrollPanejsp;privateJFileChooserjfc;publicNoteBook(){super("记事本");Containerc=getContentPane();jfc=newJFileChooser();mb=newJMenuBar();file=newJMenu("文件");edit=newJMenu("编辑");form=newJMenu("格式");help=newJMenu("帮助");newfile=newJMenuItem("新建");open=newJMenuItem("打开");save=newJMenuItem("保存");exit=newJMenuItem("退出");cut=newJMenuItem("剪切");copy=newJMenuItem("复制");paste=newJMenuItem("粘贴");select_all=newJMenuItem("全选");font=newJMenuItem("字体...");about=newJMenuItem("关于...");newfile.addActionListener(newHandler1());open.addActionListener(newHandler1());save.addActionListener(newHandler1());exit.addActionListener(newHandler1());cut.addActionListener(newHandler1());copy.addActionListener(newHandler1());paste.addActionListener(newHandler1());select_all.addActionListener(newHandler1());font.addActionListener(newHandler1());about.addActionListener(newHandler1());mb.add(file);mb.add(edit);mb.add(form);mb.add(help);file.add(newfile);file.add(open);file.add(save);file.add(exit);edit.add(cut);edit.add(copy);edit.add(paste);edit.add(select_all);form.add(font);help.add(about);setJMenuBar(mb);ta=newJTextArea();//ta.setColumns(20);ta.setLineWrap(true);add(ta);jsp=newJScrollPane(ta);jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);add(jsp);setSize(800,600);setVisible(true);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});}//打开文件privatevoidopenfile(){JFileChooserfc=newJFileChooser();intreturnVal=fc.showOpenDialog(this);if(returnVal==JFileChooser.APPROVE_OPTION){Filefile=fc.getSelectedFile();try{ta.read(newFileReader(file),null);}catch(IOExceptionexp){}}}//保存文件privatevoidsavefile(){JFileChooserfc=newJFileChooser();intreturnVal=fc.showSaveDialog(this);if(returnVal==JFileChooser.APPROVE_OPTION){Filefile=fc.getSelectedFile();try{ta.write(newFileWriter(file));}catch(IOExceptionexp){}}}//新建privatevoidnew_file(){ta.setText("");}//剪切privatevoidcut_file(){ta.cut();}//复制privatevoidcopy_file(){ta.copy();}//粘贴privatevoidpaste_file(){ta.paste();}privatevoidselect_file(){ta.selectAll();}**privatevoidfont_mode(){}//主函数publicstaticvoidmain(Stringargs[]){NoteBooknb=newNoteBook();}classHandler1implementsActionListener{publicvoidactionPerformed(ActionEvente){if(e.getSource()==open)//打开openfile();if(e.getSource()==save)//保存savefile();if(e.getSource()==newfile)//新建new_file();if(e.getSource()==exit)//退出System.exit(0);if(e.getSource()==cut)cut_file();if(e.getSource()==copy)copy_file();if(e.getSource()==paste)paste_file();if(e.getSource()==select_all)select_file();**if(e.getSource()==font)font_mode();if(e.getSource()==about){JOptionPane.showMessageDialog(null,"A06计算机1班"+"n"+"江挺"+"n"+"060505107");}}}}
解决方案
解决方案二:
http://topic.csdn.net/u/20090523/00/2782ce71-9f8a-4450-a0db-0952db0a0aca.html看看这个可能有帮助
解决方案三:
classFontTestimplementsActionListener{//获取当前的环境变量privatejava.awt.GraphicsEnvironmentenv=java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment();privateJComboBoxcbFontName,cbFontStyle,cbFontHeight;privateJDialogjd;privateJButtonbtnGo;publicFontTest(){jd=newJDialog();String[]fontNames=env.getAvailableFontFamilyNames();cbFontName=newJComboBox(fontNames);String[]fontStyles=newString[]{"普通","粗体","斜体"};cbFontStyle=newJComboBox(fontStyles);String[]fontHeights=newString[10];for(inti=12;i<22;i++)fontHeights[i-12]=newString(Integer.toString(i+10));cbFontHeight=newJComboBox(fontHeights);btnGo=newJButton("OK");btnGo.setActionCommand("ApplyFont");JPanelpaneTop=newJPanel();paneTop.add(cbFontName);paneTop.add(cbFontStyle);paneTop.add(cbFontHeight);paneTop.add(btnGo);jd.add(paneTop);}publicvoidaddHandEvent(){btnGo.addActionListener(this);}publicvoidactionPerformed(ActionEventevent){if(event.getActionCommand().equals("ApplyFont")){StringfontName=cbFontName.getSelectedItem().toString();intfontStyle;switch(cbFontStyle.getSelectedIndex()){case0:fontStyle=Font.PLAIN;break;case1:fontStyle=Font.BOLD;break;case2:fontStyle=Font.ITALIC;break;default:fontStyle=Font.PLAIN;break;}//获取字体大小intfontSize=Integer.parseInt(cbFontHeight.getSelectedItem().toString());Fontfont=newFont(fontName,fontStyle,fontSize);area.setFont(font);}}publicvoidshow(){addHandEvent();jd.setTitle("Font");jd.pack();jd.setVisible(true);jd.setLocationRelativeTo(null);}}
LZ看看这是我字体改变的类,是放在记事本类里面作为它的内部类来调用的,可以改变字体大小,颜色,和字体等