2012-05-31 39 views
2

我將爲Linux開發一個任務管理器,目前處於初級階段。首先,我在Windows平臺的Java Eclipse中實現了它,它工作得很好。它在Linux平臺中不起作用。我已經使用了Java/Swing的組合。針對linux的TaskManager - Java Swing能在Linux上工作嗎?

窗口中的「tasklist.exe」工作正常。 Linux中的「ps-aux」不起作用。

代碼:

package test.t100.t001; 

import java.awt.*; 
import java.awt.event.KeyEvent; 
import java.io.BufferedReader; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintStream; 

import javax.swing.*; 

public class TabbedPaneDemo extends JPanel { 

    private static final long serialVersionUID = 1L; 
    Integer i; 

    JTextArea output = new JTextArea(); 

    public static void main(String args[]) 
    { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() 
      { 
       JFrame frame = new JFrame("TabbedPaneDemo"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new TabbedPaneDemo(), BorderLayout.CENTER); 
       frame.pack(); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    private String getDetails() throws IOException { 
     //fn(); 
     String line; 
     String result = ""; 
     PrintStream p; 
     Process p1 = Runtime.getRuntime().exec("tasklist.exe"); 
     // read from a process 
     BufferedReader input = new BufferedReader(
       new InputStreamReader(p1.getInputStream())); 
     while ((line = input.readLine()) != null) 
     { 
      //System.out.println(line); 
      output.append(line + "\n"); 
      result += line+"\n"; 
      //p.println (line); 
      //textarea.setVisible(true);  
     } 
     //msgBox(result); 
     //p.close(); 
     input.close(); 

     return result; 
    } 

    public TabbedPaneDemo() { 
     super(new GridLayout(1, 1)); 

     JTabbedPane tabbedPane = new JTabbedPane(); 
     ImageIcon icon = createImageIcon("images"); 


     JComponent panel1 = makeTextPanel("tasklist"); 
     tabbedPane.addTab("tasks", icon, panel1, 
       "ta"); 
     // add it to something! 
     panel1.add(new JScrollPane(output)); 
     tabbedPane.setMnemonicAt(0, KeyEvent.VK_1); 

     JComponent panel2 = makeTextPanel("windows"); 
     tabbedPane.addTab("wins", icon, panel2, 
       "wi"); 
     tabbedPane.setMnemonicAt(1, KeyEvent.VK_2); 


     add(tabbedPane);//`enter code here` 
     tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); 

     try { 
      String s = getDetails(); 
      output.setText(s); 
     } catch(IOException e) { 
      e.printStackTrace(); 
     } 
    } 


    public static void msgBox(String msg) { 
     javax.swing.JOptionPane.showConfirmDialog((java.awt.Component) 
       null, msg, "WindowsUtils", 
       javax.swing.JOptionPane.DEFAULT_OPTION); 
    } 


    protected JComponent makeTextPanel(String text) 
    { 
     JPanel panel = new JPanel(false); 
     JLabel filler = new JLabel(text); 
     filler.setHorizontalAlignment(JLabel.CENTER); 
     panel.setLayout(new GridLayout(1, 1)); 
     panel.add(filler); 
     return panel; 
    } 

    protected static ImageIcon createImageIcon(String path) { 
     java.net.URL imgURL = TabbedPaneDemo.class.getResource(path); 
     if (imgURL != null) { 
      return new ImageIcon(imgURL); 
     } else { 
      System.err.println("Couldn't find file: " + path); 
      return null; 
     } 
    } 
} 
+0

在評論你的[最後一個問題(http://stackoverflow.com/q/10783123/418556)我建議你這樣做從命令行1st並實現'當Runtime.exec()不會「的建議。你有一個適用於Linux的版本在命令行上工作嗎?你當前的代碼***仍然不表示你明白危險工作機智h過程。:( –

回答

6

由於您似乎忽略了我的建議,以便整理Process的創建方式以及輸出的消耗情況,我會嘗試一種不同的方法。

Java Swing在Linux上工作嗎?

是的,當然可以。這裏是你的證明(每個都附有源代碼)。

  1. Set Location by Platform Example
  2. Nested Layout Example
7

我假設由Java Swings你的意思是Java Swing。 Java是跨平臺的,所以除非你使用了一些外部庫(它有平臺特定的調用),你應該能夠在任何平臺上編譯和運行。

也就是說,根據this之前的SO帖子,ps -ef會讓你運行所有的任務,所以看起來你可能會使用錯誤的呼叫。

我假設你正試圖執行此代碼的Linux平臺具有某種GUI,而不是那些基於控制檯的發行版之一。

+0

+1用於解決OP實際上要求的問題,而不是文本中包含的唯一問題。非常直觀。 :) –

+0

@AndrewThompson:乾杯:D – npinti

+0

@Thompson:Thnx ..它也在Ubuntu上工作。我想出了錯誤,並實施了你所說的話。 –