📄 receivenewmsg.java
字号:
/**
* @(#)frm.ReceiveNewMSG.java 2008-10-9
* Copy Right Information : Tarena
* Project : IPMsg
* JDK version used : jdk1.6.4
* Comments : 收到消息窗口。
* Version : 1.0
* Sr Date Modified By Why & What is modified
* 1. 2008-10-9 小猪 新建
**/
package frm;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import logic.ClientTCPThread;
import data.FileData;
/**
* 收到消息窗口。
* 2008-10-9
* @author 达内科技[Tarena Training Group]
* @version 1.0
* @since JDK1.6(建议)
*/
@SuppressWarnings("serial")
public class ReceiveNewMSG extends JFrame implements ActionListener{
/** 消息的来源 */
private JLabel lblFrom = new JLabel("Administrator (Tarena-ZHULF)");
/** 消息的时间 */
private JLabel lblTime = new JLabel("时间: 2008-10-09 星期四 13:53:51");
/** 接受按钮 */
private JButton btnRecevie = new JButton();
/** 消息内容 */
private JTextArea areaContent = new JTextArea();
/** 关闭按钮 */
private JButton btnClose = new JButton("关闭(C)");
/** 回复按钮 */
private JButton btnReply = new JButton("回复(R)");
/** 是否引用原文 */
private JCheckBox boxQuote = new JCheckBox("引用原文(Q)");
/** 显示消息内容和文件的JPanel */
private JPanel paneContent = new JPanel();
/** 传送文件的列表 */
private Vector<FileData> list = new Vector<FileData>();
/** 接受ip */
private String ip = "";
/** 服务器端口 */
private int port = 0;
/** 接受文件线程 */
private ClientTCPThread clientTCPThread = null;
public ReceiveNewMSG(String time,String from,String path,String content,String ip,int port) {
this.ip = ip;
this.port = port;
setTitle("收到消息 +");
setSize(400,300);
setResizable(false);
Toolkit tk=Toolkit.getDefaultToolkit();
setLocation((tk.getScreenSize().width-getSize().width)/2,(tk.getScreenSize().height-getSize().height)/2);
try {
setIconImage(ImageIO.read(getClass().getResource("/frm/img/ipmsg.gif")));
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
lblFrom.setText(from);
lblTime.setText(time);
areaContent.append(content+"\n");
init();
System.out.println(path);
initSendFile(path);
addWindowListener(new MyWindowAdapter());
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
}
/**
* 初始化面板。
*/
private void init(){
btnReply.setPreferredSize(new Dimension(80,25));
btnReply.setMargin(new Insets(0,0,0,0));
btnReply.addActionListener(this);
btnReply.setMnemonic(KeyEvent.VK_R);
btnClose.setPreferredSize(new Dimension(80,25));
btnClose.setMargin(new Insets(0,0,0,0));
btnClose.addActionListener(this);
btnClose.setMnemonic(KeyEvent.VK_C);
boxQuote.setMnemonic(KeyEvent.VK_Q);
boxQuote.addActionListener(this);
btnRecevie.setMargin(new Insets(0,0,0,0));
btnRecevie.setFocusPainted(false);
btnRecevie.setContentAreaFilled(false);
btnRecevie.addActionListener(this);
lblFrom.setPreferredSize(new Dimension(300,20));
lblFrom.setHorizontalAlignment(SwingConstants.CENTER);
lblTime.setPreferredSize(new Dimension(300,20));
lblTime.setHorizontalAlignment(SwingConstants.CENTER);
areaContent.setEditable(false);
areaContent.setBackground(getBackground());
paneContent.setLayout(new BorderLayout(8,8));
//paneContent.add(btnRecevie,BorderLayout.NORTH);
paneContent.add(new JScrollPane(areaContent));
JPanel pane = new JPanel();
pane.setPreferredSize(new Dimension(400,70));
pane.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
pane.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED),"消息来自..."));
pane.add(lblFrom);
pane.add(lblTime);
JPanel paneTop = new JPanel();
paneTop.setLayout(new BorderLayout());
paneTop.add(new FillWidth(8,8),BorderLayout.EAST);
paneTop.add(new FillWidth(8,8),BorderLayout.WEST);
paneTop.add(new FillWidth(8,5),BorderLayout.NORTH);
paneTop.add(new FillWidth(8,8),BorderLayout.SOUTH);
paneTop.add(pane);
JPanel paneBottom = new JPanel();
paneBottom.setPreferredSize(new Dimension(400,40));
paneBottom.setLayout(new FlowLayout(FlowLayout.RIGHT,15,8));
paneBottom.add(btnClose);
paneBottom.add(btnReply);
paneBottom.add(boxQuote);
add(new FillWidth(8,8),BorderLayout.EAST);
add(new FillWidth(8,8),BorderLayout.WEST);
add(paneTop,BorderLayout.NORTH);
add(paneBottom,BorderLayout.SOUTH);
add(paneContent);
}
/**
* 初始化接受文件的列表
* @param path
*/
private void initSendFile(String path){
StringTokenizer tokenizer = new StringTokenizer(path,"|");
while(tokenizer.hasMoreTokens()){
String xpath = tokenizer.nextToken();
FileData data = FileData.stringToFileData(xpath);
if(data!=null)
list.add(data);
}
initBtnList();
}
/**
* 窗体关闭时触发事件。
*/
private class MyWindowAdapter extends WindowAdapter{
public void windowClosing(WindowEvent e) {
//super.windowClosing(e);
if(sureStopSend())
dispose();
}
}
/**
* 初始化接受文件内容按钮。
*/
public void initBtnList(){
String str = "";
for(FileData data:list)
str += data.getFileName()+" ";
btnRecevie.setText(str);
if(!btnRecevie.getText().equals(""))
paneContent.add(btnRecevie,BorderLayout.NORTH);
if(btnRecevie.getText().equals("") && paneContent.isAncestorOf(btnRecevie)){
paneContent.remove(btnRecevie);
paneContent.validate();
paneContent.repaint();
}
}
/**
* 按钮点击事件。
*/
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnClose){
dispose();
return;
}
if(e.getSource()==btnReply){
dispose();
return;
}
if(e.getSource()==btnRecevie){
if(clientTCPThread==null && list.size()>0){
FileData data = list.get(0);
list.remove(0);
DirectoryChoose choose = new DirectoryChoose(this,"选择保存目录");
File file = choose.getSelectedFile();
if(file!=null && file.exists() && file.canRead()){
clientTCPThread = new ClientTCPThread(this,ip,port,data.getFilePath(),file.getAbsolutePath());
clientTCPThread.start();
}else
initBtnList();
}else if(clientTCPThread!=null && clientTCPThread.isRun()){
sureStopSend();
}
}
}
public void setSpeed(String speed){
btnRecevie.setText(speed);
}
/**
* 确认终止当前传送
* @return true:终止;false:反之。
*/
private boolean sureStopSend(){
if(clientTCPThread!=null && clientTCPThread.isRun()){
clientTCPThread.pauseSend();
int n = JOptionPane.showConfirmDialog(null, "是否要终止传送","提示",JOptionPane.OK_CANCEL_OPTION);
if(n == JOptionPane.OK_OPTION){
clientTCPThread.stopSend();
clientTCPThread = null;
return true;
}
else{
clientTCPThread.continueSend();
return false;
}
}else
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -