【问题标题】:calling class from another class从另一个班级调用班级
【发布时间】:2016-07-26 05:35:32
【问题描述】:

我正在从我的类批处理运行中调用类 guibuilder。 batchrun 的代码是:-

public class batchrun {
public static String md5gen(String a) throws NoSuchAlgorithmException
{
    MessageDigest m= MessageDigest.getInstance("MD5");
    m.reset();
    m.update(a.getBytes());
    byte[] digest=m.digest();
    BigInteger bigInt = new BigInteger(1,digest);
    String hashtext = bigInt.toString(16);
    while(hashtext.length() < 32 ){
      hashtext = "0"+hashtext;
    }
    return hashtext;
}
private static String getInputAsString(InputStream is)
{
   try(java.util.Scanner s = new java.util.Scanner(is)) 
   { 
       return s.useDelimiter("\\A").hasNext() ? s.next() : ""; 
   }
}
public static void main(String[] args) throws InterruptedException {
    try {

        guibuilder.main(args);  
        guibuilder gb=new guibuilder();
        String fg=guibuilder.antd;
        String arg1=gb.arg;
        String userinp1=gb.userinp;

        System.out.println("FG="+fg+" arg1="+arg1+" userinp="+userinp1);


  Process pan =  Runtime.getRuntime().exec(new String[]    {"C:\\test1.bat",arg1,fg});

        pan.waitFor();



            String extra="\\";
            extra+=userinp1;
            String patha=fg+extra;
            ProcessBuilder pb = new     ProcessBuilder("adb","shell","getprop","ro.csc.sales_code");
            Process p=pb.start();
            p.waitFor();
            String stdout = getInputAsString(p.getInputStream());
            String newstring=stdout.substring(0,3);;
            String fn=fg+"\\"+newstring+".txt";
            ZipFile zipFile = new ZipFile(patha);
            Enumeration<?> enu = zipFile.entries();
            int flag=0;
            String so="so";
            File file = new File(fn); 
        FileOutputStream fos = new FileOutputStream(file);
            PrintStream ps = new PrintStream(fos);

            System.setOut(ps);

            while (enu.hasMoreElements()) {
                ZipEntry zipEntry = (ZipEntry) enu.nextElement();
                String name = zipEntry.getName();
                long size= zipEntry.getSize();
                String extension= name.substring(name.lastIndexOf(".")+1,name.length());

                if(extension.equals(so))
                {
                    String plaintext=name+size;
                    String md5result=md5gen(plaintext);
                    System.out.println(name+"   "+size+"   "+md5result);
                    ++flag;

                }

            }
            if(flag==0)
                System.out.println("fail");


}catch (IOException ex){
    System.out.println(ex.getMessage());
} catch (NoSuchAlgorithmException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
    }

}

guibuilder的代码是

public class guibuilder {

private JFrame frame;
public static String antd;
public static String arg;
public static String userinp;


/**
 * Launch the application.
 * @return 
 */
public static void main(String[] args) { 

    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                guibuilder window = new guibuilder();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

}

/**
 * Create the application.
 */
public guibuilder() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JButton btnExtract = new JButton("Extract");
    btnExtract.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            frame.dispose();
            fchooser fc1=new fchooser();
            antd=fc1.demo();
             arg=JOptionPane.showInputDialog("Enter the apk path");
             userinp=JOptionPane.showInputDialog("Enter the apk name");



        }
    });
    btnExtract.setBounds(69, 55, 89, 23);
    frame.getContentPane().add(btnExtract);

    JButton btnCompare = new JButton("Compare");
    btnCompare.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            frame.dispose();
            newjframe n = new newjframe();
            n.setVisible(true);
        }
    });
    btnCompare.setBounds(261, 55, 89, 23);
    frame.getContentPane().add(btnCompare);
}
}

我希望程序在继续执行 batchrun 中的代码之前等待 guibuilder 的执行。但是在这段代码中,我什至没有选择 guibuilder 中的文件,程序继续执行并且 System.out.println("FG="+fg+" arg1="+arg1+" userinp="+userinp1);在我在 guibuilder 中选择任何内容之前打印此行。

【问题讨论】:

标签: java swing class jfilechooser


【解决方案1】:

您的代码显示您的 java 项目有两个主要类。一个在batchrun 类中,另一个在guibuilder 类中。 [来自你的声明guibuilder.main(args)]

在您的项目中只使用一个主类。这可能会解决您的问题。

我认为您的 guibuilder 类具有这样的结构

class guibuilder{
    ...... //global variables

    public static void main(String[] args){
        ......... //statments

    }
}  

不要在一个项目中使用两个主要方法。

你需要像这样构造你的 guibuilder 类(如下所示)

class guibuilder{
    ...... //global variables

    public static void buildGui(){//you can use any method name here
        ......... //statments

    }
} 

要从另一个类调用此方法只需使用此语句

guibuilder.buildGui()

或者,另一种方式

class guibuilder{
    ...... //global variables

    public void buildGui(){//you can use any method name here
        ......... //statments

    }
} 

要从另一个类调用此方法,请使用此语句

guibuilder gui=new guibuilder();
gui.buildGui();

【讨论】:

  • 感谢 Nasir,但我正在使用 windowbuilder 插件,它会自动生成类。我想运行整个 guibuilder 类,然后去 batchrun,这是最简单的方法(使用两个电源)。
  • 然后简单地从 guibuilder 源中删除主类。我认为可以删除该文件
猜你喜欢
  • 1970-01-01
  • 2017-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多