1

有一个小问题,不确定如何调用特定函数来打印其详细信息。我创建了一个单选按钮,用于检查 PC 上的总物理内存,也有一个用于 GPU 的按钮,两者都可以正常工作。

现在我不知道如何调用相同的函数,以便在我对特定系统属性进行系统扫描时打印在更大的窗口中。

if (isWindows()) {

        jTextArea1.setText(header + "User Name : " + name
                + "\nOperating System :" + jComboBox1.getSelectedItem()
                + "\nSelected Gamer Ability : " + this.jComboBox4.getSelectedItem()
                + "\nSelected Age Group :" + this.jComboBox5.getSelectedItem()
                + "\nSystem Version : " + System.getProperty("os.version")
                + "\nSystem Architecture : " + System.getProperty("os.arch")
  PROBLEM PART  + "\nSystem Total Ram : " + this.jRadioButton2......
                + "\nScan ID : " + n + "\n \n")

 }



private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt)       {                                              
    String filepath = "..\\Checker\\src\\batchchecker\\memory.bat";
    try 
    {
        Process p = Runtime.getRuntime().exec(filepath); // filepath
        p.waitFor();
        InputStream in = p.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int c = -1;
        while ((c = in.read()) != -1) 
        {
            baos.write(c);
        }
        String response = new String(baos.toByteArray());
        jRadioButton2.setText(evt.getActionCommand());
        JOptionPane.showMessageDialog(null, "" + evt.getActionCommand()
            + response);

        }
        catch (Exception e) 
        {
            e.printStackTrace(System.err);
        }
    }
}

从上面的代码可以看出,我的收音机做了它需要做的事情并经过了测试。我只是不确定如何将相同的结果称为更大的图景,它实际打印所有细节以及其余部分。代码行是+ "\nSystem Total Ram : " + this.jRadioButton2......

4

1 回答 1

0

似乎您只需要将实现移动到一个单独的方法,该方法可以从您的 actionPerformed() 方法和您的其他调用中调用。例如:

public String findMemoryDetails() {
    // ... put code here
}

然后在这里调用它:

private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    String memDetails = findMemoryDetails();
    JOptionPane.showMessageDialog(null, "" + evt.getActionCommand() + memDetails);
}

和这里:

+ "\nSystem Total Ram : " + findMemoryDetails()
于 2012-12-05T17:43:05.250 回答