⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tnt.java

📁 java开发的桌面应用程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

			}

			public void sendFile() {
				int[] index = friends.getSelectedIndices();
				JFileChooser jfc = new JFileChooser();
				FileSend fs = new FileSend();
				int determine = jfc.showOpenDialog(null);
				if (determine == JFileChooser.APPROVE_OPTION) {
					for (int i = 0; i < index.length; i++) {
						NameIpBean nib = (NameIpBean) model
								.getElementAt(index[i]);
						/**
						 * 摦懺暘攝丂port,埲屻幚尰
						 */
						/*fs.sendTo(nib.getIp(),
								jfc.getSelectedFile().toString(), fileSendPort);*/
						fs.sendTo(nib.getIp(),
								jfc.getSelectedFile().toString(), 6001);
						
					}
				}
			}
		});
		// hot key ctrl+enter
		itemSend.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
				ActionEvent.CTRL_MASK));
		itemSend.addActionListener(this);
		itemSendAll.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// send message to all of your friends
				sendBroadcastMsg(3);
			}
		});

		itemName.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new ChangeNameDial().show();
			}
		});
		itemRefresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendBroadcastMsg(1);
			}
		});
		// remote connect
		itemRemoteDesk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int[] index = friends.getSelectedIndices();
				if (index.length > 0) {
					for (int i = 0; i < index.length; i++) {
						NameIpBean nib = (NameIpBean) model
								.getElementAt(index[i]);
						/**
						 * 摦懺暘攝丂port,埲屻幚尰
						 */
						/*new RemoteControlClient().client(nib.getIp(),
								remotePort);*/
						new RemoteControlClient().client(nib.getIp(),
								6000);
					}
				} else {
					JOptionPane.showMessageDialog(null,
							"please select friends", "no friends selected!",
							JOptionPane.WARNING_MESSAGE);
				}

			}
		});

		file.add(itemOpenFile);
		file.add(itemFileTrans);
		tool.add(itemSend);
		tool.add(itemSendAll);
		tool.add(itemName);
		tool.add(itemRefresh);
		tool.add(itemRemoteDesk);
		JMenuBar menubar = new JMenuBar();
		menubar.add(file);
		menubar.add(tool);
		menubar
				.setBounds(heightborder, 0, width - 3 * heightborder,
						heightComp);
		panelMenuBlock.add(menubar);
		panelMenuBlock.setLayout(null);
		panelMenuBlock.setBounds(0, 0, width, heightComp);

		// quick panel
		// JPanel panelQuickBar = new JPanel();
		// Myname
		JLabel labelName = new JLabel("MyName :");
		labelName.setBounds(width - 135, heightComp + heightborder, 105,
				heightComp);
		myName = new JLabel();
		myName.setBounds(width - 125, heightComp + 2 * heightborder, 105,
				2 * heightComp);

		// right panel
		panelRightBlock.setLayout(new BorderLayout());
		panelRightBlock.setBounds(width - 135, heightComp + 6 * heightborder
				+ 10, 125, height - 8 * heightComp);
		/*
		 * DefaultMutableTreeNode leaf = new DefaultMutableTreeNode("tree
		 * node1"); DefaultMutableTreeNode leaf11 = new
		 * DefaultMutableTreeNode("tree node11"); DefaultMutableTreeNode leaf12 =
		 * new DefaultMutableTreeNode("tree node12"); DefaultMutableTreeNode
		 * leaf13 = new DefaultMutableTreeNode("tree node13");
		 * leaf11.add(leaf12);leaf.add(leaf11);leaf.add(leaf13); JTree tree=new
		 * JTree(leaf); tree.setBounds(width-80,heightComp+25, 50, 100);
		 * JScrollPane jsp1=new JScrollPane(tree);
		 * jsp1.setBounds(width-80,heightComp+25, 50, 100);
		 * panelRightBlock.add(jsp1,BorderLayout.NORTH);
		 */
		// read name from Myname.ini
		File nameFile = new File(chatName);
		// judge the file exist or not
		if (!nameFile.exists()) {
			// create a new namefile
			nameFile.createNewFile();
		}
		FileReader fr = new FileReader(nameFile);
		BufferedReader br = new BufferedReader(fr);
		String localName = br.readLine();
		NameIpBean nib = new NameIpBean();
		if (localName == null) {
			nib.setName("Set your name!");
			myName.setText("Set your name!");
		} else {
			nib.setName(localName);
			myName.setText(localName);
		}
		nib.setIp(InetAddress.getLocalHost().getHostAddress());
		model.addElement(nib);
		friends.setModel(model);
		friends.setBorder(BorderFactory.createTitledBorder("friends"));
		panelRightBlock.add(new JScrollPane(friends));

		// left panel
		JPanel panelLeftBlock = new JPanel();
		panelLeftBlock.setLayout(null);
		panelLeftBlock.setBounds(heightborder, heightborder, width - 135 - 2
				* heightborder, height - 2 * heightComp - heightborder);
		// display message area
		areaDisplayMsg = new JTextArea(1, 1);
		// auto wrap
		areaDisplayMsg.setLineWrap(true);
		areaDisplayMsg.setEditable(false);
		// sroll pane of display message area
		JScrollPane scrollDisp = new JScrollPane(areaDisplayMsg);
		scrollDisp
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		scrollDisp
				.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scrollDisp.setBounds(heightborder, heightComp + heightborder, width
				- 135 - 3 * heightborder, 240);
		// send message area
		areaSendMsg = new JTextArea(1, 1);
		// auto wrap
		areaSendMsg.setLineWrap(true);
		// sroll pane of send message area
		JScrollPane scrollSend = new JScrollPane(areaSendMsg);
		scrollSend
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		scrollSend
				.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scrollSend.setBounds(heightborder, 275, width - 135 - 3 * heightborder,
				90);
		// buttons
		JButton btnHistory = new JButton("History"), btnClose = new JButton(
				"Close"), btnSend = new JButton("Send");
		// use udp send message
		btnSend.addActionListener(this);

		// close tnt window
		btnClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					File recordsFile = new File(recordsPath);
					if (!recordsFile.exists()) {
						recordsFile.createNewFile();
					}
					RandomAccessFile raf = new RandomAccessFile(recordsFile,
							"rw");
					raf.skipBytes((int) raf.length());
					raf.write(areaDisplayMsg.getText().getBytes());
					raf.close();

					// tell friends i am leaving
					sendBroadcastMsg(0);
				} catch (Exception ex) {
					ex.getMessage();
				}
				System.exit(0);
			}
		});
		btnHistory.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 屻偱
			}
		});
		btnHistory.setBounds(5, (height - 77), 80, heightComp);
		btnClose.setBounds(width - 135 - 70 - 70 - 20, (height - 77), 70,
				heightComp);
		btnSend.setBounds(width - 135 - 70 - 10, (height - 77), 70, heightComp);
		// add compents
		panelLeftBlock.add(scrollDisp);
		panelLeftBlock.add(scrollSend);
		panelLeftBlock.add(btnHistory);
		panelLeftBlock.add(btnClose);
		panelLeftBlock.add(btnSend);
		// open file
		/*
		 * itemOpenFile.addActionListener(new ActionListener() { public void
		 * actionPerformed(ActionEvent e) { JFileChooser jfc = new
		 * JFileChooser(); int rtn = jfc.showOpenDialog(TnT.this); if (rtn ==
		 * JFileChooser.APPROVE_OPTION) {
		 * jtf.setText(jfc.getSelectedFile().toString()); } else if (rtn ==
		 * JFileChooser.CANCEL_OPTION) { jtf.setText(""); } } });
		 */

		/*
		 * // save itemSaveFile.addActionListener(new ActionListener() { public
		 * void actionPerformed(ActionEvent e) { jtf.setText(""); JFileChooser
		 * jfc = new JFileChooser(); int rtn = jfc.showSaveDialog(TnT.this); if
		 * (rtn == JFileChooser.APPROVE_OPTION) { String path =
		 * jfc.getSelectedFile().toString(); try { if (text.getText() != null &&
		 * "".equals(text.getText()) == false) { BufferedWriter pw = new
		 * BufferedWriter( new FileWriter(path));
		 * 
		 * pw.write(text.getText()); pw.close(); new ShowDialog("1",
		 * null).show(); } else { new ShowDialog("0", null).show(); }
		 *  } catch (Exception ex) {
		 *  } } else if (rtn == JFileChooser.CANCEL_OPTION) { jtf.setText("");
		 *  } } });
		 */

		// btn1.addActionListener(btnL);
		/*
		 * btn2.addActionListener(new ActionListener() { public void
		 * actionPerformed(ActionEvent e) { text.setText(""); try { Method[] me =
		 * Class.forName(jtf.getText()).getMethods(); for (int i = 0; i <
		 * me.length; i++) { String name = me[i].toString(); if
		 * (name.indexOf("Listener") != -1 || name.indexOf("Event") != -1) {
		 * text.append(me[i] + "\n"); } } } catch (Exception es) {
		 *  } } });
		 */
		// add compents
		Container c = frame.getContentPane();
		c.setLayout(null);
		c.add(panelMenuBlock);
		// c.add(panelQuickBar);
		c.add(labelName);
		c.add(myName);
		c.add(panelRightBlock);
		c.add(panelLeftBlock);
		// setVisible 儊僜僢僪偼偤傂嵟屻偱
		frame.setVisible(true);

	}

	// add friends list
	public void reflushFriends(String flg) {
		if ("0".equals(flg)) {
			int index = -1;
			NameIpBean element;
			for (int i = 0; i < model.size(); i++) {
				element = (NameIpBean) model.getElementAt(i);
				if (friendsName.equals(element.getName())
						|| friendsIp.equals(element.getIp())) {
					index = model.indexOf(element);
				}
			}
			if (index != -1) {
				model.removeElementAt(index);
				friendsName = null;
				friendsIp = null;
			}
		} else if ("1".equals(flg)) {
			NameIpBean nib = new NameIpBean();
			nib.setName(friendsName);
			nib.setIp(friendsIp);
			model.addElement(nib);
			friendsName = null;
			friendsIp = null;
		}
		// reflush the friends list
		friends.repaint();

	}

	class ChangeNameDial extends JDialog {
		JTextField jtfName = null;

		public ChangeNameDial() {
			super(frame, "Set Name", false);
			this.setBounds(350, 400, 300, 150);
			Container diaC = this.getContentPane();
			JButton jbSet = new JButton("Set");
			JButton jbCancel = new JButton("Cancel");
			jbSet.setBounds(140, 85, 55, 20);
			jbCancel.setBounds(200, 85, 75, 20);
			jtfName = new JTextField(20);
			jtfName.setBounds(60, 25, 180, 20);
			// set name
			jbSet.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					myName.setText(jtfName.getText());
					NameIpBean nib = (NameIpBean) model.get(0);
					nib.setName(myName.getText());
					try {
						BufferedWriter bw = new BufferedWriter(new FileWriter(
								chatName));
						bw.write(jtfName.getText());
						bw.flush();
						bw.close();
					} catch (Exception ex) {

					}
					// reflush the friends list
					friends.repaint();
					dispose();
				}
			});

			// canceled
			jbCancel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					dispose();
				}
			});
			diaC.setLayout(null);
			diaC.add(jtfName);
			diaC.add(jbSet);
			diaC.add(jbCancel);
			this.setVisible(true);
		}
	}

	public static void main(String[] args) {
		try {
			Tnt myTnt = new Tnt();
			myTnt.run();
			// tell frinds i am coming
			myTnt.sendBroadcastMsg(1);
		} catch (Exception e) {
			e.getMessage();
		}

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -