march alex's blog
          hello,I am march alex
          posts - 52,comments - 7,trackbacks - 0
          今天開始準備寫一個Java可以跑得python編譯器。
          基本假設是這樣的:
              有一個JFrame,里面有一框,是輸入框也是輸出框,一開始如果有需要輸入的內容得先寫到上面去;
              然后單擊菜單欄打開python文件;
              然后得到的結果就會顯示出來。
          目前知識有了一個框架,當test.py內容為:
          print 123456789+123456789000000000
          時,運行程序,現實的效果如下:

          代碼如下:
          import java.awt.event.ActionEvent;
          import java.awt.event.ActionListener;
          import java.io.BufferedReader;
          import java.io.File;
          import java.io.FileNotFoundException;
          import java.io.FileReader;
          import java.io.IOException;
          import java.util.StringTokenizer;

          import javax.swing.JFileChooser;
          import javax.swing.JFrame;
          import javax.swing.JLabel;
          import javax.swing.JList;
          import javax.swing.JMenu;
          import javax.swing.JMenuBar;
          import javax.swing.JMenuItem;
          import javax.swing.JTextArea;


          public class CompilerOpener extends JFrame {

              private static final int Width = 1000;
              private static final int Height = 600;
              private static JFrame frame = null;
              private static JTextArea textArea = null;
              
              
              public CompilerOpener() {
                  frame = new JFrame("Java的python編譯器");
                  
                  textArea = new JTextArea();
                  textArea.setLineWrap(true);// 激活自動換行功能
                  frame.add(textArea);
                  
                  JMenuBar menuBar = new JMenuBar();
                  frame.setJMenuBar(menuBar);
                  
                  JMenu fileMenu = new JMenu("文件");
                  JMenuItem openItem = new JMenuItem("打開");
                  openItem.addActionListener(new MyAction());
                  fileMenu.add(openItem);
                  menuBar.add(fileMenu);
                  
                  frame.setLocationRelativeTo(null);
                  frame.setSize(Width, Height);
                  frame.setLocation(100, 100);
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  frame.setVisible(true);
              }
              
              private class MyAction implements ActionListener {
                  public void actionPerformed(ActionEvent evt) {
                      //Object s = evt.getSource();
                      JFileChooser jfc=new JFileChooser();
                      jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
                      jfc.showDialog(new JLabel(), "選擇");
                      File file=jfc.getSelectedFile();
                      int file_len = file.toString().length();
                      String ans = new String();
                      if(!file.isFile() || !file.toString().substring(file_len-3, file_len).equals(".py")) {
                          ans = "請確定你選擇的文件是一個正確的python文件!";
                      } else {
                          String content = textArea.getText();
                          ans += content;
                          ans += "結果:\n";
                          try {
                              BufferedReader reader = new BufferedReader(new FileReader(file.toString()));
                              String line = null;
                              try {
                                  while((line = reader.readLine()) != null) {
                                      StringTokenizer st = new StringTokenizer(line , " +");
                                      String s , a , b;
                                      s = st.nextToken();
                                      a = st.nextToken();
                                      b = st.nextToken();
                                      char[] ca = a.toCharArray();
                                      char[] cb = b.toCharArray();
                                      int lena = ca.length, lenb = cb.length;
                                      int c = 0,len = lena > lenb ? lena : lenb;
                                      int[] aa = new int[len+1];
                                      for(int i=0;i<len;i++) {
                                          if(lena-1-i >= 0)
                                              c += (ca[lena-1-i] - '0'); 
                                          if(lenb-1-i >= 0)
                                              c += (cb[lenb-1-i] - '0');
                                          aa[i] = c % 10;
                                          c /= 10;
                                      }
                                      if(c > 0) aa[len++] = 1;
                                      for(int i=len-1;i>=0;i--) ans += aa[i];
                                      ans += "\n";
                                  }
                                  textArea.setText(ans);
                              } catch (IOException e) {
                                  e.printStackTrace();
                              }
                          } catch (FileNotFoundException e) {
                              e.printStackTrace();
                          }
                          
                      }
                      
                  }
              }
              
              public static void main(String[] args) {
                  new CompilerOpener();
              }
          }
          posted on 2015-03-20 14:19 marchalex 閱讀(185) 評論(0)  編輯  收藏 所屬分類: java小程序
          主站蜘蛛池模板: 邻水| 龙陵县| 花莲县| 锦州市| 宁陵县| 凤翔县| 宜川县| 城步| 蒲城县| 蓝山县| 淳化县| 宜阳县| 阳曲县| 松潘县| 天门市| 霍林郭勒市| 徐水县| 富宁县| 宁安市| 托里县| 双鸭山市| 昌平区| 永修县| 山阳县| 阳曲县| 平和县| 江津市| 高碑店市| 新邵县| 旬邑县| 乌兰县| 湟中县| 奉化市| 松滋市| 惠州市| 长葛市| 伊川县| 淮安市| 卓资县| 昌江| 耒阳市|