📄 mainframe.java
字号:
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel1() {
if (jPanel1 == null) {
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(5, 59, 44, 21));
jLabel1.setText("密码");
jLabel = new JLabel();
jLabel.setText("用户名");
jLabel.setBounds(new Rectangle(6, 18, 43, 23));
jPanel1 = new JPanel();
jPanel1.setLayout(null);
jPanel1.setBounds(new Rectangle(0, 1, 206, 122));
jPanel1.add(jLabel, null);
jPanel1.add(jLabel1, null);
jPanel1.add(getJTextField(), null);
jPanel1.add(getJButton4(), null);
jPanel1.add(getJButton5(), null);
jPanel1.add(getJPasswordField(), null);
}
return jPanel1;
}
/**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(63, 18, 135, 21));
jTextField.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 14));
}
return jTextField;
}
/**
* This method initializes jDesktopPane
*
* @return javax.swing.JDesktopPane
*/
private JDesktopPane getJDesktopPane() {
if (jDesktopPane == null) {
jDesktopPane = new JDesktopPane();
jDesktopPane.setBounds(new Rectangle(396, 25, 206, 124));
jDesktopPane.add(getJPanel1(), null);
}
return jDesktopPane;
}
/**
* This method initializes jSplitPane
*
* @return javax.swing.JSplitPane
*/
private JSplitPane getJSplitPane() {
if (jSplitPane == null) {
jLabel4 = new JLabel();
jLabel4.setText("Online");
jSplitPane = new JSplitPane();
jSplitPane.setBounds(new Rectangle(396, 155, 207, 50));
jSplitPane.setRightComponent(getJScrollPane4());
jSplitPane.setLeftComponent(jLabel4);
}
return jSplitPane;
}
/**
* This method initializes jButton2
*
* @return javax.swing.JButton
*/
private JButton getJButton2() {
if (jButton2 == null) {
jButton2 = new JButton();
jButton2.setText("字体");
//jButton2.setIcon(new ImageIcon("Icon\\FontFamily.JPG"));
//jButton2.setIcon(new ImageIcon("Icon\\IMSmallToolbarFont.ico"));
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {//字体选择器
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Object[] fontFamilyValues = ge.getAvailableFontFamilyNames();
Object selectedValue = JOptionPane.showInputDialog(null,"Choose one", "FontFamilyChooser",
JOptionPane.INFORMATION_MESSAGE,null,fontFamilyValues, fontFamilyValues[0]);
if (selectedValue!=null){
fontFamily = (String)selectedValue;
jTextArea.setFont(new Font(fontFamily,Font.PLAIN,fontSize));
}
//System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return jButton2;
}
/**
* This method initializes jButton3
*
* @return javax.swing.JButton
*/
private JButton getJButton3() {
if (jButton3 == null) {
jButton3 = new JButton();
jButton3.setText("颜色");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
//jColorChooser.setVisible(true);
Color retValue=JColorChooser.showDialog(new JFrame(),new String("ColorChooser"),myTextColor);
if(retValue!=null){
myTextColor = retValue;
jTextArea.setForeground(myTextColor);
jButton3.setForeground(myTextColor);
}
//System.out.println(myTextColor.getRGB());
//System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return jButton3;
}
/**
* This method initializes jButton4
*
* @return javax.swing.JButton
*/
private JButton getJButton4() {
if (jButton4 == null) {
jButton4 = new JButton();
jButton4.setBounds(new Rectangle(111, 95, 71, 20));
jButton4.setText("注册");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if(login){//已登陆
JOptionPane.showMessageDialog(null, "您已经登陆了", "ERROR", JOptionPane.INFORMATION_MESSAGE);
return;
}
myUserInfo = new UserInfo();
myUserInfo.name = JOptionPane.showInputDialog("请输入用户名");
if(myUserInfo.name == null) return;
myUserInfo.password = JOptionPane.showInputDialog("请输入密码");
if(myUserInfo.password == null) return;
myUserInfo.sex = JOptionPane.showInputDialog("请输入您的性别");
if(myUserInfo.sex == null) return;
myUserInfo.age = JOptionPane.showInputDialog("请输入您的年龄");
if(myUserInfo.age == null) return;
myUserInfo.city = JOptionPane.showInputDialog("请输入您所在的城市");
if(myUserInfo.city == null) return;
myUserInfo.email = JOptionPane.showInputDialog("请输入Email");
if(myUserInfo.email == null) return;
try{
socket = new Socket(jTextField2.getText(),1683);
objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectInputStream = new ObjectInputStream(socket.getInputStream());
}catch(ConnectException ce){
//System.out.println("Can not connect to Host "+jTextField1.getText());
JOptionPane.showMessageDialog(null, new String("不能连接到主机").concat(jTextField2.getText()), "ERROR", JOptionPane.ERROR_MESSAGE);
login = false;//登陆失败
return;
}catch(Exception e2){
e2.printStackTrace();
}
StreamData newUser = new StreamData(myUserInfo.name,"ServerNewUser","TEXT",fontFamily,fontSize,myTextColor.getRGB(),myUserInfo.toString().getBytes(),myUserInfo.toString().getBytes().length);
try{
objectOutputStream.writeObject(newUser);
}catch(Exception ioe){
ioe.printStackTrace();
}
login = true;
//System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return jButton4;
}
/**
* This method initializes jButton5
*
* @return javax.swing.JButton
*/
private JButton getJButton5() {
if (jButton5 == null) {
jButton5 = new JButton();
jButton5.setBounds(new Rectangle(21, 95, 71, 20));
jButton5.setText("登录");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if(login){//已登陆
JOptionPane.showMessageDialog(null, "您已经登陆了", "ERROR", JOptionPane.INFORMATION_MESSAGE);
return;
}
if(new String(jTextField.getText()).length()==0) {
JOptionPane.showMessageDialog(null, "用户名不能为空,请重新输入", "ERROR", JOptionPane.ERROR_MESSAGE);
return; //拒绝空用户名
}
try{
socket = new Socket(jTextField2.getText(),1683);
objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectInputStream = new ObjectInputStream(socket.getInputStream());
}catch(ConnectException ce){
//System.out.println("Can not connect to Host "+jTextField1.getText());
JOptionPane.showMessageDialog(null, new String("不能连接到主机").concat(jTextField2.getText()), "ERROR", JOptionPane.ERROR_MESSAGE);
login = false;//登陆失败
return;
}catch(Exception e2){
e2.printStackTrace();
}
login = true;//成功登陆
jTextField.setEditable(false);
jPasswordField.setEditable(false);
data = new StreamData(jTextField.getText(),"Server","TEXT",jPasswordField.getText(),fontSize,myTextColor.getRGB(),new String("Hello").getBytes(),((new String("Hello")).getBytes()).length);
try{
objectOutputStream.writeObject(data);
}catch(Exception ioe){
ioe.printStackTrace();
}
//System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return jButton5;
}
/**
* This method initializes jDesktopPane1
*
* @return javax.swing.JDesktopPane
*/
private JDesktopPane getJDesktopPane1() {
if (jDesktopPane1 == null) {
jDesktopPane1 = new JDesktopPane();
jDesktopPane1.setBounds(new Rectangle(396, 216, 205, 187));
jDesktopPane1.add(getJPanel2(), null);
}
return jDesktopPane1;
}
/**
* This method initializes jPanel2
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel2() {
if (jPanel2 == null) {
jLabel2 = new JLabel();
jLabel2.setText("请在在线列表中选择聊天对象");
jLabel2.setBounds(new Rectangle(3, 2, 199, 18));
jPanel2 = new JPanel();
jPanel2.setLayout(null);
jPanel2.setBounds(new Rectangle(1, 1, 205, 185));
jPanel2.add(jLabel2, null);
jPanel2.add(getJScrollPane2(), null);
}
return jPanel2;
}
/**
* This method initializes jList
*
* @return javax.swing.JList
*/
private JList getJList() {
if (jList == null) {
jList = new JList();
jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION );
jList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent e) {
//jButton1.setEnabled(true);
toName = (String)jList.getSelectedValue();
if(toName!=null){
jLabel2.setText(new String("与 ").concat(toName).concat(new String(" 聊天中")));
boolean exist = false;
for(int i=1;i<jTabbedPane.getTabCount();i++){//在标签上添加选定的人
if(jTabbedPane.getTitleAt(i).equals(toName)){
jTabbedPane.setSelectedIndex(i);
exist = true;
break;
}
}
if(exist == false){
JScrollPaneWithText jSText = new JScrollPaneWithText();
jTabbedPane.addTab(toName, null, jSText, null);
jTabbedPane.setSelectedComponent(jSText);
}
// TODO Auto-generated Event stub valueChanged()
}
else
jLabel2.setText("请在在线列表中选择聊天对象");
}
});
}
return jList;
}
/**
* This method initializes jScrollPane4
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane4() {
if (jScrollPane4 == null) {
jScrollPane4 = new JScrollPane();
jScrollPane4.setViewportView(getJList());
}
return jScrollPane4;
}
/**
* This method initializes jButton6
*
* @return javax.swing.JButton
*/
private JButton getJButton6() {
if (jButton6 == null) {
jButton6 = new JButton();
jButton6.setText("图片");
//jButton6.setIcon(new ImageIcon("Icon\\Image.JPG"));
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if(login == false){//尚未登陆
JOptionPane.showMessageDialog(null, "请先登陆", "ERROR", JOptionPane.ERROR_MESSAGE);
return;
}
if(toName==null){//没有接收方
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -