package com.lzw;
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.ScrollPaneConstants;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import com.lzw.dialog.LinkInfoDialog;
import com.lzw.rmi.RobotControlImpl;
public class MainFrame extends JFrame {
private JTextField hostField;
private JTextField nameField;
private JList linkList;
ServerSocket server = null;
JToggleButton linkButton;
private JPanel linkPanel;
private DefaultListModel listModel;
private SystemTray systemTray;
private RemoteScreenFrame remoteScreenFrame;
private JScrollPane panel = new JScrollPane();
private ImageIcon icon = new ImageIcon(getClass().getResource(
"/res/trayIcon.png"));
public JPanel getLinkPanel() {
return linkPanel;
}
/**
* Create the frame
*/
public MainFrame() {
super();
// 创建存储联系人模型数据的文件对象
File listModelFile = new File("./data/listModel.data");
if (listModelFile.exists()) {// 如果数据模型文件存在
try {
// 获取文件的输入流
FileInputStream fis = new FileInputStream(listModelFile);
// 将文件输入流转换为对象输入流
ObjectInputStream ois = new ObjectInputStream(fis);
// 从文件中读取列表组件的数据模型对象
listModel = (DefaultListModel) ois.readObject();
} catch (Exception e) {
e.printStackTrace();
}
} else {// 否则
listModel = new DefaultListModel();// 创建新的数据模型对象
}
initComponents();// 执行界面初始化
linkList.setModel(listModel);// 设置列表组件使用的数据模型
initSystemTray();
try {
// 创建远程调用的存根(也就是服务器的代理对象,用于执行本地方法)
RobotControlImpl remoteRobot_Server = new RobotControlImpl();
// 启动RMI服务器(默认端口是1099)
Registry registry = LocateRegistry.createRegistry(1098);
// 注册RMI实现类
registry.rebind("robot", remoteRobot_Server);
} catch (Exception e) {
e.printStackTrace();
}
this.setLocationRelativeTo(null);
startServer();
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* 启动服务套接字
*/
public void startServer() {
try {
server = new ServerSocket(9527); // 创建服务器套接字
new Thread(new Runnable() {
public void run() {
panel
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
panel
.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
while (!server.isClosed()) {
try {
// 接受远程计算机的连接请求
final Socket socket = server.accept();
if (socket.isConnected()) {
// 获取对方计算机的主机名称
String name = socket.getInetAddress()
.getHostName();
// 显示提示信息
int i = JOptionPane.showConfirmDialog(
MainFrame.this, "收到" + name
+ "的求助请求,是否开始协助?");
if (i == JOptionPane.NO_OPTION) {
socket.close();
continue;
}
setExtendedState(JFrame.NORMAL);
// 创建远程屏幕监控面板
remoteScreenFrame = new RemoteScreenFrame(
MainFrame.this, panel, socket);
panel.setViewportView(remoteScreenFrame);
new Thread(remoteScreenFrame).start();
// 创建右下角的X断开按钮
JButton button = new JButton("X");
button.setMargin(new Insets(0, 0, 0, 0));
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
socket.close();
panel.setVisible(false);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
panel.setCorner(
ScrollPaneConstants.LOWER_RIGHT_CORNER,
button);
// 隐藏远程连接面板
linkPanel.setVisible(false);
// 显示远程监控面板
panel.setVisible(true);
add(panel, BorderLayout.CENTER);
validate();
}
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(MainFrame.this, e);
}
}
}
}).start();
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(MainFrame.this, e);
JOptionPane.showMessageDialog(MainFrame.this, "本软件已经启动。");
System.exit(0);
}
}
/**
* 初始化系统托盘的方法
*/
private void initSystemTray() {
if (SystemTray.isSupported())
systemTray = SystemTray.getSystemTray();
TrayIcon trayIcon = new TrayIcon(icon.getImage());
trayIcon.setImageAutoSize(true);
URL url = getClass().getResource("/res/1.jpg");
PopupMenu popupMenu = new PopupMenu("托盘菜单");
// 创建显示主窗体菜单项
MenuItem showMenuItem = new MenuItem("显示主窗体");
showMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MainFrame.this.setExtendedState(JFrame.NORMAL);
MainFrame.this.setVisible(true);
}
});
// 创建退出菜单项
MenuItem exitMenuItem = new MenuItem("退出");
exitMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
popupMenu.add(showMenuItem);
popupMenu.addSeparator();
popupMenu.add(exitMenuItem);
trayIcon.setPopupMenu(popupMenu);
try {
systemTray.add(trayIcon);
} catch (AWTException e) {
e.printStackTrace();
}
}
/**
* 初始化界面的方法
*/
private void initComponents() {
setIconImage(icon.getImage());
getContentPane().setLayout(new BorderLayout());
setTitle("明日科技——远程协助模块(局域网版本)");
addWindowListener(new ThisWindowListener());
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
setSize(476, 301);
final JPanel panel = new JPanel();
panel.setBorder(new LineBorder(Color.GREEN, 1, false));
panel.setLayout(new BorderLayout());
panel.setPreferredSize(new Dimension(180, 0));
getContentPane().add(panel, BorderLayout.WEST);
final JScrollPane scrollPane = new JScrollPane();
panel.add(scrollPane);
linkList = new JList();
linkList.addListSelectionListener(new LinkListListSelectionListener());
linkList.setBorder(new BevelBorder(B
评论2