String JComboBox的问题

String JComboBox的问题,string,login,ssh,jcombobox,String,Login,Ssh,Jcombobox,我试图确保在连接过程中,我在ComboBox上选择的是主机名 如果我硬编码字符串主机连接工作得很好,但是我想使用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.

我试图确保在连接过程中,我在ComboBox上选择的是主机名

如果我硬编码字符串主机连接工作得很好,但是我想使用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,用户也会尝试连接到服务器a我希望如果用户选择了服务器B,应用程序会尝试将用户连接到服务器B,当选择了服务器a,然后连接到服务器a

您定义了相同的JComboBox两次-您在welcomewindow()中对本地JComboBox进行操作(更改值),但是,您可以从未触及的全局JumpList.getSelectedItem()中获取JumpList.getSelectedItem(),因此它始终是第一个选项;)

您定义了两次相同的JComboBox—您在welcomewindow()中对本地JComboBox进行操作(更改值),但随后从未触及的全局JComboBox中获取JumpList.getSelectedItem(),因此它始终是第一个选项;)