📄 myconnent.java
字号:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class myconnent extends JFrame {
/**
* Launch the application
*
* @param args
*/
public static void main(String args[]) {
try {
myconnent frame = new myconnent();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the frame
*/
String sex = "";
Socket client;
private BufferedReader in;
private PrintWriter out;
final JTextField txtname = new JTextField();
public myconnent() {
super();
setTitle("聊天登录");
getContentPane().setLayout(null);
setBounds(100, 100, 500, 249);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JRadioButton man = new JRadioButton();
man.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
sex = "man";
}
});
man.setText("男");
man.setBounds(286, 39, 45, 27);
getContentPane().add(man);
final JRadioButton woman = new JRadioButton();
woman.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
sex = "woman";
}
});
woman.setText("女");
woman.setBounds(368, 39, 55, 25);
getContentPane().add(woman);
//
ButtonGroup bg = new ButtonGroup();
bg.add(man);
bg.add(woman);
final JLabel lbname = new JLabel();
lbname.setText("姓名");
lbname.setBounds(15, 40, 75, 25);
getContentPane().add(lbname);
txtname.setBounds(68, 40, 160, 20);
getContentPane().add(txtname);
final JButton btin = new JButton();
btin.addActionListener(new ActionListener() {//按钮中判断文本框和radio
public void actionPerformed(final ActionEvent e) {
if (txtname.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "请输入用户名!", "提示",
JOptionPane.ERROR_MESSAGE);
} else if (sex.length() == 0) {
JOptionPane.showMessageDialog(null, "请选择性别!", "提示",
JOptionPane.ERROR_MESSAGE);
} else {
try {
link();//连接服务器
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
myclient client=new myclient();
client.init(in,out,txtname.getText());
client.show(true);
close();
}
}
});
btin.setText("进入");
btin.setBounds(195, 115, 130, 30);
getContentPane().add(btin);
}
public void close(){//隐藏当前窗口
this.setVisible(false);
}
public void link() throws Exception{ //连接服务器
client = new Socket("localhost",6000);
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream());
out.println(txtname.getText());
out.flush();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -