【问题标题】:Calling a function that already works for a radiobutton so it prints the details调用已经适用于单选按钮的函数,以便打印详细信息
【发布时间】:2012-12-05 17:02:42
【问题描述】:

有一个小问题,不确定如何调用特定函数来打印其详细信息。 我创建了一个单选按钮,用于检查 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......

【问题讨论】:

  • 您希望private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) 再次运行吗?我觉得我没看清楚……

标签: java swing processbuilder


【解决方案1】:

似乎您只需要将实现移动到一个单独的方法,该方法可以从您的 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()

【讨论】:

  • 非常感谢这一切都很好!对 GPU 做了同样的事情,以类似的方式调用它
  • @JimClyne 酷。如果需要,您也可以将此标记为正确答案。
猜你喜欢
  • 2017-04-11
  • 1970-01-01
  • 2015-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-06
相关资源
最近更新 更多