?? jdialogconnection.java~19~
字號:
package multiclientnew;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.*;
import java.io.*;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class JDialogConnection extends JFrame {
String nameRequest;
String ipRequest;
String thisName;
DatagramSocket socket;
byte[] buf =new byte[1000];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
InetAddress serverHost;
int serverPort =5566;
public JDialogConnection(String thisname,String name,String ip) {
thisName = thisname;
nameRequest = name;
ipRequest = ip;
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
getContentPane().setLayout(null);
this.setLocation(300,200);
this.setSize(200,150);
jLabel1.setText(nameRequest+"@ "+ipRequest+" "+"請求私聊");
jLabel1.setBounds(new Rectangle(20, 10, 224, 35));
this.setTitle("DialogConnection");
this.setVisible(true);
jButton2.setBounds(new Rectangle(96, 62, 87, 28));
jButton2.setText("拒絕 ");
jButton2.addActionListener(new JDialogConnection_jButton2_actionAdapter(this));
jButton1.addActionListener(new JDialogConnection_jButton1_actionAdapter(this));
this.getContentPane().add(jLabel1);
this.getContentPane().add(jButton1);
this.getContentPane().add(jButton2);
jButton1.setBounds(new Rectangle(7, 62, 77, 28));
jButton1.setText("同意");
}
JLabel jLabel1 = new JLabel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
public void connectionServer(String str){
try{
serverHost = InetAddress.getByName(ipRequest);
}catch(IOException e1){
System.out.println("ip地址出錯\n");
}
try{
socket = new DatagramSocket();
System.out.println("客戶端數據報套接字建立\n");
}catch(IOException e2){
System.out.println("端口打開出錯\n");
}
if(socket!=null){
buf = str.getBytes();
dp = new DatagramPacket(buf,buf.length,serverHost,serverPort);
try{
socket.send(dp);
if(str.equalsIgnoreCase("拒絕")){
socket.close();
}
}catch(IOException e3){
System.out.println("信息發送出錯\n");
}
}
}
public void jButton1_actionPerformed(ActionEvent e) {
connectionServer("同意");
this.setVisible(false);
PtoPChat p2chat = new PtoPChat(thisName,nameRequest,ipRequest);
}
public void jButton2_actionPerformed(ActionEvent e) {
connectionServer("拒絕");
this.setVisible(false);
}
}
class JDialogConnection_jButton2_actionAdapter implements ActionListener {
private JDialogConnection adaptee;
JDialogConnection_jButton2_actionAdapter(JDialogConnection adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
class JDialogConnection_jButton1_actionAdapter implements ActionListener {
private JDialogConnection adaptee;
JDialogConnection_jButton1_actionAdapter(JDialogConnection adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -