2016-04-14 83 views
1

我想确保我所选择的组合框将是我要去尝试做问题与JComboBox中

,如果我硬编码字符串主机连接工作完全正常的连接过程中的主机名,但我想使用droplist确定,我想连接到

这里的主人是我有

import javax.swing.*; 
import java.awt.event.*; 

import com.jcraft.jsch.Channel; 
import com.jcraft.jsch.ChannelExec; 
import com.jcraft.jsch.ChannelSftp; 
import com.jcraft.jsch.JSch; 
import com.jcraft.jsch.Session; 
import java.io.*; 
import java.util.EmptyStackException; 


@SuppressWarnings("serial") 
public class welcomewindow extends JFrame { 

public static void main(String[] args) { 

@SuppressWarnings("unused") 

welcomewindow frameTabel = new welcomewindow(); 


} 
String host; 
JButton blogin = new JButton("Login"); 


JLabel UIusername = new JLabel("Username",SwingConstants.LEFT); 
JLabel Jumpbox = new JLabel("Choose your Jumpbox",SwingConstants.LEFT); 
JLabel UIpassword = new JLabel("Password",SwingConstants.LEFT); 
JTextField txuser = new JTextField(15); 
//JComboBox ComboBox = new JComboBox(); 
@SuppressWarnings({ }) 
String[] J1J2 = {"ServerA", "ServerB"}; 
@SuppressWarnings({ "unchecked", "rawtypes" }) 
JComboBox JumpList= new JComboBox(J1J2); 
JPasswordField pass = new JPasswordField(15); 




@SuppressWarnings({ "rawtypes", "unchecked" }) 
welcomewindow(){ 
super("Login Authorization to Tracer"); 
setSize(300,150); 
setLocation(500,280); 
//panel.setLayout (null); 
String[] J1J2 = {"ServerA", "ServerB"}; 
JComboBox JumpList= new JComboBox(J1J2); 
UIusername.setVerticalAlignment(SwingConstants.CENTER); 
UIpassword.setVerticalAlignment(SwingConstants.CENTER); 
Jumpbox.setVerticalAlignment(SwingConstants.CENTER); 
JLabel UIusername = new JLabel("Username",SwingConstants.LEFT); 
JLabel UIpassword = new JLabel("Password",SwingConstants.LEFT); 
JLabel Jumpbox = new JLabel("Choose Your Jumpbox",SwingConstants.LEFT); 
blogin.setVerticalAlignment(SwingConstants.CENTER); 
JumpList.setSelectedIndex(0); 

JPanel panel = new JPanel(); 
panel.add(Jumpbox); 
panel.add(JumpList); 
panel.add(UIusername); 
panel.add(txuser); 
panel.add(UIpassword); 
panel.add(pass); 
panel.add(blogin); 
getContentPane().add(panel); 
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
setVisible(true); 
panel.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0),"EnterButton1"); 

panel.getRootPane().getActionMap().put("EnterButton1",new AbstractAction(){ 
     public void actionPerformed(ActionEvent ae) 
     { 
      String puname = txuser.getText(); 
      @SuppressWarnings("deprecation") 
      String ppaswd = pass.getText(); 
      if(puname.isEmpty() || ppaswd.isEmpty()){ 
      JOptionPane.showMessageDialog(null,"Username Or Password Field is empty"); 
      } 
      else{ 
       blogin.doClick(); 
       } 
      } 
    }); 
actionlogin(); 
} 



public void actionlogin(){ 
blogin.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
    String puname1 = txuser.getText(); 
    @SuppressWarnings("deprecation") 
    String ppaswd1 = pass.getText(); 
    String host = (String) JumpList.getSelectedItem(); 
    if(puname1.isEmpty() || ppaswd1.isEmpty()){ 
     JOptionPane.showMessageDialog(null,"Username Or Password Field is empty"); 
    } 
    else{ 
String puname = txuser.getText(); 
@SuppressWarnings("deprecation") 
String ppaswd = pass.getText(); 
String command1="pwd"; 
int port=22; 
String remoteFile="/home/bsoghmonian/test.txt"; 
System.out.println(host); 
boolean sshconnected = false; 
try 
{ 
System.out.println(host); 
JSch jsch = new JSch(); 
Session session = jsch.getSession(puname, host, port); 
    session.setPassword(ppaswd); 
    session.setConfig("StrictHostKeyChecking", "no"); 
System.out.println("Establishing Connection..."); 
session.connect(); 
    System.out.println("Connection established."); 
System.out.println("Crating SFTP Channel."); 
ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp"); 
sftpChannel.connect(); 
sshconnected = true; 
System.out.println("SFTP Channel created."); 
if(sshconnected){ 
instancewindow instancewelcome =new instancewindow(); 
instancewelcome.setVisible(true); 
dispose(); 
}else{ 
    throw new EmptyStackException(); 
} 

代码.............直到节目结束

我在这里的问题是即使我选择服务器B用户尝试连接到ServerA我想如果用户选择ServerB应用程序尝试将用户连接到ServerB并选择ServerA时,然后连接到ServerA

回答

0

您定义相同的JComboBox两次 - 您在welcomewindow()之内操作(更改值)在本地一个,但是然后您从未触及的全局一个中取出JumpList.getSelectedItem(),因此它始终是第一个选项;)

+0

你完美:)你是最好的 – berjberj1