📄 remotechatclient.java
字号:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.util.Scanner;
import java.util.StringTokenizer;
import javax.swing.*;
public class RemoteChatClient extends JFrame implements ActionListener {
JLabel label;
JButton buttonSend, buttonGet;
JPanel panel;
JTextField textField;
JTextArea textArea;
static RemoteInterface send;
static String nameClient;
RemoteChatClient() {
label = new JLabel("Message", JLabel.CENTER);
textArea = new JTextArea(5, 30);
JScrollPane scroll = new JScrollPane(textArea);
JPanel panouText = new JPanel();
panouText.add(scroll);
panouText.setBorder(BorderFactory.createEtchedBorder());
panouText.setPreferredSize(new Dimension(350, 100));
panouText.setMinimumSize(new Dimension(350, 100));
textField = new JTextField(20);
buttonSend = new JButton("Trimite");
buttonSend.addActionListener(this);
buttonGet = new JButton("Primeste");
buttonGet.addActionListener(this);
JPanel panelButoane = new JPanel();
panelButoane.add(buttonSend);
panelButoane.add(buttonGet);
JPanel GText = new JPanel();
GText.setLayout(new BorderLayout());
GText.add(panouText, BorderLayout.NORTH);
GText.add(textField, BorderLayout.CENTER);
panel = new JPanel();
panel.setLayout(new BorderLayout());
getContentPane().add(panel);
panel.add(BorderLayout.NORTH, label);
panel.add(BorderLayout.CENTER, GText);
panel.add(BorderLayout.SOUTH, panelButoane);
}
/*
* public void Text() { Scanner sent = new Scanner(System.in);
* System.out.println("Type msg"); String textSent = sent.nextLine();
* textSent }
*/
public void actionPerformed(ActionEvent event) {
if (event.getSource() instanceof JButton) {
if (event.getSource() == buttonSend) {
String textSend = textField.getText();
String mesaj = nameClient + "|" + textSend;
try {
send.sendMessage(mesaj);
} catch (java.rmi.RemoteException e) {
System.out.println("eroari" + e);
}
textArea.append(nameClient + ">" + textSend + "\n");
textField.setText(new String(""));
}
if (event.getSource() == buttonGet) {
try {
String textGet = send.getMessage();
StringTokenizer st = new StringTokenizer(textGet, "|");
String nick = st.nextToken();
String text = st.nextToken();
if (text != "")
textArea.append(nick + ">" + text + "\n");
} catch (java.rmi.RemoteException e) {
System.out.println("eroareee" + e);
}
}
}
}
public static void main(String[] args) {
RemoteChatClient frame = new RemoteChatClient();
frame.setTitle("Client Chat - ");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
try {
String nume = "rmi://127.0.0.1/ChatServer";
send = ((RemoteInterface) Naming.lookup(nume));
} catch (java.rmi.NotBoundException e) {
System.out.println("eroarem" + e);
} catch (java.rmi.RemoteException e) {
System.out.println("eroarel" + e);
} catch (java.net.MalformedURLException e) {
System.out.println("eroaren" + e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -