我的源码,关键调用的方法在最后几行,我在前面加了*号,就是font_mode()里面该如何编写,只要弹出那个JDialog即可,
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;public class NoteBook extends JFrame{
public JTextArea ta;
private JMenuBar mb;
private JMenu file,edit,form,help;
private JMenuItem newfile,open,save,exit;
private JMenuItem cut,copy,paste,select_all;
private JMenuItem font,about;
private JScrollPane jsp;
private JFileChooser jfc;

 
public NoteBook(){
super("记事本");
Container c = getContentPane();

jfc = new JFileChooser();
mb = new JMenuBar();

file = new JMenu("文件");
edit = new JMenu("编辑");
form = new JMenu("格式");
help = new JMenu("帮助");

newfile = new JMenuItem("新建");
open = new JMenuItem("打开");
save = new JMenuItem("保存");
exit = new JMenuItem("退出");

cut = new JMenuItem("剪切");
copy = new JMenuItem("复制");
paste = new JMenuItem("粘贴");
select_all = new JMenuItem("全选");

font = new JMenuItem("字体...");
about = new JMenuItem("关于...");

newfile.addActionListener(new Handler1());
open.addActionListener(new Handler1());
save.addActionListener(new Handler1());
exit.addActionListener(new Handler1());

cut.addActionListener(new Handler1());
copy.addActionListener(new Handler1());
paste.addActionListener(new Handler1());
select_all.addActionListener(new Handler1());

font.addActionListener(new Handler1());
about.addActionListener(new Handler1());

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 = new JTextArea();
//ta.setColumns(20);
ta.setLineWrap(true); 
add(ta);
jsp = new JScrollPane(ta);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
add(jsp); 
setSize(800,600);
setVisible(true);

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}  //打开文件
private void openfile(){
JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION){
File file = fc.getSelectedFile();
  try{
   ta.read(new FileReader(file),null);
   }
  catch(IOException exp)   {}
}
}

//保存文件
private void savefile(){
JFileChooser fc = new JFileChooser();
int returnVal = fc.showSaveDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION){
File file = fc.getSelectedFile();
  try{
   ta.write(new FileWriter(file));
   }
  catch(IOException exp)   {}
}
}

//新建
private void new_file(){
ta.setText("");
}

//剪切
private void cut_file(){
ta.cut();
}

//复制
private void copy_file(){
ta.copy();
}
//粘贴 
private void paste_file(){
ta.paste();
}

private void select_file(){
ta.selectAll();
}

** private void font_mode(){

}

//主函数
public static void main(String args[]){
NoteBook nb = new NoteBook();
}

class Handler1 implements ActionListener{
public void actionPerformed(ActionEvent e){

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");
}

}
}
}

解决方案 »

  1.   

    http://topic.csdn.net/u/20090523/00/2782ce71-9f8a-4450-a0db-0952db0a0aca.html看看 这个  可能有帮助
      

  2.   

    class FontTest implements ActionListener{
    // 获取当前的环境变量
    private java.awt.GraphicsEnvironment env = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment();
    private JComboBox cbFontName, cbFontStyle, cbFontHeight;
    private JDialog jd;
    private JButton btnGo;

    public FontTest() {
      jd=new JDialog();
      String[] fontNames = env.getAvailableFontFamilyNames();
      cbFontName = new JComboBox(fontNames);
      String[] fontStyles =new String[]{ "普通", "粗体", "斜体" };
      cbFontStyle = new JComboBox(fontStyles);
      String[] fontHeights = new String[10];
      for (int i = 12; i < 22; i++)
        fontHeights[i - 12] = new String(Integer.toString(i+10));
      cbFontHeight = new JComboBox(fontHeights);
      btnGo = new JButton("OK");
      btnGo.setActionCommand("Apply Font");
      
      JPanel paneTop = new JPanel();
      paneTop.add(cbFontName);
      paneTop.add(cbFontStyle);
      paneTop.add(cbFontHeight);
      paneTop.add(btnGo);
      jd.add(paneTop);
    }
    public void addHandEvent(){
    btnGo.addActionListener(this);
    }
    public void actionPerformed(ActionEvent event) {
      if (event.getActionCommand().equals("Apply Font")) { 
    String fontName = cbFontName.getSelectedItem().toString();
    int fontStyle;
        switch (cbFontStyle.getSelectedIndex()) {
        case 0:
          fontStyle = Font.PLAIN;
          break;
        case 1:
          fontStyle = Font.BOLD;
          break;
        case 2:
          fontStyle = Font.ITALIC;
          break;
        default:
          fontStyle = Font.PLAIN;
          break;
        }
        // 获取字体大小
        int fontSize = Integer.parseInt(cbFontHeight.getSelectedItem().toString());
        Font font = new Font(fontName, fontStyle, fontSize);
        area.setFont(font);
      }
    } public  void show() {
      addHandEvent();
      jd.setTitle("Font");
      jd.pack();
      jd.setVisible(true);
      jd.setLocationRelativeTo(null);
    }
    }LZ看看这是我字体改变的类,是放在记事本类里面作为它的内部类来调用的,可以改变字体大小,颜色,和字体等