package netchat;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
public class FrameChat extends JFrame
{
Socket cSocket;
DataOutputStream DOS;
private BorderLayout layoutMain = new BorderLayout();
private JPanel panelCenter = new JPanel();
private JMenuBar menuBar = new JMenuBar();
private JMenu menuFile = new JMenu();
private JMenuItem menuFileExit = new JMenuItem();
private JMenu menuHelp = new JMenu();
private JMenuItem menuHelpAbout = new JMenuItem();
private JLabel statusBar = new JLabel();
private JToolBar toolBar = new JToolBar();
private JButton buttonOpen = new JButton();
private JButton buttonClose = new JButton();
private JButton buttonHelp = new JButton();
private ImageIcon imageOpen =
new ImageIcon(FrameChat.class.getResource("openfile.gif"));
private ImageIcon imageClose =
new ImageIcon(FrameChat.class.getResource("closefile.gif"));
private ImageIcon imageHelp =
new ImageIcon(FrameChat.class.getResource("help.gif"));
private JScrollPane jScrollPane1 = new JScrollPane();
public static JTextArea jTxtArea = new JTextArea();
private JTextField jTxt = new JTextField();
private JButton jBTSend = new JButton();
private JButton jBtSendFile = new JButton();
//private JComboBox jComboBoxUserChoose = new JComboBox();
private JComboBox jComboBoxUserChoose = new JComboBox();
public FrameChat()
{
try
{
jbInit();
cSocket = new Socket("10.3.0.244", 1111);
DOS = new DataOutputStream(cSocket.getOutputStream());
//while (true) {
Thread Trcv = new Thread(new receiveMsg(cSocket));
Trcv.start();
//System.out.println("Thread start!");
Thread T=new Thread(new ReceiveFile());
T.start();
//}
}
catch (Exception e)
{
e.printStackTrace();
}
}
private void jbInit()
throws Exception
{
this.setJMenuBar(menuBar);
this.getContentPane().setLayout(layoutMain);
panelCenter.setLayout(null);
this.setSize(new Dimension(400, 300));
menuFile.setText("File");
menuFileExit.setText("Exit");
menuFileExit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
fileExit_ActionPerformed(ae);
}
});
menuHelp.setText("Help");
menuHelpAbout.setText("About");
menuHelpAbout.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
helpAbout_ActionPerformed(ae);
}
});
statusBar.setText("");
buttonOpen.setToolTipText("Open File");
buttonOpen.setIcon(imageOpen);
buttonClose.setToolTipText("Close File");
buttonClose.setIcon(imageClose);
buttonHelp.setToolTipText("About");
buttonHelp.setIcon(imageHelp);
jScrollPane1.setBounds(new Rectangle(10, 5, 370, 155));
jTxtArea.setLineWrap(true);
jTxt.setBounds(new Rectangle(10, 165, 265, 25));
jBTSend.setText("Send Message");
jBTSend.setBounds(new Rectangle(275, 165, 105, 25));
jBTSend.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jBTSend_actionPerformed(e);
}
});
jBtSendFile.setText("Send File");
jBtSendFile.setBounds(new Rectangle(275, 195, 105, 25));
jBtSendFile.setActionCommand("jBtSendFile");
jBtSendFile.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jBtSendFile_actionPerformed(e);
}
});
jComboBoxUserChoose.setBounds(new Rectangle(10, 190, 265, 30));
menuFile.add(menuFileExit);
menuBar.add(menuFile);
menuHelp.add(menuHelpAbout);
menuBar.add(menuHelp);
this.getContentPane().add(statusBar, BorderLayout.SOUTH);
toolBar.add(buttonOpen);
toolBar.add(buttonClose);
toolBar.add(buttonHelp);
this.getContentPane().add(toolBar, BorderLayout.NORTH);
jScrollPane1.getViewport().add(jTxtArea, null);
panelCenter.add(jComboBoxUserChoose, null);
panelCenter.add(jBtSendFile, null);
panelCenter.add(jBTSend, null);
panelCenter.add(jTxt, null);
panelCenter.add(jScrollPane1, null);
/*
Socket C=new Socket("10.3.0.244",1111);
Socket C1=new Socket("10.3.0.244",1111);
jComboBoxUserChoose.addItem(C);
jComboBoxUserChoose.addItem(C1);
*/
this.getContentPane().add(panelCenter, BorderLayout.CENTER);
}
void fileExit_ActionPerformed(ActionEvent e)
{
System.exit(0);
}
void helpAbout_ActionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(this, new FrameChat_AboutBoxPanel1(),
"About", JOptionPane.PLAIN_MESSAGE);
}
private void jBTSend_actionPerformed(ActionEvent e)
{
if (!jTxt.getText().trim().equals(""))
{
Thread T = new Thread(new sendMsg(cSocket, jTxt.getText()));
T.start();
jTxt.setText("");
}
/*
send S=new send(socket);
S.doSend(jTxt.getText());
jTxt.setText("");*/
/*
try {
//System.out.println("msg:"+jTxt.getText());
//System.out.println("my info:"+cSocket.toString());
DOS.writeUTF(jTxt.getText());//為什么程序到這里就無法遠行下去了呢?
//System.out.println("msg:"+jTxt.getText());
jTxt.setText("");
//System.out.println("my info:"+cSocket.toString());
} catch (Exception f) {
f.printStackTrace();
}*/
}
private void jBtSendFile_actionPerformed(ActionEvent e)
{
try
{
FileDialog fd = new FileDialog(this, "文件打開對話框", FileDialog.LOAD);
fd.setVisible(true);
File file = new File(fd.getDirectory(), fd.getName());
Socket C = new Socket("10.3.0.244", 1111);
Thread sendFile = new Thread(new FileSend(file, C));
sendFile.start();
}
catch (Exception e1)
{
//不傳輸文件就什么也不做
}
}
}