2016-04-14 88 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