我想用Java写个记事本.发现Java比其他语言弱多了,没有MessageBox,没有状态栏,这两个还能靠自己勉强解决.可是,这个字体选择(notepad-->格式-->字体)的对现在的我来说实在无能为力了.网上有很多现成的源码100%没实现这个功能,顶多是饶过这个.所以,向大家求助了!

解决方案 »

  1.   

    看看jdk目录下的demo\jfc\Stylepad例子,有好多功能
      

  2.   

    我知道JDK里有个记事本demo,不过那里面的东西对我们中国人来说好象是乱七八糟的,要不然早被人提取出来开源了.
      

  3.   

    你用什么做的啊?要知道有很多东西都是系统级别的,java要做到跨平台就要脱离这些东西...你可以学下SWT,那里面提供了FontDialog之类的东西,完全调用windows中的Dialog
      

  4.   


    JComboBox fontList = new JComboBox();

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

    String[] fonts = ge.getAvailableFontFamilyNames();DefaultComboBoxModel fontModel = new DefaultComboBoxModel(fonts);

    fontList.setModel(fontModel);/*
     //进行事件处理
     ......
     ......
    *///import java.awt.*;
    //import javax.swing.*;
    //import java.awt.event.*;
      

  5.   

    就是调用系统的FontDialog,去看看别人的Notepad Demo,应该会有很多的
      

  6.   

    劝楼主还是放弃之
    我也是写了一半就觉的没意思了
    放弃了import java.awt.*;
    import java.awt.event.*;
    import java.io.*;public class MyNote extends Frame{
    private MenuBar mb = null;
    private TextArea ta = null;
    private Menu m1 = null;
    private Menu m2 = null;
    private Panel p = null;
    BufferedReader fr = null;
    BufferedWriter fw = null;
    MenuItem[] filem = {new MenuItem("新建"),new MenuItem("打开"),new MenuItem("保存"),new MenuItem("另存为"),new MenuItem("退出")};
    MenuItem[] editmenu = {new MenuItem("剪切"),new MenuItem("复制"),new MenuItem("粘贴"),new MenuItem("删除"),new MenuItem("全选")};
    public MyNote(){
    mb = new MenuBar();
    ta = new TextArea();
    m1 = new Menu("文件");
    m2 = new Menu("编辑");
    p = new Panel();
    for(int i = 0;i < filem.length;i++){
    filem[i].addActionListener(new MyActionListener());
    m1.add(filem[i]);
    }
    for(int i = 0;i < editmenu.length;i++){
    m2.add(editmenu[i]);
    }
    mb.add(m1);
    mb.add(m2);
    this.setMenuBar(mb);
    p.setSize(10,20);
    add(p,BorderLayout.EAST);

    this.add(ta,BorderLayout.CENTER);



    public void launchFrame(){
    this.setTitle("我的记事本");
    this.setBounds(400,400,400,400);
    pack();

    this.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent we){
    System.exit(0);
    }
    });
    this.setVisible(true);
    }
    private class MyActionListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
    String text = ((MenuItem)e.getSource()).getActionCommand();
    if(text == "退出"){
    System.exit(0);
    }else if(text == "打开"){
    openFile();
    } else if(text == "保存"){
    saveFile();
    } else if(text == "另存为"){
    anotherSaveFile();
    }
    }

    }

    public static void main(String[] args){
    MyNote mn = new MyNote();
    mn.launchFrame();
    }
    public void saveFile(){
    FileDialog fd = new FileDialog(this,"保存文件",FileDialog.SAVE);
    File file = null;
    fd.setVisible(true);
    System.out.println(fd.getDirectory()+fd.getFile());
    file = new File(fd.getDirectory()+fd.getFile());
    String []str = ta.getText().split("\n");
    try{
    fw = new BufferedWriter(new FileWriter(file));
    for(int i = 0;i < str.length; i++){
    fw.write(str[i]);
    fw.newLine();
    fw.flush();
    }
    }catch(FileNotFoundException e2){
    e2.printStackTrace();
    }catch(IOException e1){
    e1.printStackTrace();
    }finally{
    try {
    fw.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    public void openFile(){
    FileDialog fd = new FileDialog(this,"打开文件",FileDialog.LOAD);
    fd.setVisible(true);
    String str = null;
    System.out.println(fd.getDirectory()+fd.getFile());
    ta.setText("");
    try {
    fr = new BufferedReader(new FileReader(fd.getDirectory()+fd.getFile()));
    while((str =fr.readLine()) != null){
    if (ta.getText() == null){
    ta.setText(str);
    }else {
    ta.setText(ta.getText()+"\n"+str);
    }
    }
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }catch(IOException e){
    e.printStackTrace();
    }


    }
    public void anotherSaveFile(){
    FileDialog fd = new FileDialog(this,"另存为",FileDialog.SAVE);
    File file = null;
    fd.setVisible(true);
    System.out.println(fd.getDirectory()+fd.getFile());
    file = new File(fd.getDirectory()+fd.getFile());
    String []str = ta.getText().split("\n");
    try{
    fw = new BufferedWriter(new FileWriter(file));
    for(int i = 0;i < str.length; i++){
    fw.write(str[i]);
    fw.newLine();
    fw.flush();
    }
    }catch(FileNotFoundException e2){
    e2.printStackTrace();
    }catch(IOException e1){
    e1.printStackTrace();
    }finally{
    try {
    fw.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    }
      

  7.   

    楼上的那个是网络上普遍存在的不完整的记事本代码.5楼的建议不错,可是连那个界面我设计都很要命的.6.7楼的我还在用awt不想太早进入swing.
      

  8.   


    /*
     * 以下代码在 Windows XP + JDK 6 测试通过,2008年11月14日,oracle_lover
     */
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;public class FontTest extends JFrame implements ActionListener {  // 主类直接继承了 JFrame,实现了 ActionListener 接口  private static final long serialVersionUID = -2344998755780835481L;
      // 获取当前的环境变量
      private java.awt.GraphicsEnvironment env = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment();
      // 字体名字,风格,大小的下拉框
      private JComboBox cbFontName, cbFontStyle, cbFontHeight;
      // 文本编辑框
      private JTextArea taEditor;  public FontTest() {
        super("FontTest"); // 继承父类的构造函数
        this.setSize(640, 480); // 设置窗口大小
        this.setLocationRelativeTo(null); // 窗口屏幕居中
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 窗口关闭,则程序退出
        Container container = this.getContentPane();    // 获取系统中所有可用字体的名字,并加入下拉框
        String[] fontNames = env.getAvailableFontFamilyNames();
        cbFontName = new JComboBox(fontNames);
        // 将三种字体风格加入下拉框
        String[] fontStyles = { "普通", "粗体", "斜体" };
        cbFontStyle = new JComboBox(fontStyles);
        // 将 12~21 字号加入下拉框
        String[] fontHeights = new String[10];
        for (int i = 12; i < 22; i++)
          fontHeights[i - 12] = new String(Integer.toString(i));
        cbFontHeight = new JComboBox(fontHeights);    // 创建执行按钮,并加入监听
        JButton btnGo = new JButton("应用字体");
        btnGo.setActionCommand("Apply Font");
        btnGo.addActionListener(this);    // 创建一个面板,把三个下拉框,按钮放进去,面板默认是 FlowLayout
        JPanel paneTop = new JPanel();
        paneTop.add(cbFontName);
        paneTop.add(cbFontStyle);
        paneTop.add(cbFontHeight);
        paneTop.add(btnGo);    // 创建文本框,并放入滚动条
        taEditor = new JTextArea();
        taEditor.append("试试字体会不会变");
        JScrollPane sp = new JScrollPane(taEditor);    // 将各组件加入窗口
        container.add(paneTop, BorderLayout.PAGE_START);
        container.add(sp, BorderLayout.CENTER);
      }  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 fontHeight = Integer.parseInt(cbFontHeight.getSelectedItem().toString());
          // 创建一个新字体
          Font font = new Font(fontName, fontStyle, fontHeight);
          // 文本框应用新字体
          taEditor.setFont(font);
        }
      }  // 创建GUI
      private static void createAndShowGUI() {
        FontTest xframe = new FontTest();
        xframe.setVisible(true);
      }  public static void main(String[] args) {
        // 为事件处理线程安排一个任务:创建并显示程序的GUI
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            createAndShowGUI();
          }
        });
      }}
      

  9.   

    void draw(Graphics2D g2d){
    g2d.setPaint(new Color(R,G,B));//可以设置字体颜色,R,G,B已经赋值国
    g2d.setFont(new Font(name,italic+bold,((int)stroke)*18));//name的数组在下面,italic为斜体,bold为粗体,italic+bold  就是2者都有 最后的参数设置粗细,stroke已经赋值,它是单精度型
    if(word!=null){
    g2d.drawString(word,x1,y1);//这是在面板上画出文本,x1,y1表示坐标
    }
    }     String name[]={
    //显示文字的样式
    "宋体",
    "隶书",
    "华文彩云",
    "华文行楷",
    "方正舒体",
    "Times New Roman",
    "Serif",
    "Monospaced",
    "仿宋_GB2312"
    };以上是我编写画图版里面用来改变字体格式的一部分,希望能对你有用
      

  10.   

    http://blog.chinaunix.net/u/21684/showart_1149593.html
    这个是完整的记事本代码
      

  11.   

    我真不想看见swing啊!只能用awt.要不谁给我设计这个精简版的界面,就是弹出个Dialog(3个label,3个listBox,1个按钮)
    图片地址http://hiphotos.baidu.com/sageking2/pic/item/6d658fd9e2f655f538012fc9.jpg
    我尝试了,不过我这里怎么都显示不出来,以前设计的好象今天不能用,见鬼了.我用了setVisible(true);也设置了大小.
      

  12.   

    else if(st.equals("确定")){//弹出字体对话框
                  if (jtf1.getText().equals("PLAIN"))//如果选择了PLAIN
                  {
                  int fontsize = Integer.parseInt(jtf2.getText());//字体大小
                  int fontstyle = 0;  //0就是一种类型
                  Font f = new Font("字体设置", fontstyle, fontsize);  //设置字体
                  t.setFont(f);//设置文本域里的字体为f里的参数
                  }
                  if (jtf1.getText().equals("BOLD")) //同上
                  {
                  int fontsize = Integer.parseInt(jtf2.getText());
                  int fontstyle = 1;
                  Font f = new Font("字体设置", fontstyle, fontsize);
                  t.setFont(f);
                  }
                  if (jtf2.getText().equals("ITALIC"))//同上
                  {
                  int fontsize = Integer.parseInt(jtf2.getText());
                  int fontstyle = 2;
                   Font f = new Font("字体设置", fontstyle, fontsize);
                   t.setFont(f);
                  }
                  dfornt.dispose();
                }有没有不知道,当年我自己是这样写的.
      

  13.   

    swing 和 awt 对比着来吧.