Итак, у меня есть класс, который называется ChatClient и работает с классом ChatServer в качестве мессенджера шифрования для нескольких чатов. Итак, у меня есть некоторые компоненты Swing, как показано ниже, и я инициализирую их в своем конструкторе ChatClient:
общедоступный статический клиент ChatClient;
// GUI ELEMENTS
BufferedReader in;
PrintWriter out;
JFrame frame;
public static JTextArea userText;
JTextArea DisplayMessage;
JLabel connectInfo;
JToggleButton tglbtnConnect;
JToggleButton tglbtnDisconnect;
JLayeredPane layeredPane;
JLayeredPane layeredPane_1;
JRadioButton rdbtnAes;
JRadioButton rdbtnDes;
JRadioButton rdbtnCbs;
JRadioButton rdbtnOfb;
JPanel panel;
JLabel lblServer;
JLabel lblText;
JLabel lblCryptText;
JTextArea encryptedText;
JToggleButton tglbtnNewToggleButton;
JToggleButton tglbtnNewToggleButton_1;
Тогда у меня есть run () метод, который обеспечивает обмен сообщениями. У меня вопрос, когда я использую основной метод, как показано ниже, чтобы я запускал метод run () в моей кнопке подключения, когда открывается рамка (изначально запрашивается имя пользователя), и я хочу, чтобы он запускал метод после кнопки подключения :
public static void main(String[] args) throws Exception {
client = new ChatClient();
client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
client.frame.setVisible(true);
}
tglbtnConnect = new JToggleButton ("Соединить");
tglbtnConnect.addActionListener (новый ActionListener () {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
client.run();
connectInfo.setText("Connected");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
tglbtnConnect.setBounds(12, 36, 106, 25);
Проблема в том, что когда я нажимаю на кнопку «Подключиться», он запрашивает имя пользователя, затем покидает графический интерфейс, чтобы я не мог использовать какой-либо компонент. Любая помощь или подсказка приветствуется. Вы можете найти весь мой класс ниже: https://pastebin.com/N7Ncz7yk
Вы можете найти мой метод запуска ниже:
private void run() throws Exception {
// Make connection and initialize streams
String serverAddress = "localhost";
Socket socket = new Socket(serverAddress, 9001);
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
// Process all messages from server, according to the protocol.
while (true) {
String line = in.readLine();
if (line.startsWith("SUBMITNAME")) {
out.println(getName());
} else if (line.startsWith("NAMEACCEPTED")) {
userText.setEditable(true);
} else if (line.startsWith("MESSAGE")) {
int idx = line.lastIndexOf(">");
String mssg = line.substring(idx + 1);
if(rdbtnAes.isSelected() && rdbtnCbs.isSelected()){
String key="MZygpewJsCpRrfOr";
byte[] encryptionKey = "MZygpewJsCpRrfOr".getBytes();
//byte[] plainText = input.getBytes();
AES advancedEncryptionStandard = new AES(encryptionKey);
byte[] cipherText = advancedEncryptionStandard.encrypt(mssg,key,0);
String decryptedCipherText = advancedEncryptionStandard.decrypt(cipherText,key,0);
String encrytepdText=new String(cipherText);
// APPEND MESSAGE
DisplayMessage.append(encrytepdText + "\n" + line.substring(8,idx + 1) + decryptedCipherText + "\n");
}
else if(rdbtnAes.isSelected() && rdbtnOfb.isSelected()){
String key="MZygpewJsCpRrfOr";
byte[] encryptionKey = "MZygpewJsCpRrfOr".getBytes();
//byte[] plainText = input.getBytes();
AES advancedEncryptionStandard = new AES(encryptionKey);
byte[] cipherText = advancedEncryptionStandard.encrypt(mssg,key,1);
String decryptedCipherText = advancedEncryptionStandard.decrypt(cipherText,key,1);
String encrytepdText=new String(cipherText);
// APPEND MESSAGE
DisplayMessage.append(encrytepdText + "\n" + line.substring(8,idx + 1) + decryptedCipherText + "\n");
}
else if(rdbtnDes.isSelected()&& rdbtnCbs.isSelected()){
DES desEncryption=new DES(mssg,0);
DisplayMessage.append(desEncryption.encryptedData + "\n" + line.substring(8,idx + 1) + desEncryption.decryptedMessage + "\n");
}
else if(rdbtnDes.isSelected() && rdbtnOfb.isSelected()){
DES desEncryption=new DES(mssg,1);
DisplayMessage.append(desEncryption.encryptedData + "\n" + line.substring(8,idx + 1) + desEncryption.decryptedMessage + "\n");
}
}
}
}