0

今日、私は友人のためにURDu(ARABIC like Language)用のメモ帳エディターを作成しました...私が思いついたコードは非常に長く、これを行う正しい方法ではないと思います。特にスイッチケースがあります。 JFrame->JTextPanelにUTF-8文字を直接書き込むその他の方法

import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.util.regex.*;
import java.awt.event.*;
class MyJFrames extends JFrame implements ActionListener{
private JTextArea txt;
private JMenuBar mb;
private JMenu file=new JMenu("File"); 
private JMenu edit=new JMenu("Edit");  
private JMenu format=new JMenu("Format"); 
private JMenuItem open;
private JMenuItem save;
private JMenuItem saveAs;
private JMenuItem exit;
private JMenuItem find;
private JMenuItem findAndReplace;
private JCheckBoxMenuItem wrap;
private JMenuItem font;
private JScrollPane pane=null;
private MyFiling mf=new MyFiling();
private static String fileName;
private static int saveValue=0;
private JComboBox fontCombo;
private Font ffont;
MyJFrames(){
    super("Urdu Notepad");
        mb=new JMenuBar();
    txt = new JTextArea();
    txt.setFont(new Font("Arial",1,30));
    txt.setWrapStyleWord(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    txt.grabFocus();
    txt.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    open=new JMenuItem("Open"); 
    save=new JMenuItem("Save"); 
    saveAs=new JMenuItem("Save As"); 
    exit=new JMenuItem("Exit");
    find=new JMenuItem("Find");
    findAndReplace=new JMenuItem("Replace");
    wrap=new JCheckBoxMenuItem("Wrap");
    font=new JMenuItem("Font");
    file.add(open);
    file.add(save);
    file.add(saveAs);
    file.add(exit);
    edit.add(find);
    edit.add(findAndReplace);
    format.add(wrap);
    format.add(font);
    mb.add(file);
    mb.add(edit);
    mb.add(format);
    pane=new JScrollPane(txt);
    setJMenuBar(mb);
    add(pane);
    txt.addKeyListener(new keyAdapterOnText());
    open.addActionListener(this);
    save.addActionListener(this);
    saveAs.addActionListener(this);
    exit.addActionListener(this);
    find.addActionListener(this);
    findAndReplace.addActionListener(this);
    wrap.addActionListener(this);
    font.addActionListener(this);
    setSize(600,600);
    setVisible(true);

}

private class keyAdapterOnText extends KeyAdapter{
keyAdapterOnText(){
}
public void keyTyped(KeyEvent key){
char enter=key.getKeyChar();
key.consume();
String st=txt.getText();
switch(enter){
case 'a':
    txt.setText(st+"\u0627");
    break;
case 'b':
txt.setText(st+"\u0628");
break;
case 'c':
txt.setText(st+"\u0686");
break;
case 'd':
txt.setText(st+"\u062F");
break;
case 'e':
txt.setText(st+"\u0639");
break;
case 'f':
txt.setText(st+"\u0641");
break;
case 'g':
txt.setText(st+"\u06AF");
break;
case 'h':
txt.setText(st+"\u06BE");
break;
case 'i':
txt.setText(st+"\u064A");
break;
case 'j':
txt.setText(st+"\u062C");
break;
case 'k':
txt.setText(st+"\u06A9");
break;
case 'l':
txt.setText(st+"\u0644");
break;
case 'm':
txt.setText(st+"\u0645");
break;
case 'n':
txt.setText(st+"\u0646");
break;
case 'o':
txt.setText(st+"\u0665");
break;
case 'p':
txt.setText(st+"\u067E");
break;
case 'q':
txt.setText(st+"\u0642");
break;
case 'r':
txt.setText(st+"\u0631");
break;
case 's':
txt.setText(st+"\u0633");
break;
case 't':
txt.setText(st+"\u062A");
break;
case 'u':
txt.setText(st+"\u0674");
break;
case 'v':
txt.setText(st+"\u0637");
break;
case 'w':
txt.setText(st+"\u0648");
break;
case 'x':
txt.setText(st+"\u0634");
break;
case 'y':
txt.setText(st+"\u06D2");
break;
case 'z':
txt.setText(st+"\u0632");
break;
case 'A':
txt.setText(st+"\u0622");
break;
case 'B':
txt.setText(st+"\u0628");
break;
case 'C':
txt.setText(st+"\u062B");
break;
case 'D':
txt.setText(st+"\u0688");
break;
case 'E':
txt.setText(st+"\u0639");
break;
case 'F':
txt.setText(st+"\u0641");
break;
case 'G':
txt.setText(st+"\u063A");
break;
case 'H':
txt.setText(st+"\u062D");
break;
case 'I':
txt.setText(st+"\u0627");
break;
case 'J':
txt.setText(st+"\u0636");
break;
case 'K':
txt.setText(st+"\u062E");
break;
case 'L':
txt.setText(st+"\u0644");
break;
case 'M':
txt.setText(st+"\u0627");
break;
case 'N':
txt.setText(st+"\u06BA");
break;
case 'O':
txt.setText(st+"\u0629");
break;
case 'P':
txt.setText(st+"\u0654");
break;
case 'Q':
txt.setText(st+"\u0642");
break;
case 'R':
txt.setText(st+"\u0691");
break;
case 'S':
txt.setText(st+"\u0635");
break;
case 'T':
txt.setText(st+"\u0679");
break;
case 'U':
txt.setText(st+"\u0621");
break;
case 'V':
txt.setText(st+"\u0638");
break;
case 'W':
txt.setText(st+"\u0648");
break;
case 'Y':
txt.setText(st+"\u06D2");
break;
case 'Z':
txt.setText(st+"\u0630");
break;
case ' ':
txt.setText(st+" ");
break;
}
}
}
public void actionPerformed(ActionEvent e){
    String title=e.getActionCommand();
    if(title.compareTo("Open")==0){
        JFileChooser chooser= new JFileChooser();
        chooser.showOpenDialog(this);
        String path= chooser.getSelectedFile().getAbsolutePath();
        if(path!=null){
            txt.setText("YOU selected file path = "+path);
            try{
                mf.showFile(path);
            }catch(IOException eX){
                System.out.println(" EXCEPTION CAUGHT "+eX);
            }
        }
    }
    else if(title.compareTo("Save")==0){
        if(saveValue==0){
            JFileChooser chooser= new JFileChooser();
            int option=chooser.showSaveDialog(this);
            File file=chooser.getSelectedFile();
            fileName=file.getName();
            System.out.println("saveD and option = "+option+" filename "+fileName);
            if(option==0){
                try{
                    mf.saveFile(txt.getText(),fileName);
                    saveValue=1;
                }catch(Exception exe){;}
            }
            else{;}             
        }
        else{
            try{
                mf.saveFile(txt.getText(),fileName);
            }catch(Exception exe){
                System.out.println(" EXCEPTION CAUGHT "+exe);
            }
        }
    }
    else if(title.compareTo("Save As")==0){
        JFileChooser chooser= new JFileChooser();
        int option=chooser.showSaveDialog(this);
        File file=chooser.getSelectedFile();
        fileName=file.getName();
        System.out.println("saved and option = "+option+" filename "+fileName);
        if(option==0){
            try{
                mf.saveFile(txt.getText(),fileName);
            }catch(Exception exe){;}
        }
    }
    else if(title.compareTo("Exit")==0){
        setVisible(false);
        System.exit(0);
    }
    else if(title.compareTo("Find")==0){
        String inputValue = JOptionPane.showInputDialog("Find");
        if(inputValue!=null){
            String currentText=txt.getText();
            if(currentText.toLowerCase().contains(inputValue.toLowerCase())){
                JOptionPane.showMessageDialog(this, "String FOUND ");
            }
            else
                JOptionPane.showMessageDialog(this, "String NOT FOUND ");
        }
    }
    else if(title.compareTo("Replace")==0){
        String findValue = JOptionPane.showInputDialog("Find");
        String currentText;
        if(findValue!=null){
            currentText=txt.getText();
            if(currentText.toLowerCase().contains(findValue.toLowerCase())){
                String replaceValue = JOptionPane.showInputDialog("Replace");
                String replaceText = currentText.replace(findValue,replaceValue);
                txt.setText(replaceText);
            }
            else
                JOptionPane.showMessageDialog(this, "STRING IS NOT FOUND ");
        }
    }
    else if(title.compareTo("Wrap")==0){
        if(wrap.getState()){
            txt.setLineWrap(true);
        }
        else{
            txt.setLineWrap(false);
        }
    }

}
private class MyFiling{
    public void showFile(String path)throws IOException{
        File f=new File(path);
        FileInputStream fst=new FileInputStream(f);
        int i=0;
        String text="";
        while((i=fst.read())!=-1){
            text=text+(char)i;
        }
        txt.setText(text);
        fst.close();
    }
    public void saveFile(String text,String name)throws Exception{
        name=name+".txt";
        FileOutputStream out=new FileOutputStream(name);
        out.write(text.getBytes());
        out.close();
    }
}
}
4

1 に答える 1

0

JTextArea ではなく、ファイルの読み取り/書き込みに問題があります。

テキストファイルの読み書きにはReader/を使用してください。Writerそれらを正しいエンコーディングで使用すれば、すべて問題ありません。内部的に Java は UTF-16 で動作し、変換は aReaderまたはWriter.

現時点では、ストリームからバイトを読み取っていて、それらを文字として処理しようとすると、ほとんどの場合エラーが発生します。

于 2012-12-18T07:07:54.557 に答える