现在我在用swing做一个记事本我的思路是这样的我想主类为记事本类,用记事本类去驱动记事本界面类记事本界面,打开,保存,字体编辑各写一个单独的类然后记事本界面在有字体编辑点击事件时才new一个字体编辑类,把界面类里的TextArea.getFont作为参数传递过去,然后在字体编辑类里修改这个对象,如果没有选择任何修改,就返回这个Font,并且隐藏这个字体编辑类对象。界面类那边判断字体编辑类对象是否隐藏了,如果隐藏了,就设置TextArea的字体为字体编辑类传递过来的Font对象,可是一直都是修改不了,而且现在还出现点了字体编辑菜单就没反应了,真是越改越悲剧希望高人可以指点一下代码里又没有红色波浪线,怎么上面就显示错误啊

解决方案 »

  1.   

     打开problems视图,看下是什么错!
      

  2.   

    吃了个饭回来,感谢各位帮助,图上的那个问题已经解决了,是因为我项目中还有一些别的类,具体是怎么搞出错误来我也不知道,但我把那些不必要的类删了就搞定了但是我上面描述的还是没解决,还是复制粘贴一下吧
    现在我在用swing做一个记事本我的思路是这样的我想主类为记事本类,用记事本类去驱动记事本界面类记事本界面,打开,保存,字体编辑各写一个单独的类然后记事本界面在有字体编辑点击事件时才new一个字体编辑类,把界面类里的TextArea.getFont作为参数传递过去,然后在字体编辑类里修改这个对象,如果没有选择任何修改,就返回这个Font,并且隐藏这个字体编辑类对象。界面类那边判断字体编辑类对象是否隐藏了,如果隐藏了,就设置TextArea的字体为字体编辑类传递过来的Font对象,可是一直都是修改不了,而且现在还出现点了字体编辑菜单就没反应了,真是越改越悲剧
      

  3.   


    求解决现在我在用swing做一个记事本我的思路是这样的我想主类为记事本类,用记事本类去驱动记事本界面类记事本界面,打开,保存,字体编辑各写一个单独的类然后记事本界面在有字体编辑点击事件时才new一个字体编辑类,把界面类里的TextArea.getFont作为参数传递过去,然后在字体编辑类里修改这个对象,如果没有选择任何修改,就返回这个Font,并且隐藏这个字体编辑类对象。界面类那边判断字体编辑类对象是否隐藏了,如果隐藏了,就设置TextArea的字体为字体编辑类传递过来的Font对象,可是一直都是修改不了在字体编辑类那个界面里是可以修改了,但是记事本那边的那个JTextArea的怎么都改不了
      

  4.   


    错误已经解决,现在是这个情况现在我在用swing做一个记事本我的思路是这样的我想主类为记事本类,用记事本类去驱动记事本界面类记事本界面,打开,保存,字体编辑各写一个单独的类然后记事本界面在有字体编辑点击事件时才new一个字体编辑类,把界面类里的TextArea.getFont作为参数传递过去,然后在字体编辑类里修改这个对象,如果没有选择任何修改,就返回这个Font,并且隐藏这个字体编辑类对象。界面类那边判断字体编辑类对象是否隐藏了,如果隐藏了,就设置TextArea的字体为字体编辑类传递过来的Font对象,可是一直都是修改不了在字体编辑类那个界面里是可以修改了,但是记事本那边的那个JTextArea的怎么都改不了
      

  5.   


    我就全部发出来吧
    主类package lyh.Notepad;public class LyhNotepad {
    public static void main(String args[]){
    new NotepadInterface();
    }
    }
    字体编辑类package lyh.Notepad;import java.awt.Font;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextField;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;class FontEditor extends JFrame implements ActionListener{
    private JLabel fontName = new JLabel("字体");
    private JLabel style = new JLabel("字形");
    private JLabel size = new JLabel("大小");
    private JLabel example = new JLabel("示例");
    private JTextField fontNameTextField = new JTextField("",8);
    private JTextField styleTextField = new JTextField("",4);
    private JTextField sizeTextField = new JTextField("",5);
    private JTextField exampleTextField = new JTextField("某某某",5);
    private String fontNameData[] = {"方正姚体","仿宋_GB2312","黑体","华文隶书","华文新魏","华文行楷","楷体_GB2312","宋体","微软雅黑","新宋体","幼圆"};
    private String styleData[] = {"常规","斜体","粗体","斜粗体"};
    private String sizeData[] = {"2","4","6","8","10","12","14","16","18","20","22","24","26","28","30"};
    private JList fontNameList = new JList(fontNameData);
    private JList styleList = new JList(styleData);
    private JList sizeList = new JList(sizeData);
    private JScrollPane fontnameScrollPane = new JScrollPane(fontNameList);
    private JScrollPane sizeScrollPane = new JScrollPane(sizeList);
    private JButton confirm = new JButton("确定");
    private JButton cancel = new JButton("取消");
    private JPanel panel1 = new JPanel();//字体相关的容器
    private JPanel panel2 = new JPanel();//字形相关的容器
    private JPanel panel3 = new JPanel();//字体大小相关容器
    private JPanel panel4 = new JPanel();//示例及按钮容器
    private GridBagConstraints gbc = new GridBagConstraints();
    private Font fontEditorFont;

    //调用此方法即可得到字体编辑器所修改的Font对象
    public Font returnFontFromFontEditor(){
    return fontEditorFont;
    }

    //ActionListener
    public void actionPerformed(ActionEvent e){
           if(e.getSource()==confirm){
            //返回所选择的字体、字形,大小
            returnFontFromFontEditor();
            setVisible(false);
           }
           if(e.getSource()==cancel){
            setVisible(false);
           }
    }

    FontEditor(Font f){
            super("字体设置"); 
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
            setLocationRelativeTo(null);
            this.fontEditorFont = f;//接收从Notepad对象传递过来的JTextArea对象的Font对象
            
            //设置这4个JTextField为不可编辑
            fontNameTextField.setEditable(false);
            styleTextField.setEditable(false);
            sizeTextField.setEditable(false);
            exampleTextField.setEditable(false);
            
            //字体相关组建的容器
            panel1.setLayout(new GridBagLayout());
            gbc.gridx=0;
            gbc.gridy=0;
            panel1.add(fontName,gbc);
            gbc.gridy=GridBagConstraints.RELATIVE;
            panel1.add(fontNameTextField,gbc);
            fontNameList.setVisibleRowCount(5); //设置可见行数
            fontNameList.setAutoscrolls(true);//设置自动滚动条
            panel1.add(fontnameScrollPane,gbc);
            
            //字形相关组建的容器
            panel2.setLayout(new GridBagLayout());
            gbc.gridx=0;
            gbc.gridy=0;
            gbc.anchor=GridBagConstraints.NORTH;
            panel2.add(style,gbc);
            gbc.gridy=GridBagConstraints.RELATIVE;
            panel2.add(styleTextField,gbc);
            panel2.add(styleList,gbc);
            
            //字体大小相关组建的容器
            panel3.setLayout(new GridBagLayout());
            gbc.gridx=0;
            gbc.gridy=0;
            gbc.anchor=GridBagConstraints.NORTH;
            panel3.add(size,gbc);
            gbc.gridy=GridBagConstraints.RELATIVE;
            panel3.add(sizeTextField,gbc);
            sizeList.setFixedCellWidth(35);//设置一个固定值,将用于列表中每个单元的宽度
            sizeList.setVisibleRowCount(5);
            sizeList.setAutoscrolls(true);
            panel3.add(sizeScrollPane,gbc);
            
            //示例及按钮容器
            panel4.setLayout(new GridBagLayout());
            gbc.gridx=0;
            gbc.gridy=0;
            panel4.add(example,gbc);
            gbc.gridy=GridBagConstraints.RELATIVE;
            gbc.insets=new Insets(0,0,10,0);
            panel4.add(exampleTextField,gbc);
            panel4.add(confirm,gbc);
            panel4.add(cancel,gbc);
            
            //窗口布局
            setLayout(new GridBagLayout());
            gbc.insets=new Insets(0,0,0,0);
            gbc.gridx=0;
            gbc.gridy=0;
            add(panel1,gbc);
            gbc.gridx=1;
            gbc.gridy=0;
            gbc.insets=new Insets(0,20,0,0);
            add(panel2,gbc);
            gbc.gridx=2;
            gbc.gridy=0;
            add(panel3,gbc);
            gbc.gridx=3;
            gbc.gridy=0;
            add(panel4,gbc);
            pack();
            
            //注册
            confirm.addActionListener(this);
            cancel.addActionListener(this);
            fontNameList.addListSelectionListener(new LyhJListListener());
            styleList.addListSelectionListener(new LyhJListListener());
            sizeList.addListSelectionListener(new LyhJListListener());
        }
    //私有内部类,监听3个JList
    private class LyhJListListener implements ListSelectionListener{
    public void valueChanged(ListSelectionEvent event) {
    //原字体,字形,大小
    String fontName = fontEditorFont.getFontName();
    int style = fontEditorFont.getStyle();
    int size = fontEditorFont.getSize();
    //如果字体改变了,更新字体
    if(!fontNameList.isSelectionEmpty()){
    fontName = (String)fontNameList.getSelectedValue();
    fontNameTextField.setText(fontName);
    exampleTextField.setFont(new Font(fontName,fontEditorFont.getStyle(),fontEditorFont.getSize()));
    }
    //如果字形改变了,更新字形
    if(!styleList.isSelectionEmpty()){
    String styleTemp=(String)styleList.getSelectedValue();
    if("常规"==styleTemp){
    style = Font.PLAIN;
    styleTextField.setText("常规");
    exampleTextField.setFont(new Font(fontEditorFont.getFontName(),style,fontEditorFont.getSize()));
    }
    if("斜体"==styleTemp){
    style = Font.ITALIC;
    styleTextField.setText("斜体");
    exampleTextField.setFont(new Font(fontEditorFont.getFontName(),style,fontEditorFont.getSize()));
    }
    if("粗体"==styleTemp){
    style = Font.BOLD;
    styleTextField.setText("粗体");
    exampleTextField.setFont(new Font(fontEditorFont.getFontName(),style,fontEditorFont.getSize()));
    }
    if("斜粗体"==styleTemp){
    style = Font.BOLD + Font.ITALIC;
    styleTextField.setText("斜粗体");
    exampleTextField.setFont(new Font(fontEditorFont.getFontName(),style,fontEditorFont.getSize()));
    }
    }
    //字体大小改变了,更新字体大小
    if(!sizeList.isSelectionEmpty()){
    size = Integer.parseInt((String)sizeList.getSelectedValue());
    sizeTextField.setText(Integer.toString(size));
    exampleTextField.setFont(new Font(fontEditorFont.getFontName(),style,size));
    }
    //没有改变的话按原样返回
    fontEditorFont = new Font(fontName,style,size);
    }
    }
    }
    对话框类package lyh.Notepad;import javax.swing.JOptionPane;class LyhDialog {
    //关于记事本对话框
    public void lyhAboutNotepad(){
    String softwareName = "SoftwareName : Lyh Notepad\n";
    String coder = "Coder : Lyh\n";
    String version = "Version : 1.0\n";
    String date = "Date : 2010.11.24";
    JOptionPane.showMessageDialog(null,softwareName + coder + version + date);
    }
    //退出对话框
    public void lyhExitDialog(){
    JOptionPane.showConfirmDialog(null,"是否退出");
    }

    //Just for fun对话框
    public void lyhJustForFun(){
    JOptionPane.showMessageDialog(null,"Just for fun!");
    }
    }
      

  6.   


    记事本界面类package lyh.Notepad;import java.awt.Event;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JTextArea;
    import javax.swing.KeyStroke;class NotepadInterface extends JFrame implements ActionListener{
    private JMenuBar menubar = new JMenuBar();
    private JMenu file = new JMenu("文件(F)");
    private JMenu edit = new JMenu("编辑");
    private JMenu form = new JMenu("格式");
    private JMenu view = new JMenu("查看");
    private JMenu help = new JMenu("帮助");
        
        //文件菜单子项
    private JMenuItem create = new JMenuItem("新建(N)");
    private JMenuItem open = new JMenuItem("打开(O)");
    private JMenuItem save = new JMenuItem("保存(S)");
    private JMenuItem otherSave = new JMenuItem("另存为(A)");
    private JMenuItem pageSet = new JMenuItem("页面设置(U)");
    private JMenuItem print = new JMenuItem("打印(P)");
    private JMenuItem exit = new JMenuItem("退出(X)");
        
        //编辑菜单子项
    private JMenuItem undo = new JMenuItem("撤销(U)");
    private JMenuItem cut = new JMenuItem("剪切(T)");
    private JMenuItem copy = new JMenuItem("复制(C)");
    private JMenuItem paste = new JMenuItem("粘贴(P)");
    private JMenuItem delete = new JMenuItem("删除(L)");
    private JMenuItem find = new JMenuItem("查找(F)");
    private JMenuItem findNext = new JMenuItem("查找下一个(N)");
    private JMenuItem replace = new JMenuItem("替换(R)");
    private JMenuItem goTo = new JMenuItem("转到");
    private JMenuItem checkAll = new JMenuItem("全选(A)");
    private JMenuItem date = new JMenuItem("时间/日期(D)");
        
    private JMenuItem font = new JMenuItem("字体");
            
    private JMenuItem helpTitle = new JMenuItem("帮助主题(H)");
    private JMenuItem aboutNotepad = new JMenuItem("关于记事本(A)");
            
    private JTextArea textArea = new JTextArea();
        
    //private ExitDialog exitDialog;
    //private AboutNotepad aboutNotepadDialog;
    private FontEditor fontEditor;
    private LyhDialog lyhDialog;
        
        //窗口监听
        public void actionPerformed(ActionEvent e){
         Object obj = e.getSource();
            //退出对话框
            if(obj==exit){
             lyhDialog = new LyhDialog();
             lyhDialog.lyhExitDialog();
            }
            //关于信息对话框
            if(obj==aboutNotepad){
             lyhDialog = new LyhDialog();
             lyhDialog.lyhAboutNotepad();
            }
            //字体编辑
            if(obj==font){
             fontEditor = new FontEditor(textArea.getFont());
             if(false==fontEditor.isVisible()){
             textArea.setFont(fontEditor.returnFontFromFontEditor());
             }
            }
            
            //Just for fun系列
            if(obj==pageSet || obj==print || obj==goTo || obj==helpTitle){
             lyhDialog = new LyhDialog();
             lyhDialog.lyhJustForFun();
            }
        }
        
        public JTextArea getTextArea(){
         return this.textArea;
        }
        
        //设置字体、字形、大小
        public void notepadSetFont(Font f){
         this.textArea.setFont(f);
        }
        
        NotepadInterface(){
            super("新建 文本文档.txt - 记事本");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(500,500);
            setLocationRelativeTo(null);
            
            setJMenuBar(menubar);
            menubar.add(file);
            menubar.add(edit);
            menubar.add(form);
            menubar.add(view);
            menubar.add(help);
            
            //文件菜单的菜单项
            file.add(create);
            file.add(open);
            file.add(save);
            file.add(otherSave);
            file.addSeparator();//添加间隔线
            file.add(pageSet);
            file.add(print);
            file.addSeparator();
            file.add(exit);
            
            //编辑菜单的菜单项
            edit.add(undo);
            edit.addSeparator();
            edit.add(cut);
            edit.add(copy);
            edit.add(paste);
            edit.add(delete);
            edit.addSeparator();
            edit.add(find);
            edit.add(findNext);
            edit.add(replace);
            edit.add(goTo);
            edit.addSeparator();
            edit.add(checkAll);
            edit.add(date);
            
            form.add(font);
            
            help.add(helpTitle);
            help.addSeparator();
            help.add(aboutNotepad);
            
            getContentPane().add(textArea);
            
            font.addActionListener(this);
            
            exit.addActionListener(this);
            aboutNotepad.addActionListener(this);
            
            
            pageSet.addActionListener(this);
            print.addActionListener(this);
            goTo.addActionListener(this);
            helpTitle.addActionListener(this);
            
            //为菜单及菜单子项添加快捷键
            file.setMnemonic('F');
            create.setMnemonic('N');
            open.setMnemonic('O');
            save.setMnemonic('S');
            print.setMnemonic('P');
            exit.setMnemonic('X');
            create.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,Event.CTRL_MASK, true));
            open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,Event.CTRL_MASK, true));
            save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,Event.CTRL_MASK, true));
            print.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,Event.CTRL_MASK, true));
            
            //为编辑及编辑子项添加快捷键
            edit.setMnemonic('E');
            undo.setMnemonic('U');
            cut.setMnemonic('T');
            copy.setMnemonic('C');
            paste.setMnemonic('P');
            delete.setMnemonic('L');
            find.setMnemonic('F');
            findNext.setMnemonic('N');
            replace.setMnemonic('R');
            goTo.setMnemonic('G');
            checkAll.setMnemonic('A');
            date.setMnemonic('D');
            undo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,Event.CTRL_MASK, true));
            cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,Event.CTRL_MASK, true));
            copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,Event.CTRL_MASK, true));
            paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,Event.CTRL_MASK, true));
            delete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0,true));//使用 0 指定无修饰符
            find.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,Event.CTRL_MASK, true));
            findNext.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3,0, true));
            replace.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,Event.CTRL_MASK, true));
            goTo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G,Event.CTRL_MASK, true));
            checkAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,Event.CTRL_MASK, true));
            date.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5,0, true));
            
            setVisible(true);
            }
        }
    /*
    //退出对话框
    class ExitDialog extends JDialog implements ActionListener{
        private Button confirm = new Button("确认");
        private Button cannel = new Button("退出");
        
        public void actionPerformed(ActionEvent e)
        {
           if(e.getSource()==confirm)
           {
               System.exit(0);
           }
           if(e.getSource()==cannel)
           {
               setVisible(false);
           }
        }
        
        ExitDialog(Frame f,String s,boolean b)
        {
            super(f,s,b); 
            addWindowListener(new WindowAdapter()
            {
               public void windowClosing(WindowEvent e)
               {
                   setVisible(false);
               }
            });
            setSize(150,150);
            setLocationRelativeTo(null);//设置居中
            setLayout(new FlowLayout());
            add(confirm);
            add(cannel);
            confirm.addActionListener(this);
            cannel.addActionListener(this);
            setVisible(true);
        }
    }
    */
      

  7.   


    //字体编辑
            if(obj==font){
             //你的
    //            fontEditor = new FontEditor(textArea.getFont());
             //我的
             fontEditor = new FontEditor(textArea.getFont(),textArea);
                if(false==fontEditor.isVisible()){
                    textArea.setFont(fontEditor.returnFontFromFontEditor());
                }
            }    //你的
    //    FontEditor(Font f){
        //我的
        FontEditor(Font f,JTextArea textArea){
     //我的,多加的,FontEditor.java类的成员变量赋值
            this.textArea = textArea;//多加的FontEditor.java类的成员变量
    private JTextArea textArea;//ActionListener
        public void actionPerformed(ActionEvent e){
               if(e.getSource()==confirm){
                   //返回所选择的字体、字形,大小
                   returnFontFromFontEditor();
                   //我的设置字体
                   textArea.setFont(returnFontFromFontEditor());
                   setVisible(false);
               }
               if(e.getSource()==cancel){
                   setVisible(false);
               }
        }
    照我该的添加下,我没改你的代码,我只是添加的一点东西。
    改了之后你就知道为什么了你的字体没有变。
      

  8.   

    楼主啊,关键问题就在你的“字体”那个按钮的事件监听上
    你打开字体更改那个Frame之后,后面代码是立刻执行的
    而不是等你的Frame点了确定之后再执行的
    所以肯定改不了字体罗import java.awt.Event;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JTextArea;
    import javax.swing.KeyStroke;class NotepadInterface extends JFrame implements ActionListener{
        private JMenuBar menubar = new JMenuBar();
        private JMenu file = new JMenu("文件(F)");
        private JMenu edit = new JMenu("编辑");
        private JMenu form = new JMenu("格式");
        private JMenu view = new JMenu("查看");
        private JMenu help = new JMenu("帮助");
        
        //文件菜单子项
        private JMenuItem create = new JMenuItem("新建(N)");
        private JMenuItem open = new JMenuItem("打开(O)");
        private JMenuItem save = new JMenuItem("保存(S)");
        private JMenuItem otherSave = new JMenuItem("另存为(A)");
        private JMenuItem pageSet = new JMenuItem("页面设置(U)");
        private JMenuItem print = new JMenuItem("打印(P)");
        private JMenuItem exit = new JMenuItem("退出(X)");
        
        //编辑菜单子项
        private JMenuItem undo = new JMenuItem("撤销(U)");
        private JMenuItem cut = new JMenuItem("剪切(T)");
        private JMenuItem copy = new JMenuItem("复制(C)");
        private JMenuItem paste = new JMenuItem("粘贴(P)");
        private JMenuItem delete = new JMenuItem("删除(L)");
        private JMenuItem find = new JMenuItem("查找(F)");
        private JMenuItem findNext = new JMenuItem("查找下一个(N)");
        private JMenuItem replace = new JMenuItem("替换(R)");
        private JMenuItem goTo = new JMenuItem("转到");
        private JMenuItem checkAll = new JMenuItem("全选(A)");
        private JMenuItem date = new JMenuItem("时间/日期(D)");
        
        private JMenuItem font = new JMenuItem("字体");
            
        private JMenuItem helpTitle = new JMenuItem("帮助主题(H)");
        private JMenuItem aboutNotepad = new JMenuItem("关于记事本(A)");
            
        private JTextArea textArea = new JTextArea();
        
        //private ExitDialog exitDialog;
        //private AboutNotepad aboutNotepadDialog;
        private FontEditor fontEditor;
        private LyhDialog lyhDialog;
        
        //窗口监听
        public void actionPerformed(ActionEvent e){
            Object obj = e.getSource();
            //退出对话框
            if(obj==exit){
                lyhDialog = new LyhDialog();
                lyhDialog.lyhExitDialog();
            }
            //关于信息对话框
            if(obj==aboutNotepad){
                lyhDialog = new LyhDialog();
                lyhDialog.lyhAboutNotepad();
            }
            //字体编辑
            if(obj==font){
                fontEditor = new FontEditor(textArea);
            }
            
            //Just for fun系列
            if(obj==pageSet || obj==print || obj==goTo || obj==helpTitle){
                lyhDialog = new LyhDialog();
                lyhDialog.lyhJustForFun();
            }
        }
        
        public JTextArea getTextArea(){
            return this.textArea;
        }
        
        //设置字体、字形、大小
        public void notepadSetFont(Font f){
            this.textArea.setFont(f);
        }
        
        NotepadInterface(){
            super("新建 文本文档.txt - 记事本");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(500,500);
            setLocationRelativeTo(null);
            
            setJMenuBar(menubar);
            menubar.add(file);
            menubar.add(edit);
            menubar.add(form);
            menubar.add(view);
            menubar.add(help);
            
            //文件菜单的菜单项
            file.add(create);
            file.add(open);
            file.add(save);
            file.add(otherSave);
            file.addSeparator();//添加间隔线
            file.add(pageSet);
            file.add(print);
            file.addSeparator();
            file.add(exit);
            
            //编辑菜单的菜单项
            edit.add(undo);
            edit.addSeparator();
            edit.add(cut);
            edit.add(copy);
            edit.add(paste);
            edit.add(delete);
            edit.addSeparator();
            edit.add(find);
            edit.add(findNext);
            edit.add(replace);
            edit.add(goTo);
            edit.addSeparator();
            edit.add(checkAll);
            edit.add(date);
            
            form.add(font);
            
            help.add(helpTitle);
            help.addSeparator();
            help.add(aboutNotepad);
            
            getContentPane().add(textArea);
            
            font.addActionListener(this);
            
            exit.addActionListener(this);
            aboutNotepad.addActionListener(this);
            
            
            pageSet.addActionListener(this);
            print.addActionListener(this);
            goTo.addActionListener(this);
            helpTitle.addActionListener(this);
            
            //为菜单及菜单子项添加快捷键
            file.setMnemonic('F');
            create.setMnemonic('N');
            open.setMnemonic('O');
            save.setMnemonic('S');
            print.setMnemonic('P');
            exit.setMnemonic('X');
            create.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,Event.CTRL_MASK, true));
            open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,Event.CTRL_MASK, true));
            save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,Event.CTRL_MASK, true));
            print.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,Event.CTRL_MASK, true));
            
            //为编辑及编辑子项添加快捷键
            edit.setMnemonic('E');
            undo.setMnemonic('U');
            cut.setMnemonic('T');
            copy.setMnemonic('C');
            paste.setMnemonic('P');
            delete.setMnemonic('L');
            find.setMnemonic('F');
            findNext.setMnemonic('N');
            replace.setMnemonic('R');
            goTo.setMnemonic('G');
            checkAll.setMnemonic('A');
            date.setMnemonic('D');
            undo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,Event.CTRL_MASK, true));
            cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,Event.CTRL_MASK, true));
            copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,Event.CTRL_MASK, true));
            paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,Event.CTRL_MASK, true));
            delete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0,true));//使用 0 指定无修饰符
            find.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,Event.CTRL_MASK, true));
            findNext.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3,0, true));
            replace.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,Event.CTRL_MASK, true));
            goTo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G,Event.CTRL_MASK, true));
            checkAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,Event.CTRL_MASK, true));
            date.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5,0, true));
            
            setVisible(true);
            }
        }这样改
      

  9.   

    另一个类import java.awt.Font;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;class FontEditor extends JFrame implements ActionListener{
        private JLabel fontName = new JLabel("字体");
        private JLabel style = new JLabel("字形");
        private JLabel size = new JLabel("大小");
        private JLabel example = new JLabel("示例");
        private JTextField fontNameTextField = new JTextField("",8);
        private JTextField styleTextField = new JTextField("",4);
        private JTextField sizeTextField = new JTextField("",5);
        private JTextField exampleTextField = new JTextField("某某某",5);
        private String fontNameData[] = {"方正姚体","仿宋_GB2312","黑体","华文隶书","华文新魏","华文行楷","楷体_GB2312","宋体","微软雅黑","新宋体","幼圆"};
        private String styleData[] = {"常规","斜体","粗体","斜粗体"};
        private String sizeData[] = {"2","4","6","8","10","12","14","16","18","20","22","24","26","28","30"};
        private JList fontNameList = new JList(fontNameData);
        private JList styleList = new JList(styleData);
        private JList sizeList = new JList(sizeData);
        private JScrollPane fontnameScrollPane = new JScrollPane(fontNameList);
        private JScrollPane sizeScrollPane = new JScrollPane(sizeList);
        private JButton confirm = new JButton("确定");
        private JButton cancel = new JButton("取消");
        private JPanel panel1 = new JPanel();//字体相关的容器
        private JPanel panel2 = new JPanel();//字形相关的容器
        private JPanel panel3 = new JPanel();//字体大小相关容器
        private JPanel panel4 = new JPanel();//示例及按钮容器
        private GridBagConstraints gbc = new GridBagConstraints();
        private Font fontEditorFont;
        private JTextArea textArea;
        
        //调用此方法即可得到字体编辑器所修改的Font对象
        public Font returnFontFromFontEditor(){
            return fontEditorFont;
        }
        
        //ActionListener
        public void actionPerformed(ActionEvent e){
               if(e.getSource()==confirm){
                   //返回所选择的字体、字形,大小
                textArea.setFont(fontEditorFont);
                   setVisible(false);
               }
               if(e.getSource()==cancel){
                   setVisible(false);
               }
        }
        
        FontEditor(JTextArea area){
            super("字体设置"); 
            textArea = area;
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
            setLocationRelativeTo(null);
            this.fontEditorFont = area.getFont();//接收从Notepad对象传递过来的JTextArea对象的Font对象
            
            //设置这4个JTextField为不可编辑
            fontNameTextField.setEditable(false);
            styleTextField.setEditable(false);
            sizeTextField.setEditable(false);
            exampleTextField.setEditable(false);
            
            //字体相关组建的容器
            panel1.setLayout(new GridBagLayout());
            gbc.gridx=0;
            gbc.gridy=0;
            panel1.add(fontName,gbc);
            gbc.gridy=GridBagConstraints.RELATIVE;
            panel1.add(fontNameTextField,gbc);
            fontNameList.setVisibleRowCount(5); //设置可见行数
            fontNameList.setAutoscrolls(true);//设置自动滚动条
            panel1.add(fontnameScrollPane,gbc);
            
            //字形相关组建的容器
            panel2.setLayout(new GridBagLayout());
            gbc.gridx=0;
            gbc.gridy=0;
            gbc.anchor=GridBagConstraints.NORTH;
            panel2.add(style,gbc);
            gbc.gridy=GridBagConstraints.RELATIVE;
            panel2.add(styleTextField,gbc);
            panel2.add(styleList,gbc);
            
            //字体大小相关组建的容器
            panel3.setLayout(new GridBagLayout());
            gbc.gridx=0;
            gbc.gridy=0;
            gbc.anchor=GridBagConstraints.NORTH;
            panel3.add(size,gbc);
            gbc.gridy=GridBagConstraints.RELATIVE;
            panel3.add(sizeTextField,gbc);
            sizeList.setFixedCellWidth(35);//设置一个固定值,将用于列表中每个单元的宽度
            sizeList.setVisibleRowCount(5);
            sizeList.setAutoscrolls(true);
            panel3.add(sizeScrollPane,gbc);
            
            //示例及按钮容器
            panel4.setLayout(new GridBagLayout());
            gbc.gridx=0;
            gbc.gridy=0;
            panel4.add(example,gbc);
            gbc.gridy=GridBagConstraints.RELATIVE;
            gbc.insets=new Insets(0,0,10,0);
            panel4.add(exampleTextField,gbc);
            panel4.add(confirm,gbc);
            panel4.add(cancel,gbc);
            
            //窗口布局
            setLayout(new GridBagLayout());
            gbc.insets=new Insets(0,0,0,0);
            gbc.gridx=0;
            gbc.gridy=0;
            add(panel1,gbc);
            gbc.gridx=1;
            gbc.gridy=0;
            gbc.insets=new Insets(0,20,0,0);
            add(panel2,gbc);
            gbc.gridx=2;
            gbc.gridy=0;
            add(panel3,gbc);
            gbc.gridx=3;
            gbc.gridy=0;
            add(panel4,gbc);
            pack();
            
            //注册
            confirm.addActionListener(this);
            cancel.addActionListener(this);
            fontNameList.addListSelectionListener(new LyhJListListener());
            styleList.addListSelectionListener(new LyhJListListener());
            sizeList.addListSelectionListener(new LyhJListListener());
        }
        //私有内部类,监听3个JList
        private class LyhJListListener implements ListSelectionListener{
            public void valueChanged(ListSelectionEvent event) {
                //原字体,字形,大小
                String fontName = fontEditorFont.getFontName();
                int style = fontEditorFont.getStyle();
                int size = fontEditorFont.getSize();
                //如果字体改变了,更新字体
                if(!fontNameList.isSelectionEmpty()){
                    fontName = (String)fontNameList.getSelectedValue();
                    fontNameTextField.setText(fontName);
                    exampleTextField.setFont(new Font(fontName,fontEditorFont.getStyle(),fontEditorFont.getSize()));
                }
                //如果字形改变了,更新字形
                if(!styleList.isSelectionEmpty()){
                    String styleTemp=(String)styleList.getSelectedValue();
                    if("常规"==styleTemp){
                        style = Font.PLAIN;
                        styleTextField.setText("常规");
                        exampleTextField.setFont(new Font(fontEditorFont.getFontName(),style,fontEditorFont.getSize()));
                    }
                    if("斜体"==styleTemp){
                        style = Font.ITALIC;
                        styleTextField.setText("斜体");
                        exampleTextField.setFont(new Font(fontEditorFont.getFontName(),style,fontEditorFont.getSize()));
                    }
                    if("粗体"==styleTemp){
                        style = Font.BOLD;
                        styleTextField.setText("粗体");
                        exampleTextField.setFont(new Font(fontEditorFont.getFontName(),style,fontEditorFont.getSize()));
                    }
                    if("斜粗体"==styleTemp){
                        style = Font.BOLD + Font.ITALIC;
                        styleTextField.setText("斜粗体");
                        exampleTextField.setFont(new Font(fontEditorFont.getFontName(),style,fontEditorFont.getSize()));
                    }
                }
                //字体大小改变了,更新字体大小
                if(!sizeList.isSelectionEmpty()){
                    size = Integer.parseInt((String)sizeList.getSelectedValue());
                    sizeTextField.setText(Integer.toString(size));
                    exampleTextField.setFont(new Font(fontEditorFont.getFontName(),style,size));
                }
                //没有改变的话按原样返回
                fontEditorFont = new Font(fontName,style,size);
            }    
        }
    }
      

  10.   


    多谢大神了,允许我再问两句吗java里面的不全部都是按值传递吗?就是复制
    有这种像C++一样的按引用传递
    天啊,我的是什么教材
      

  11.   


    是啊,我有想到那个问题的,就是加了if判断上去也是不行
    我现在就很奇怪java也可以按引用传递
      

  12.   

    建议不要太多的new,影响性能,内存满了就杯具了
      

  13.   


    像我这个小程序现在只用了20多M的内存,所以不会有太大影响的,另外你说的new我也有考虑到,就是在没有点击启动字体编辑类的情况下是不会new的,这样就实现了动态的加载
    在昨晚弄清楚这个问题以后,我会在结束字体编辑类的时候将它赋值为null,并马上用System.gc()进行回收,尽可能模拟商用程序的设计,下一步会加入接口,模拟多人共同开发感觉这次做的这个才真的像是一个程序,原来想不明白的搞什么接口之类的东西有什么用,现在就豁然开朗了
      

  14.   


    还在吗,我修改一点东西,能帮我看看吗http://topic.csdn.net/u/20101128/12/c55d09a8-4f6c-4c77-b597-02956fa0b05c.html?seed=1970629434&r=70198921#r_70198921