大家好,我正在开发程序,在该程序中我必须执行某个任务,之后我也可以关闭窗口..显然但它没有关闭窗口......我的主要课程就像
public class laudit {
public static void main(String[] arg){
SwingUtilities.invokeLater( new Runnable(){
public void run(){
JFrame frame = new mainFrame("Linux Audit");
frame.setVisible(true);
frame.setSize(700,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}
带面板的第二类是...在其中我开始一个 ssh 连接但它并没有像完成后应该停止的那样停止..
public class panel extends JPanel {
String shost;
String suser;
String spass;
int sport;
public int getsport() {
return this.sport;
}
public String getshost() {
return this.shost;
}
public String getsuser() {
return this.suser;
}
public String getspass() {
return this.spass;
}
public panel(){
Dimension size = getPreferredSize();
size.width = 680;
size.height = 600;
setPreferredSize(size);
setBorder(BorderFactory.createTitledBorder("Linux Audit"));
setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
JLabel labelhost = new JLabel("Host ");
JLabel labeluser = new JLabel("User name ");
JLabel labelpass = new JLabel("Password ");
JLabel labelport = new JLabel("Port ");
final JTextField host = new JTextField(15);
final JTextField user = new JTextField(15);
final JTextField pass=(JTextField)new JPasswordField(15);
final JTextField port = new JTextField(15);
final JButton start = new JButton("Start Audit");
//layout design
gc.anchor = GridBagConstraints.LINE_END;
gc.weightx = 0.5;
gc.weighty = 0.5;
gc.gridx=0;
gc.gridy=0;
add(labelhost,gc);
gc.gridx=0;
gc.gridy=1;
add(labeluser,gc);
gc.gridx=0;
gc.gridy=2;
add(labelpass,gc);
gc.gridx=0;
gc.gridy=3;
add(labelport,gc);
gc.anchor = GridBagConstraints.LINE_START;
gc.gridx=1;
gc.gridy=0;
add(host,gc);
gc.gridx=1;
gc.gridy=1;
add(user,gc);
gc.gridx=1;
gc.gridy=2;
add(pass,gc);
gc.gridx=1;
gc.gridy=3;
add(port,gc);
gc.anchor = GridBagConstraints.FIRST_LINE_START;
gc.weighty=10;
gc.gridx=1;
gc.gridy=4;
add(start,gc);
start.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String shost = host.getText();
String suser = user.getText();
String spass = pass.getText();
String sportb = port.getText();
int sport = Integer.parseInt(sportb);
sshConnection s = new sshConnection();
try {
s.Connection(shost,suser,spass,sport);
} catch (JSchException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
这是 ssh 连接我对 java 编程很陌生
public class sshConnection {
public void Connection (String sHost,String sUser,String sPass,int sPort)throws JSchException, IOException{
String sshhost = sHost;
String sshuser = sUser;
String sshpass = sPass;
int sshport = sPort;
/*System.out.println(sshhost);
System.out.println(sshuser);
System.out.println(sshport);
System.out.println(sshpass);*/
String endLineStr = " # ";
JSch shell = new JSch();
// get a new session
Session session = shell.getSession(sshuser, sshhost, sshport);
// set user password and connect to a channel
session.setUserInfo(new SSHUserInfo(sshpass));
session.connect();
Channel channel = session.openChannel("shell");
channel.connect();
DataInputStream dataIn = new DataInputStream(channel.getInputStream());
DataOutputStream dataOut = new DataOutputStream(channel.getOutputStream());
//file start
File f = new File("Result.txt");
if(!f.exists())
{
try {
f.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
}
try {
FileOutputStream fos = new FileOutputStream(f);
PrintStream ps = new PrintStream(fos);
System.setOut(ps);
} catch (Exception e) {
e.printStackTrace();
} //file end
// send ls command to the server
dataOut.writeBytes("ls -la\r\n");
dataOut.flush();
// and print the response
String line = dataIn.readLine();
System.out.println(line);
while(!line.endsWith(endLineStr)) {
System.out.println(line);
line = dataIn.readLine();
}
dataIn.close();
dataOut.close();
channel.disconnect();
session.disconnect();
} }
class SSHUserInfo implements UserInfo {
private String sshpass;
SSHUserInfo(String sshpass) {
this.sshpass = sshpass;
}
public String getPassphrase() {
return null;
}
public String getPassword() {
return sshpass;
}
public boolean promptPassword(String arg0) {
return true;
}
public boolean promptPassphrase(String arg0) {
return true;
}
public boolean promptYesNo(String arg0) {
return true;
}
public void showMessage(String arg0) {
System.out.println(arg0);
}
}
请帮助我一切正常,除了这个缺陷......谢谢..