package qq_server;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.*;
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Color;
import javax.swing.JTextArea;
////////////////////////////////
import java.awt.event.*;
import java.net.*;
import java.io.*;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2014</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Frame1 extends JFrame {
File file;
Dialog1 dia = new Dialog1();
int control = 0;
int checknum = 0;
socketconnect Socketconn = new socketconnect();
String filename;
String name_df;
JPanel contentPane;
XYLayout xYLayout1 = new XYLayout();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JLabel jLabel6 = new JLabel();
JTextField jTextField1 = new JTextField(); //ip
JTextField jTextField2 = new JTextField(); //port
JTextField jTextField3 = new JTextField(); //name
JLabel jLabel7 = new JLabel();
JTextField jTextField4 = new JTextField(); //adress
JButton jButton1 = new JButton(); //select
JButton jButton2 = new JButton(); //send
JButton jButton3 = new JButton(); //connect
JButton jButton4 = new JButton(); //no connect
JLabel jLabel8 = new JLabel();
TextArea TextArea1 = new TextArea(); //show message
JLabel jLabel9 = new JLabel();
JTextField jTextField5 = new JTextField(); //send message
JLabel trans = new JLabel();
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(xYLayout1);
setSize(new Dimension(478, 425));
setTitle("Frame Title");
jLabel1.setFont(new java.awt.Font("宋体", Font.BOLD, 16));
jLabel1.setText("配置:");
jLabel2.setFont(new java.awt.Font("宋体", Font.BOLD, 16));
jLabel2.setText("文件传输:");
jLabel3.setFont(new java.awt.Font("宋体", Font.BOLD, 16));
jLabel3.setText("连接:");
jLabel4.setText("你的IP地址:");
jLabel5.setText("端口号:");
jLabel6.setText("你的昵称:");
jLabel7.setText("文件路径:");
jButton1.setText("选择");
jButton2.setText("发送");
jButton3.setText("连接");
jButton4.setText("断开连接");
jTextField1.setBackground(Color.magenta);
jTextField2.setBackground(Color.magenta);
jTextField2.setCaretColor(Color.white);
jTextField2.setText("9000");
jTextField3.setBackground(Color.magenta);
jLabel8.setFont(new java.awt.Font("宋体", Font.BOLD, 16));
jLabel8.setText("消息显示区:");
jLabel9.setFont(new java.awt.Font("宋体", Font.BOLD, 16));
jLabel9.setText("消息发送区:");
trans.setFont(new java.awt.Font("宋体", Font.BOLD, 16));
trans.setForeground(Color.blue);
contentPane.add(jLabel1, new XYConstraints(5, 34, 96, 23));
contentPane.add(jLabel3, new XYConstraints(3, 293, 80, 37));
contentPane.add(jLabel2, new XYConstraints(3, 183, 98, 30));
contentPane.add(jLabel5, new XYConstraints(42, 109, 59, 28));
contentPane.add(jLabel6, new XYConstraints(42, 153, 76, 24));
contentPane.add(jLabel4, new XYConstraints(42, 68, 84, 29));
contentPane.add(jTextField2, new XYConstraints(116, 113, 95, 22));
contentPane.add(jTextField3, new XYConstraints(115, 159, 97, 21));
contentPane.add(jLabel7, new XYConstraints(28, 215, 77, 22));
contentPane.add(jTextField4, new XYConstraints(94, 214, 123, 25));
contentPane.add(jButton3, new XYConstraints(80, 303, 119, 24));
contentPane.add(jButton4, new XYConstraints(82, 346, 116, 25));
contentPane.add(jLabel8, new XYConstraints(224, 28, 127, 34));
contentPane.add(jLabel9, new XYConstraints(224, 276, 106, 31));
contentPane.add(jTextField1, new XYConstraints(115, 70, 94, 24));
contentPane.add(jButton2, new XYConstraints(160, 243, 60, -1));
contentPane.add(jButton1, new XYConstraints(91, 242, 63, -1));
contentPane.add(TextArea1, new XYConstraints(222, 64, 243, 173));
contentPane.add(jTextField5, new XYConstraints(221, 313, 244, 67));
contentPane.add(trans, new XYConstraints(28, 389, 141, 29));
jButton1.addActionListener(new listener());
jButton2.addActionListener(new listener());
jButton3.addActionListener(new listener());
jButton4.addActionListener(new listener());
jTextField5.addActionListener(new listener());
dia.yes.addActionListener(new listener());
dia.no.addActionListener(new listener());
new wait().start(); // 打开接收连接的线程
}
/////////////////////////////////////////////////////////////////////////////////
class listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jButton1) //选择
{
JFileChooser jfc = new JFileChooser(); //文件选择器组件
jfc.showOpenDialog(jButton1); //显示打开对话框
filename = jfc.getSelectedFile().getPath(); //获得选中文件路径和名字
jTextField4.setText(filename);
}
/////////////////////////////////////////////////////////////////////////////////////////
else if (e.getSource() == jButton2) //发送
{
try{
FileInputStream fis = new FileInputStream(filename);//获得选中文件路径和名字
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
String str;
//////////////////////////////// 获取文件名
File tempFile =new File(filename.trim());
String fileName = tempFile.getName();
//////////////////////////////////
trans.setText("Transfered Start...") ;
Socketconn.send("22011211"); //握手信号
Socketconn.send(fileName);
while((str= reader.readLine() )!=null)
{
Socketconn.send(str);
System.out.print(str) ;
}
Socketconn.send("111"); //结束信号
trans.setText("Transfered End!") ;
fis.close();
}
catch(IOException e2){
e2.printStackTrace();}
}
else if (e.getSource() == jButton3) // 连接
{
if (checknum!=1)
{
String ipfield=jTextField1.getText();
String portfield=jTextField2.getText();
if (ipfield.trim().length()==0 ){
TextArea1.append(" IP不能为空,请提供正确的IP地址\n");
}
else{
checknum = Socketconn.connect_zd(ipfield,portfield); /////////