我也想知道,帮你 up,另外,楼主似乎在做一个notepad,我也是啊,我们交个朋友吧,我的qq是6578023,一起研究研究?

解决方案 »

  1.   

    1. Mynotebook.java-----> NOTEBOOK.java, case sensitive
    2. FontDlg------------->FontDialog
    3. add:
        // in NOTEBOOK
        font.show();
        result = font.getResult();    // in FontDialog
         public String getResult() {
           // if(isOK)
              return ...;
           // else
              return null;
         }
      

  2.   

    首先不知道你的这两个类是不是写在一个文件里了,一个文件里只能有一个public class第二是你的FontDialog.java应该先编译好
      

  3.   

    编译的时候:javac *.java
    还有,你最后把程序考全的,我可以一下运行找到问题,这样我的还臆测.
      

  4.   

    Object o=Class.forName("FontDialog");
      

  5.   

    感谢你的关注.我是这样写的:
    主程序:
    public class Mynotebook extends JFrame
    {
    //初始化文本区
     public static JTextArea textArea1 = new JTextArea();

    public Mynotebook (String strTitle)
    {
       JMenuItem fontmenuitem=new JMenuItem("字体");    
                //引入字体设置事件
              fontmenuitem.addActionListener(new 
                  java.awt.event.ActionListener() {
                    public void actionPerformed(ActionEvent e){
                    fontmenuitem_action(e);
     }
                 });
         void fontmenuitem_action(ActionEvent e){
           FontDlg  fontdialog=new  FontDlg(this);
             fontdialog.show(); 
             }
    public static void main( String[] args )
    {...}
    }
    FontDlg.java程序:
    public class FontDlg extends JDialog {
        String str;
        JTextField txt;
    //确定按钮事件
    okBtn.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
      putFont(str, txt);
      }
      });public void putFont(String str, JTextField txt){
    txt.setText(str);
                   ....... Mynotebook.textArea1.setFont(new Font(name, style, size));
    }
    }运行后字体能设置了,就是在按下FontDlg.java程序中的确定按钮后同时显示如下信息:
      

  6.   

    at java.awt.Component.processMouseEvent(Component.java:5093)
            at java.awt.Component.processEvent(Component.java:4890)
            at java.awt.Container.processEvent(Container.java:1566)
            at java.awt.Component.dispatchEventImpl(Component.java:3598)
            at java.awt.Container.dispatchEventImpl(Container.java:1623)
            at java.awt.Component.dispatchEvent(Component.java:3439)
            at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3450
    )
            at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3165)        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3095)
            at java.awt.Container.dispatchEventImpl(Container.java:1609)
            at java.awt.Window.dispatchEventImpl(Window.java:1585)
            at java.awt.Component.dispatchEvent(Component.java:3439)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:450)
            at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
    read.java:197)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
    ad.java:150)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)        at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)
    Press any key to continue...
      

  7.   

    把你的FontDialog的构造函数贴出来吧。
      

  8.   

    你的错误信息不全:最上面一行是什么?
    at java.awt.Component.processMouseEvent(Component.java:5093)
            at java.awt.Component.processEvent(Component.java:4890)
            at java.awt.Container.processEvent(Container.java:1566)
            at java.awt.Component.dispatchEventImpl(Component.java:3598)
    Press any key to continue...下面的哪?
      

  9.   

    这是FontDlg.java    
        
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import javax.swing.event.*;public class FontDlg extends JDialog {
    // Define and Declaration //////////////////////////////////////////////////////
    // Declaration config items. /////////////////////////////////////////////////// String[] nameStrs={ "Arial", "Courier New", "Default", "Dialog", "DialogInput",
    "Monospaced", "Serif", "Times New Roman" };
    String[] styleStrs={ "Plain", "Bold", "Italic", "Bold Italic" };
    String[] sizeStrs={ "8", "9", "10", "12", "14", "15", "18", "20", "24" };
    Hashtable styleTbl=new Hashtable();

        String str=null;
        JTextField txt=null;
    // Define Components /////////////////////////////////////////////////////////// Container c;
    JPanel fontPnl=new JPanel(new GridBagLayout());
    JLabel nameLbl=new JLabel("Font Name:");
    JLabel styleLbl=new JLabel("Font Style");
    JLabel sizeLbl=new JLabel("Font Size:");
    JTextField nameTxt=new JTextField("Default");
    JTextField styleTxt=new JTextField("Plain");
    JTextField sizeTxt=new JTextField("15");
    JList nameLst=new JFList(nameStrs, nameTxt);
    JList styleLst=new JFList(styleStrs, styleTxt);
    JList sizeLst=new JFList(sizeStrs, sizeTxt);
    JPanel previewPnl=new JPanel(new FlowLayout(FlowLayout.CENTER));
    JLabel previewLbl=new JLabel("jNotepad Preview Text : AaBbCc123 Afritxia");
    JPanel buttonPnl=new JPanel(new FlowLayout(FlowLayout.CENTER));
    JButton okBtn=new JButton("OK");
    JButton closeBtn=new JButton("Close");// Constructor ///////////////////////////////////////////////////////////////// public FontDlg(JFrame f){
    super(f, "jNotepad Configure.", true);
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try{
    styleTbl.put(styleStrs[0], new Integer(Font.PLAIN));
    styleTbl.put(styleStrs[1], new Integer(Font.BOLD));
    styleTbl.put(styleStrs[2], new Integer(Font.ITALIC));
    styleTbl.put(styleStrs[3], new Integer(Font.BOLD | Font.ITALIC));

    initDialog();
    Point p=new Point(f.getLocation());
    int width=f.getSize().width;
    int height=f.getSize().height;
    setSize(380, 380);
    setLocation(p.x+(width-380)/2, p.y+(height-400)/2);  // Need to derive a new class to cover the incident methods.
    okBtn.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
      putFont(str, txt);
      dispose();
      }
      });
     
    closeBtn.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    dispose();
    }
    });
    addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){
    dispose();
    }
    });
    pack();
        show();
         }
           catch (Exception ex){
                  ex.printStackTrace();
              }
          }
     
    // Init Dialog ///////////////////////////////////////////////////////////////// private void initDialog(){
    c=getContentPane();
    c.setLayout(new BorderLayout());
    initFontPanel();
    getRootPane().setDefaultButton(closeBtn);
    }// Init Font Panel ///////////////////////////////////////////////////////////// private void initFontPanel(){
    GridBagConstraints gbc=new GridBagConstraints();
    gbc.fill=GridBagConstraints.BOTH;
    gbc.insets=new Insets(2, 2, 2, 2);
    gbc.gridx=0;
    gbc.gridy=0;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    gbc.weightx=1.0;
    gbc.weighty=0.0;
    fontPnl.add(nameLbl, gbc); // Add component
    gbc.gridx=1;
    gbc.gridy=0;
    fontPnl.add(styleLbl, gbc); // Add component
    gbc.gridx=2;
    gbc.gridy=0;
    fontPnl.add(sizeLbl, gbc); // Add component
    gbc.gridx=0;
    gbc.gridy=1;
    nameTxt.setEnabled(false);
    fontPnl.add(nameTxt, gbc); // Add component
    gbc.gridx=1;
    gbc.gridy=1;
    styleTxt.setEnabled(false);
    fontPnl.add(styleTxt, gbc); // Add component
    gbc.gridx=2;
    gbc.gridy=1;
    fontPnl.add(sizeTxt, gbc); // Add component
    gbc.gridx=0;
    gbc.gridy=2;
    fontPnl.add(nameLst, gbc); // Add component
    gbc.gridx=1;
    gbc.gridy=2;
    fontPnl.add(styleLst, gbc); // Add component
    gbc.gridx=2;
    gbc.gridy=2;
    fontPnl.add(sizeLst, gbc); // Add component
    gbc.fill=GridBagConstraints.HORIZONTAL;
    gbc.gridx=0;
    gbc.gridy=3;
    gbc.gridwidth=3;
    gbc.gridheight=1;
    gbc.weightx=0.0;
    gbc.weighty=0.0;
    previewPnl.setBorder(BorderFactory.createTitledBorder(" Font Preview "));
    previewPnl.add(previewLbl); // Add component
    fontPnl.add(previewPnl, gbc);
    c.add(fontPnl, BorderLayout.CENTER);
    buttonPnl.add(okBtn);
    buttonPnl.add(closeBtn);
    c.add(buttonPnl, BorderLayout.SOUTH);
    }// Fut Font When Click 'OK' Button ///////////////////////////////////////////// public void putFont(String str, JTextField txt){
    txt.setText(str);
    String name=nameTxt.getText();
    int style=((Integer)styleTbl.get(styleTxt.getText())).intValue();
    String sizeStr=sizeTxt.getText().trim();
    int size=11;
    if(sizeStr.length()>2)
    sizeStr.substring(2);
    try{
    size=Integer.parseInt(sizeStr);
    }catch(Exception Ex){
    }
    size=(size>=8)?(size<=24?size:24):8;
    previewLbl.setFont(new Font(name, style, size));
    Mynotebook.textArea1.setFont(new Font(name, style, size));
    //dispose();
    }


    // Class JFList //////////////////////////////////////////////////////////////// private class JFList extends JList{// Constructor ///////////////////////////////////////////////////////////////// public JFList(final String[] strs, final JTextField txt){
    super(strs);
    setBorder(BorderFactory.createEtchedBorder(
    new Color(255, 255, 255), new Color(96, 96, 96)
    ));
    addListSelectionListener(new ListSelectionListener(){
    public void valueChanged(ListSelectionEvent e){
    putFont(strs[getSelectedIndex()], txt);
    }
    });
    }
    }
    }
      

  10.   

    不好意思,一时之间还未能查出错误来。你把那个错误提示,就是Exception也贴出来看一下吧。另外你是不是要做一个FontChooser的东西啊?如果是,我写过一个,你看一下吧。希望能有用。
    /**
     * <p>Title: JavaMe</p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2003</p>
     * <p>Company: ISMT, HKUST</p>
     * @author group4
     * @version 1.0
     */
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    import javax.swing.text.*;import com.borland.jbcl.layout.*;
    import MainApplication.*;
    public class FontChooser implements ActionListener, ListSelectionListener
    {
      private JDialog dialog;
      private JPanel panel = new JPanel();
      private XYLayout xYLayout = new XYLayout();
      private JTextField nameField = new JTextField();
      private JTextField styleField = new JTextField();
      private JTextField sizeField = new JTextField();
      private JList nameList = new JList();
      private JPanel namePanel = new JPanel();
      private Border border;
      private TitledBorder titledBorder1, titledBorder2, titledBorder3;
      private JPanel stylePanel = new JPanel();
      private JPanel sizePanel = new JPanel();
      private JList styleList = new JList();
      private JList sizeList = new JList();
      private JButton okButton = new JButton();
      private JButton cancelButton = new JButton();  //font attributes
      private Font [] fonts;
      private String [] fontNames;
      private String [] fontStyles = {"Plain", "Bold", "Italic", "Bold & Italic"};
      private String [] fontSizes = {"8", "9", "10", "11", "12", "13", "14", "16", "18",
        "20", "22", "24", "28", "32", "36", "48", "60", "72"};
      private String name, styleString, sizeString;
      private int style = Font.PLAIN, size = 12;  private Font defaultFont = new Font("Times New Roman", Font.PLAIN, 12);  //approve status
      public static final int OK_OPTION = 1;
      public static final int CANCEL_OPTION = 2;
      public static final int ERROR_OPTION = 3;
      private int option;  private Frame frame;
      private String title;
      private boolean modal;  //constructor with arguments
      public FontChooser(Frame frame, String title, boolean modal)
      {
        this.frame = frame;
        this.title = title;
        this.modal = modal;
      }
      //constructor with arguments
      public FontChooser(Frame frame, String title, boolean modal, Font defaultFont)
      {
        this.defaultFont = defaultFont;
        this.frame = frame;
        this.title = title;
        this.modal = modal;
      }
      //constructor without arguments
      public FontChooser()
      {
        this(null, "Font Chooser", true);
      }
      //initialize method
      private void initDialog()
      {
        panel.setLayout(xYLayout);
        //buttons
        okButton.setText("OK");
        cancelButton.setText("Cancel");
        okButton.addActionListener(this);
        cancelButton.addActionListener(this);    xYLayout.setHeight(245);
        xYLayout.setWidth(340);    //customize titledBorders
        titledBorder1 = new TitledBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.black),"Font Name:");
        titledBorder2 = new TitledBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.black),"Font Style:");
        titledBorder3 = new TitledBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.black),"Font Size:");    //customize the three panels
        namePanel.setBorder(titledBorder1);
        namePanel.setLayout(new BorderLayout());
        stylePanel.setBorder(titledBorder2);
        stylePanel.setLayout(new BorderLayout());
        sizePanel.setBorder(titledBorder3);
        sizePanel.setLayout(new BorderLayout());    //text fields
        nameField.setEditable(false);
        styleField.setEditable(false);
        sizeField.setEditable(false);    stylePanel.add(styleField, BorderLayout.NORTH);
        stylePanel.add(new JScrollPane(styleList), BorderLayout.CENTER);
        panel.add(okButton, new XYConstraints(176, 195, 71, -1));
        panel.add(sizePanel, new XYConstraints(263, 16, 66, 163));
        namePanel.add(nameField, BorderLayout.NORTH);
        namePanel.add(new JScrollPane(nameList), BorderLayout.CENTER);
        sizePanel.add(sizeField, BorderLayout.NORTH);
        sizePanel.add(new JScrollPane(sizeList), BorderLayout.CENTER);
        panel.add(namePanel, new XYConstraints(15, 17, 148, 163));
        panel.add(cancelButton,  new XYConstraints(253, 195, -1, -1));
        panel.add(stylePanel,     new XYConstraints(169, 17, 90, 162));
        border = new MatteBorder(null);    namePanel.setVisible(true);
        stylePanel.setVisible(true);
        sizePanel.setVisible(true);    dialog.getContentPane().add(panel, BorderLayout.CENTER);
        dialog.setSize(342, 250);
        dialog.setResizable(false);
        //Center the window
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = dialog.getSize();
        if (frameSize.height > screenSize.height)
        {
          frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width)
        {
          frameSize.width = screenSize.width;
        }
        dialog.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);  }
      

  11.   

    //initialize method
      private void initializeList()
      {
        //get the fonts of the local platforms
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        fonts = ge.getAllFonts();
        fontNames = new String[fonts.length];
        for(int i=0; i<fonts.length; i++)
        {
          fontNames[i] = fonts[i].getName();
        }
        //create the list using the above attributes
        nameList = new JList(fontNames);
        styleList = new JList(fontStyles);
        sizeList = new JList(fontSizes);    //set the default value
        //name list
        nameList.setSelectedValue(defaultFont.getFontName(), true);
        nameList.ensureIndexIsVisible(nameList.getSelectedIndex());
        nameField.setText(defaultFont.getFontName());
        //style list
        if(defaultFont.getStyle() == Font.PLAIN)
        {
          styleField.setText("Plain");
          styleList.setSelectedValue("Plain", true);
        }
        if(defaultFont.getStyle() == Font.BOLD)
        {
          styleList.setSelectedValue("Bold", true);
          styleField.setText("Bold");
        }
        if(defaultFont.getStyle() == Font.ITALIC)
        {
          styleList.setSelectedValue("Italic", true);
          styleField.setText("Italic");
        }
        if(defaultFont.getStyle() == Font.BOLD+Font.ITALIC)
        {
          styleList.setSelectedValue("Bold & Italic", true);
          styleField.setText("Bold & Italic");
        }
        styleList.ensureIndexIsVisible(styleList.getSelectedIndex());    //size list
        sizeList.setSelectedValue(String.valueOf(defaultFont.getSize()), true);
        sizeList.ensureIndexIsVisible(sizeList.getSelectedIndex());
        sizeField.setText(String.valueOf(defaultFont.getSize()));    nameList.addListSelectionListener(this);
        styleList.addListSelectionListener(this);
        sizeList.addListSelectionListener(this);
      }
      //create the font chooser dialog
      public JDialog createDialog()
      {
        dialog = new JDialog(frame, title, modal);    //we must first initialize the list because otherwise it will not be correctly shown
        initializeList();
        initDialog();
        dialog.pack();    return dialog;
      }  //a method to get the font chooser shown and return the option the user chooses
      public int showFontChooser()
      {
        dialog = createDialog();    //dealing with the case when the dialog is shut down by click close button in the right-up corner.
        dialog.addWindowListener(new WindowAdapter()
                       {
                                   public void windowClosed(WindowEvent e)
                                   {
                                     option = CANCEL_OPTION;
                                   }
                       });    option = ERROR_OPTION;
        dialog.show();
        dialog.dispose();
        dialog = null;    return option;
      }
      //get selected font
      public Font getSelectedFont()
      {
        return new Font(name, style, size);
      }
      //action listener for the buttons
      public void actionPerformed(ActionEvent e)
      {
        //ok button clicked
        if(e.getSource() == okButton)
          {
            option = OK_OPTION;        name = (String)(nameList.getSelectedValue());
            styleString = (String)(styleList.getSelectedValue());
            sizeString = (String)(sizeList.getSelectedValue());
            if(styleString == "Plain")
            {
              style = Font.PLAIN;
            }
            else if(styleString == "Bold")
            {
              style = Font.BOLD;
            }
            else if(styleString == "Italic")
            {
              style = Font.ITALIC;
            }
            else if(styleString == "Bold & Italic")
            {
              style = Font.BOLD+Font.ITALIC;
            }        size = Integer.parseInt(sizeString);        dialog.dispose();
          }      //cancel button clicked
          if(e.getSource() == cancelButton)
          {
            option = CANCEL_OPTION;
            dialog.dispose();
          }
        }
      //list selection listener
      public void valueChanged(ListSelectionEvent e)
      {
         //name list
         if(e.getSource() == nameList)
         {
           name = (String)(nameList.getSelectedValue());
           nameField.setText(name);
         }
         //style list
         if(e.getSource() == styleList)
         {
           styleString = (String)(styleList.getSelectedValue());
           styleField.setText(styleString);
         }
         //size list
         if(e.getSource() == sizeList)
         {
           sizeString = (String)(sizeList.getSelectedValue());
           sizeField.setText(sizeString);
         }
      }}
      

  12.   

    to chenypluck():
      Your program is very long. I can't create a test case to run it to find the errors.
      One small suggestion though:
      in you FontDialog.java, you have:
         ....
         Mynotebook.textArea1.setFont(new Font(name, style, size));  This might not be a good way to encapsulate things. In this way, your FontDialog can't live without Mynotebook, right?   Better way to do:
       in your FontDialog.java:
          Font result = null;
          ...
          public Font getResult() {
            return result;
          }
       in MyNoteBook.
        ...
        FontDialog dlg = new FontD...
        dlg.show()
        if(dlg.isOK()) // you may change your mind to cancel it, right?
          textArea1.setFont(dlg.getResult());Sorry, my Chinese input software has some problem. I had to start it every time once I switched to English typing.
      

  13.   

    赫赫,亏helpall有心啊。不过他说的非常对。信息封装(隐藏)是面向对象的一大特色,也是一大要求。
      

  14.   

    恩,没有时间看完你的程序,这样吧,告诉我你要做什么。
    什么叫调用另一个JAVA程序,你直接NEW不行吗?