JComboBox问题

时间:2016-04-14 20:14:19

标签: string login ssh jcombobox username

我正在努力确保我在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用户尝试连接到ServerA,我想如果用户选择ServerB,则应用程序尝试将用户连接到ServerB,然后选择ServerA然后连接到ServerA

1 个答案:

答案 0 :(得分:0)

您定义了相同的JComboBox两次 - 您在 welcomewindow()中的本地操作(更改值),但是您从未触及的全局一个中获取JumpList.getSelectedItem(),因此它& #39; s总是第一个选项;)