?? connect.java
字號:
import java.awt.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.awt.event.*;
class Connect extends JFrame implements ActionListener,ItemListener{
/**
*
*/
private static final long serialVersionUID = 1L;
private Socket client;//套接字
private String hostName = "localhost";
private int port = 6000;
private JLabel nameLabel;
private JTextField nameText;
private JRadioButton boyRadio,girlRadio,secretRadio;
private String sex ="";
private JLabel hostLabel;
private JLabel portLabel;
private JTextField hostText;
private JTextField portText;
private JButton cancell;
private JButton ok;
private Container c;
private BufferedReader in;
private PrintWriter out;
public Connect(){
super("聊天登陸");
c = this.getContentPane();
c.setLayout(new BorderLayout());
final Color defaultBackground = this.getBackground();
hostLabel = new JLabel("地址:");
portLabel = new JLabel("端口:");
hostText = new JTextField(10);
hostText.setText(hostName); //設(shè)置默認值
portText = new JTextField(4);
portText.setText(Integer.toString(port));
cancell = new JButton("放棄");
ok = new JButton("連接");
nameLabel = new JLabel("姓名:");
nameText = new JTextField(10);
boyRadio = new JRadioButton("男生");
girlRadio = new JRadioButton("女生");
secretRadio = new JRadioButton("保密");
ButtonGroup sexGroup = new ButtonGroup();
sexGroup.add(boyRadio);
sexGroup.add(girlRadio);
sexGroup.add(secretRadio);
//*********用戶信息面板*********************//
JPanel userInfoPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
userInfoPanel.add(nameLabel);
userInfoPanel.add(nameText);
userInfoPanel.add(boyRadio);
userInfoPanel.add(girlRadio);
userInfoPanel.add(secretRadio);
//***************************************//
//****************連接服務(wù)器面板******************//
JPanel centerPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
centerPanel.add(hostLabel);
centerPanel.add(hostText);
centerPanel.add(portLabel);
centerPanel.add(portText);
//***************************************************//
//*******************組合以上兩個面板*****************//
JPanel upperPanel = new JPanel(new GridLayout(2,1)); //面板
upperPanel.setBorder(BorderFactory.createLineBorder(new Color(125,161,253),2));
upperPanel.setBackground(new Color(70,61,129));
upperPanel.add(userInfoPanel);
upperPanel.add(centerPanel);
//***************************************************//
//****************連接確認連接的面板******************//
JPanel lowerPanel = new JPanel();
JLabel spaceLabel = new JLabel("");
spaceLabel.setPreferredSize(new Dimension(20,20));
lowerPanel.add(cancell);
lowerPanel.add(spaceLabel);
lowerPanel.add(ok);
//***************************************************//
//***************組合整個框架**********************//
c.add(upperPanel,BorderLayout.CENTER);
c.add(lowerPanel,BorderLayout.SOUTH);
//**************************************************//
//事件監(jiān)聽 單選
boyRadio.addItemListener(this);
girlRadio.addItemListener(this);
secretRadio.addItemListener(this);
//事件監(jiān)聽 按鈕
cancell.addActionListener(this);
ok.addActionListener(this);
this.setPreferredSize(new Dimension(350,150));
this.setMaximumSize(new Dimension(350,150));
this.pack();
this.setResizable(false); //設(shè)置不能最大化
this.setVisible(true);
}
//事件 單選
public void itemStateChanged(ItemEvent e){
if (e.getSource() == boyRadio){
sex = "Boy";
}
if (e.getSource() == girlRadio){
sex = "Girl";
}
if (e.getSource() == secretRadio){
sex = "Secret";
}
}
//事件 按鈕
public void actionPerformed(ActionEvent e){
if(e.getSource() == cancell){
this.shutDown();
}
else if (e.getSource() == ok){
if ((nameText.getText()).trim().length() == 0){
JOptionPane.showMessageDialog(this,"請輸入一個名字","提示",JOptionPane.INFORMATION_MESSAGE);
return;
}
else if (sex.length() == 0){
JOptionPane.showMessageDialog(this,"請選擇性別","提示",JOptionPane.INFORMATION_MESSAGE);
return;
}
else{
try{
this.link();
ChatFrame app = new ChatFrame();
app.init(in,out,nameText.getText());
ok.setEnabled(false); //確保不會被再次點擊
this.setVisible(false);
}catch (Exception ee){
JOptionPane.showMessageDialog(this,"登陸失敗","失敗",JOptionPane.ERROR_MESSAGE);
return;
}
}
}
}
public void link() throws Exception{ //連接服務(wù)器
hostName = hostText.getText().trim();
port = Integer.parseInt(portText.getText());
client = new Socket(hostName,port);
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream());
out.println(nameText.getText()+"&"+sex);
out.flush();
/**
catch(NumberFormatException nfe){
System.out.println(nfe);
// return false;
}
catch(Exception e){
System.out.println(e);
// return false;
}
//return true;
*/
}
public void shutDown() {
System.exit(0);
}
public static void main(String[] args) {
Connect app = new Connect();
app.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -