📄 filedisposebutton.java~153~
字号:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class FileDisposeButton extends JPanel implements ActionListener{
JButton agreeButton;
JButton saveElseButton;
JButton disagreeButton;
JLabel fileInfo;
JPanel buttonJPanel;
JFileChooser saveDialog;
String requestPeople=null;
String fileName=null;
String userHome=null;
String ip=null;
String action=null;
int port;
File file1=null;
String fileLength;
String key;
String userName;
JButton jButton1 = new JButton();
static Hashtable saveFileDisposeButton=new Hashtable();
public FileDisposeButton(String requestPeople,String fileName,
String ip,int port,String fileLength){
this.setSize(new Dimension(10,60));
this.requestPeople=requestPeople;
this.fileName=fileName;
this.ip=ip;
this.port=port;
this.fileLength=fileLength;
file1=new File(this.fileName);
key=this.requestPeople+this.fileName;
userName=ChatArea.loginDialog.getName();
System.out.println(key+" filedisposebuttonkey");
//System.out.println(this.requestPeople+" "+this.fileName+" "+this.ip+" "+this.port);
agreeButton=new JButton();
saveElseButton=new JButton();
disagreeButton=new JButton();
buttonJPanel=new JPanel();
fileInfo=new JLabel(requestPeople+"要向您发送文件["+file1.getName()+"]");
//fileInfo.setText(requestPeople+"要向您发送文件["+file1.getName()+"]");
fileInfo.setForeground(Color.white);
fileInfo.setFont(new Font("Dialog", 0, 16));
// fileInfo.setHorizontalAlignment(JLabel.LEFT);
//fileInfo.setSize(new Dimension(15,20));
//File f=new File("txt.txt");
userHome=System.getProperty("user.home");
saveDialog=new JFileChooser();
saveDialog.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
//saveDialog.setCurrentDirectory(f);
agreeButton.setText("保存");
disagreeButton.setText("拒绝");
saveElseButton.setText("另存为");
agreeButton.setFont(new Font("Dialog",Font.ITALIC+Font.BOLD,16));
saveElseButton.setFont(new Font("Dialog",Font.ITALIC+Font.BOLD,16));
disagreeButton.setFont(new Font("Dialog",Font.ITALIC+Font.BOLD,16));
agreeButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
saveElseButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
disagreeButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
agreeButton.setBorder(null);
saveElseButton.setBorder(null);
disagreeButton.setBorder(null);
agreeButton.setBackground(new Color(140,173,215));
saveElseButton.setBackground(new Color(140,173,215));
disagreeButton.setBackground(new Color(140,173,215));
buttonJPanel.setBackground(new Color(140,173,215));
this.add(fileInfo,BorderLayout.EAST);
buttonJPanel.add(agreeButton);
buttonJPanel.add(saveElseButton);
buttonJPanel.add(disagreeButton);
//this.add(agreeButton);
//this.add(saveElseButton);
//this.add(disagreeButton);
this.add(buttonJPanel,BorderLayout.CENTER);
this.setBackground(new Color(140,173,215));
agreeButton.addActionListener(this);
saveElseButton.addActionListener(this);
disagreeButton.addActionListener(this);
this.saveFileDisposeButton.put(key,this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==agreeButton){
if(agreeButton.getText().equals("保存")){
action = "AGREE";
if (ChatArea.getSocket() != null) {
try {
this.saveElseButton.setVisible(false);
this.disagreeButton.setVisible(false);
this.agreeButton.setText("取消");
ChatArea.out.writeUTF("AGREE_ACCEPT:" + requestPeople
+ "#FILENAME#" + fileName);
//启动文件接受线程
//String userDir=System.getProperty("user.dir");
File file = new File("g:\\try", file1.getName());
Thread acceptFileThread = new Thread(new ClientAcceptFile
(requestPeople, file, ip, port,
action, fileName,fileLength));
acceptFileThread.start();
//this.agreeButton.setEnabled(false);
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
}else if(agreeButton.getText().equals("取消")){
this.agreeButton.setVisible(false);
//action = "DISAGREE";
if (ChatArea.getSocket() != null) {
try {
ChatArea.out.writeUTF("CANCEL_SENDING:" + requestPeople +"#TOPEOPLE#"+this.userName+
"#FILENAME#" + fileName);
// Thread acceptFileThread = new Thread(new ClientAcceptFile
// (requestPeople, file1, ip,
// port, action, fileName));
//Thread cancelFileSend=new Thread((ClientAcceptFile)ClientAcceptFile.saveThread.get(requestPeople+fileName));
//acceptFileThread.start();
ClientAcceptFile cancelFileSend = (ClientAcceptFile)
ClientAcceptFile.saveThread.get(requestPeople + fileName);
//System.out.println(requestPeople+fileName+"按钮取消");
cancelFileSend.dos.flush();
cancelFileSend.dos.close();
cancelFileSend.dis.close();
cancelFileSend.raf.close();
cancelFileSend.acceptFileSocket.close();
this.fileInfo.setText("您取消了文件[" + cancelFileSend.file.getName() +"]发送");
ClientAcceptFile.saveThread.remove(cancelFileSend);
}catch (IOException ioe) {
//ioe.printStackTrace();
}
}
}
}
else if(e.getSource()==saveElseButton){
int state=saveDialog.showSaveDialog(null);
action="AGREE";
if(state==saveDialog.APPROVE_OPTION){
this.saveElseButton.setVisible(false);
this.disagreeButton.setVisible(false);
this.agreeButton.setText("取消");
String chooseDir=saveDialog.getSelectedFile().getPath();
File file=new File(chooseDir,file1.getName());
Thread acceptFileThread=new Thread(new ClientAcceptFile
(requestPeople,file,ip,port,action,fileName,fileLength));
acceptFileThread.start();
//this.agreeButton.setEnabled(false);
// System.out.println(saveDialog.getSelectedFile().getPath());
}
}
else if(e.getSource()==disagreeButton){
action="DISAGREE";
if(ChatArea.getSocket()!=null){
try {
//ChatArea.out.writeUTF("DISAGREE_ACCEPT:" + requestPeople +
// "#FILENAME#" + fileName);
Thread acceptFileThread=new Thread(new ClientAcceptFile
(requestPeople,file1,ip,port,action,fileName,fileLength));
acceptFileThread.start();
}
catch (Exception e2) {
//ioe.printStackTrace();
}
}
this.agreeButton.setEnabled(false);
this.saveElseButton.setEnabled(false);
this.disagreeButton.setEnabled(false);
//FileDisposeButton fileDisposeButton=(FileDisposeButton)FileDisposeButton.saveFileDisposeButton.get(this.requestPeople+this.fileName);
//System.out.println(fileDisposeButton.fileInfo.getText());
this.fileInfo.setText("您拒绝接收"+file1.getName());
this.buttonJPanel.setVisible(false);
}
}
public static void main(String[] args) {
JFrame f=new JFrame();
FileDisposeButton fb=new FileDisposeButton("dd","ok","12.",2560,"44");
f.getContentPane().add(fb);
f.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -