-1

在这段代码中,我们在下面的代码中称为类“文件面板”。它既不是静态内部也不是内部,那么我们称之为什么?我想使用 AST 解析器从 java 代码中检查这种类型的类

    public class choosefile extends JFrame implements ActionListener{
    private static final long serialVersionUID = 1L;
    JButton file,go;
    private Dimension preferredDimensions;
    public choosefile(){
        preferredDimensions = new Dimension(800, 700);
            JFrame frame=new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setResizable(false);
            filepanel filepnl=new filepanel();
            frame.setPreferredSize(preferredDimensions);
            frame.add(filepnl);
            frame.pack();
            frame.setLocation(50,0);
            frame.setVisible(true);


    }
    @Override
    public void actionPerformed(ActionEvent e)
    {

    }
    public static void main(String[] args)
    {
        new choosefile();
    }

}
class filepanel extends JPanel implements ActionListener{

    private static final long serialVersionUID = 1L;
    JScrollPane scroll,scroll1;
     JScrollBar bar;
    JButton file,go;
    JTextField txtbx;
    JTextArea txtarea1,txtarea2;
    public filepanel()
    {
         try {
                initComponents();

            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "Could not load frame", "Main Menu Error: 0x004", JOptionPane.ERROR_MESSAGE);
                System.exit(4);
            }
    }
      private void initComponents() throws Exception {
          setLayout(new GridBagLayout());
           GridBagConstraints c = new GridBagConstraints();
          file=new JButton("Choose File..");
         // JButton button1 = new JButton("1");
           file.addActionListener(this);


           c.fill = GridBagConstraints.PAGE_START;
           c.gridx = 0;
           c.gridy = 0;
           add(file, c);

           txtbx = new JTextField(" ");
           txtbx.setPreferredSize(new Dimension(150, 20));
           c.fill = GridBagConstraints.CENTER;
           c.weightx = 0.0;
           c.gridx = 1;
           c.gridy = 0;

           add(txtbx , c);

           go = new JButton(" Go ");
           go.addActionListener(this);

           go.setPreferredSize(new Dimension(5, 20));
           c.fill = GridBagConstraints.HORIZONTAL;
           c.weightx = 0.5;
           c.gridx = 2;
           c.gridy = 0;
           //c.insets = new Insets(0,350,0,0);
           add(go, c);
          txtarea1=new JTextArea();
          scroll = new JScrollPane(txtarea1);
           scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

          txtarea1.setPreferredSize(new Dimension(250, 620));
          c.fill = GridBagConstraints.HORIZONTAL;
           c.weightx = 0.5;
           c.gridx = 0;
           c.gridy = 1;
           c.insets = new Insets(0,10,0,0);
           add(scroll, c);
           txtarea2=new JTextArea();
           scroll1 = new JScrollPane(txtarea2);
           scroll1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
              txtarea2.setPreferredSize(new Dimension(250, 620));
              c.fill = GridBagConstraints.HORIZONTAL;
               c.weightx = 0.5;
               c.gridx = 1;
               c.gridy = 1;
               c.insets = new Insets(0,3,0,0);
               add(scroll1, c);

      }
    @Override
    public void actionPerformed(ActionEvent e) {

        if(e.getSource() == file)
        {
            JFileChooser fileopen = new JFileChooser();
            FileNameExtensionFilter filter = new FileNameExtensionFilter(".java", ".java");
            fileopen.addChoosableFileFilter(filter);

            int ret = fileopen.showDialog(null, "Open file");

            if (ret == JFileChooser.APPROVE_OPTION) {
              File file = fileopen.getSelectedFile();       
              String text = readFile(file);
              txtarea1.setText(text);
             text=search(file);
              txtarea2.setText(text);
            }
        }
        }
    private String readFile(File file2)
    {
    StringBuffer fileBuffer = null;
        String fileString = null;
        String line = null;
    try
    {
            FileReader in = new FileReader(file2);
            BufferedReader brd = new BufferedReader(in);
            fileBuffer = new StringBuffer();

            while ((line = brd.readLine()) != null) {
               fileBuffer.append(line).append( System.getProperty("line.separator"));

               }

            in.close();
            fileString = fileBuffer.toString();
        } catch (IOException e) {
            return null;
        }
        return fileString;
    }
    String search(File file1){
        //JTree tree = new JTree();
        StringBuffer fileBuffer = null;
        String fileString = null;

          try{
                // Open the file that is the first 
                // command line parameter
                FileInputStream fstream = new FileInputStream(file1);


                BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
                String strLine;
                fileBuffer = new StringBuffer();

                //Read File Line By Line            
                while ((strLine = br.readLine()) != null){
                     if (strLine.contains("import")){

                         fileBuffer.append("The import library  is: " + strLine.substring(strLine.indexOf("import ") + 6, strLine.indexOf(";"))).append( System.getProperty("line.separator"));
                         //System.out.println ("The import library  is: " + strLine.substring(strLine.indexOf("import ") + 6, strLine.indexOf(";")));
                     }


                    if (strLine.contains("class")){
                         fileBuffer.append("The class name is: " + strLine.substring(strLine.indexOf("class ") + 6, strLine.indexOf("{"))).append( System.getProperty("line.separator"));

                       // System.out.println ("The class name is: " + strLine.substring(strLine.indexOf("class ") + 6, strLine.indexOf("{")));
                    }

                     if (strLine.contains("int")){
                         fileBuffer.append("There is an int variable named: " + strLine.substring(strLine.indexOf("int ") + 4, strLine.indexOf(";"))).append( System.getProperty("line.separator"));

                        //System.out.println("There is an int variable named: " + strLine.substring(strLine.indexOf("int ") + 4, strLine.indexOf(";")));
                    }
                     if (strLine.contains("//")){
                         fileBuffer.append("Comment contains: " + strLine.substring(strLine.indexOf("//") + 2));

                        //System.out.println("Comment contains: " + strLine.substring(strLine.indexOf("//") + 2));
                    }
                    // fileBuffer.append(strLine).append( System.getProperty("line.separator"));

                }


                //Close the input stream
                in.close();
                fileString = fileBuffer.toString();
            }

            catch (Exception e){
                //Catch exception if any
                System.err.println("Error: " + e.getMessage());
            }

        return fileString;

    }

}
4

1 回答 1

0

filepanel只是在与类相同的文件中定义的另一个 。您可以在 .java 文件中定义任意数量的类。但是每个 .java 文件只能有一个公共类。Class.javachoosefile

于 2013-04-04T08:47:45.683 回答