2013-07-05 23 views
0

我的代碼存在一些問題,如果您可以請(需要解釋一下,這樣我可以在將來了解:)),所以我需要一些幫助,所以這是我的代碼我需要的是我的JButton執行關機命令,而關機命令從我在JTextfield中輸入的秒數延遲。 所以到目前爲止我的代碼是:將JTextField擴展到InputStream並從Button中執行操作

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JTextField; 


public class Shutdown extends JFrame{ 
    InputStream text1; 
    JButton start; 
    String shutdownCmd; 

     public Shutdown() { 

     this.setTitle("Shutdown When you want"); 
     setSize(300, 150); 
     setResizable(false); 
     setLocation(370, 150); 
     setLayout(null); 

     JLabel desc1 = new JLabel("Time until shutdown : "); 
     desc1.setBounds(95, 25, 125, 25); 
     add(desc1); 

     JTextField text1 = new JTextField(); 
     text1.setBounds(95, 45, 120, 25); 
     text1.setForeground(Color.BLACK); 
     text1.setToolTipText("Introdu textu aici"); 
     add(text1); 

     JButton start = new JButton("Start Shudown"); 
     start.setBounds(95, 75, 120, 25); 
     add(start); 



     ActionListener eventstart = new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent arg0) { 
       // TODO auto- generated method 
       String actionstart = arg0.getActionCommand(); 
       if(actionstart.equals("Start Shudown")){ 
        try { 
         ShutdownCmd(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 

       } 

     }; 
     start.addActionListener(eventstart); 
}  
     public void ShutdownCmd() throws IOException{ 
    Runtime runtime = Runtime.getRuntime(); 
    BufferedReader br=new BufferedReader(new InputStreamReader(text1)); 
    long a=Long.parseLong(br.readLine()); 
    Process proc = runtime.exec("shutdown -s -t "+a); 
    System.exit(0); 
} 
} 

謝謝你或幫助高級! :d

回答

1

許多事情在我跳出來這裏,可是......

重新聲明text1JTextField,而不是InputStream ...

//InputStream text1; 
private JTextField text1; 

這將允許您訪問的領域,這是來自課堂上任何地方的價值。

確保當您創建文本字段,你不遮蔽變量...

//JTextField text1 = new JTextField(); 
text1 = new JTextField(10); 

利用ProcessBuilder代替Runtime.getRuntime()。它將使你的生活更容易涉及參數好得多

ProcessBuilder pb = new ProcessBuilder("shutdown", "-s", "-t", text1.getText()); 
pb.redirectError(); 
Process p = pb.start(); 

的動作指令總是會null因爲你從來沒有設置,所以下面將導致你一個NullPointerException

String actionstart = arg0.getActionCommand(); 
if(actionstart.equals("Start Shudown")){ 

當您創建按鈕,你需要設置的動作命令

JButton start = new JButton("Start Shudown"); 
start.setActionCommand("Start Shudown"); 

其他建議...

  • 使用適當的佈局管理器。即使在同一個操作系統上,您的應用程序也可能需要處理不同的屏幕分辨率,DPI,字體等。
  • 避免直接從頂級容器如JFrame進行擴展。相反,基於你的應用程序如JPanel。它使您的應用程序具有靈活性和可重用性。
0

所有你需要做的是讓JTextField的一個領域:

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JTextField; 


public class Shutdown extends JFrame{ 
    JTextField text1; 
    JButton start; 
    String shutdownCmd; 

     public Shutdown() { 

     this.setTitle("Shutdown When you want"); 
     setSize(300, 150); 
     setResizable(false); 
     setLocation(370, 150); 
     setLayout(null); 

     JLabel desc1 = new JLabel("Time until shutdown : "); 
     desc1.setBounds(95, 25, 125, 25); 
     add(desc1); 

     text1 = new JTextField(); 
     text1.setBounds(95, 45, 120, 25); 
     text1.setForeground(Color.BLACK); 
     text1.setToolTipText("Introdu textu aici"); 
     add(text1); 

     JButton start = new JButton("Start Shudown"); 
     start.setBounds(95, 75, 120, 25); 
     add(start); 



     ActionListener eventstart = new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent arg0) { 
       // TODO auto- generated method 
       String actionstart = arg0.getActionCommand(); 
       if(actionstart.equals("Start Shudown")){ 
        try { 
         ShutdownCmd(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 

       } 

     }; 
     start.addActionListener(eventstart); 
}  
     public void ShutdownCmd() throws IOException{ 
      Runtime runtime = Runtime.getRuntime(); 

      long a=Long.parseLong(text1.getText()); 
      Process proc = runtime.exec("shutdown -s -t "+a); 
      System.exit(0); 
     } 
} 

如果它是全球性的,你可以用它在任何的這個對象的功能,讓你可以從文本框的文本你想在你的JFrame對象中的任何地方。

我希望這是你想要做的。如果解釋不夠好,請告訴我。 ;)

+1

_如果沒有足夠的解釋,告訴我._在這個例子中,術語_global_是不明確的; 'text1'在成員級別具有[_package-private_](http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html)訪問權限。拼出單詞_you_可能更易讀。 – trashgod