package com.stark.manager.user;
import java.awt.Color;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
/**
*
*
* @author HuJiaMing
*
*/
public class AddUser extends JPanel {
private static final long serialVersionUID = 5836543413766255798L;
//
private JTextField id_JTextField, name_JTextField, age_JTextField ,
grade_JTextField, birthDate_JTextField, college_JTextField,
major_JTextField, joinTime_JTextField;
private JLabel id_JLabel, name_JLabel, password_JLabel, age_JLabel,
sex_JLabel, pic_JLabel, pic, grade_JLabel, birthDate_JLabel,date_JLabel,date_JLabel2,
college_JLabel, major_JLabel, joinTime_JLabel, hobby_JLabel;
private JPasswordField password;
private JButton add_Btn, cancel_Btn, setPic_Btn;
private JTextArea hobby_JTextArea;
private JRadioButton male_JRadioBtn, female_JRadioBtn;
private ButtonGroup sexGroup_Btn;
private JFileChooser jFileChooser;
private String selectedJRadio = "F";
//
PrintStream output;
DataInputStream input;
String message = "", fileName="D:"+File.separator+"android.jpg";
/* String message = "", fileName="D:"+File.separator+"Study Files"+File.separator+"javaprojects"+File.separator+"PersonalAndSlaryManagerSystem"+File.separator+"src"+File.separator+"com"+File.separator+"stark"+File.separator+"images"+File.separator+"android.jpg";
*/
public AddUser() {
this.setLayout(null);
id_JLabel = new JLabel("");
id_JTextField = new JTextField();
id_JTextField.setText("");
id_JTextField.setEditable(false);
name_JLabel = new JLabel("");
name_JTextField = new JTextField();
password_JLabel = new JLabel("");
password = new JPasswordField();
age_JLabel = new JLabel("");
age_JTextField = new JTextField();
sex_JLabel = new JLabel("");
male_JRadioBtn = new JRadioButton("");
female_JRadioBtn = new JRadioButton("");
sexGroup_Btn = new ButtonGroup();
sexGroup_Btn.add(male_JRadioBtn);
sexGroup_Btn.add(female_JRadioBtn);
male_JRadioBtn.setSelected(true);
pic_JLabel = new JLabel("");
pic = new JLabel();
setPic_Btn = new JButton("");
grade_JLabel = new JLabel("");
grade_JTextField = new JTextField();
birthDate_JLabel = new JLabel("");
birthDate_JTextField = new JTextField();
college_JLabel = new JLabel("");
college_JTextField = new JTextField();
major_JLabel = new JLabel("");
major_JTextField = new JTextField();
joinTime_JLabel = new JLabel("");
joinTime_JTextField = new JTextField();
hobby_JLabel = new JLabel("");
hobby_JTextArea = new JTextArea();
date_JLabel= new JLabel("YYYY-MM-DD");
date_JLabel.setForeground(Color.red);
date_JLabel2= new JLabel("YYYY-MM-DD");
date_JLabel2.setForeground(Color.red);
add_Btn = new JButton("");
cancel_Btn = new JButton("");
//
//
id_JLabel.setBounds(60, 5, 60, 26);
id_JTextField.setBounds(140, 5, 170, 26);
name_JLabel.setBounds(60, 30, 90, 26);
name_JTextField.setBounds(140, 30, 170, 26);
password_JLabel.setBounds(60, 55, 120, 26);
password.setBounds(140, 55, 170, 26);
age_JLabel.setBounds(60, 80, 100, 26);
age_JTextField.setBounds(140, 80, 170, 26);
sex_JLabel.setBounds(60, 105, 60, 26);
male_JRadioBtn.setBounds(140, 105, 40, 26);
female_JRadioBtn.setBounds(180, 105, 40, 26);
pic_JLabel.setBounds(60, 130, 60, 26);
pic.setBounds(160, 130, 120, 100);
setPic_Btn.setBounds(290, 210, 90, 20);
grade_JLabel.setBounds(60, 230, 60, 26);
grade_JTextField.setBounds(140, 230, 170, 26);
birthDate_JLabel.setBounds(60, 255, 60, 26);
birthDate_JTextField.setBounds(140, 255, 170, 26);
date_JLabel.setBounds(315, 255, 100, 26);
college_JLabel.setBounds(60, 280, 60, 26);
college_JTextField.setBounds(140, 280, 170, 26);
major_JLabel.setBounds(60, 305, 60, 26);
major_JTextField.setBounds(140, 305, 170, 26);
joinTime_JLabel.setBounds(60, 330, 120, 26);
joinTime_JTextField.setBounds(140, 330, 170, 26);
date_JLabel2.setBounds(315, 330, 100, 26);
hobby_JLabel.setBounds(60, 355, 60, 26);
hobby_JTextArea.setBounds(140, 355, 170, 50);
pic.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
Image image=null;
File sourceImage= new File(fileName);
try {
image=ImageIO.read(sourceImage);
} catch (IOException e1) {
e1.printStackTrace();
}
pic.setIcon( new ImageIcon(image));
//
cancel_Btn.setBounds(80, 415, 80, 25);
add_Btn.setBounds(220, 415, 80, 25);
//
this.add(id_JLabel);
this.add(id_JTextField);
this.add(name_JLabel);
this.add(name_JTextField);
this.add(password_JLabel);
this.add(password);
this.add(age_JLabel);
this.add(age_JTextField);
this.add(sex_JLabel);
this.add(pic_JLabel);
this.add(pic);
this.add(setPic_Btn);
this.add(grade_JLabel);
this.add(grade_JTextField);
this.add(male_JRadioBtn);
this.add(female_JRadioBtn);
this.add(birthDate_JLabel);
this.add(birthDate_JTextField);
this.add(date_JLabel);
this.add(college_JLabel);
this.add(college_JTextField);
this.add(major_JLabel);
this.add(major_JTextField);
this.add(joinTime_JLabel);
this.add(joinTime_JTextField);
this.add(date_JLabel2);
this.add(hobby_JLabel);
this.add(hobby_JTextArea);
this.add(add_Btn);
this.add(cancel_Btn);
setPic_Btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
pic.setVisible(true);
jFileChooser = new JFileChooser(".");
jFileChooser.addChoosableFileFilter(new MyFileFilter("gif"));
jFileChooser.addChoosableFileFilter(new MyFileFilter("jpg"));
int result = jFileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File file = jFileChooser.getSelectedFile();
fileName = file.getAbsoluteFile().toString();
pic.setIcon(new ImageIcon(fileName));
} else if (result == JFileChooser.CANCEL_OPTION) {
;//
}
}
}
);
// add_Btn
add_Btn.addActionListener(new ActionListener() {
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
if (name_JTextField.getText().toString().equals(""))
JOptionPane.showMessageDialog(null, "", "",
JOptionPane.WARNING_MESSAGE);
else if (password.getText().toString().equals(""))
JOptionPane.showMessageDialog(null, "", "",
JOptionPane.WARNING_MESSAGE);
else if (password.getText().toString().equals(""))
JOptionPane.showMessageDialog(null, "", "",
JOptionPane.WARNING_MESSAGE);
else if (age_JTextField.getText().toString().equals(""))
JOptionPane.showMessageDialog(null, "", "",
JOptionPane.WARNING_MESSAGE);
else if (grade_JTextField.getText().toString().equals(""))
JOptionPane.showMessageDialog(null, "", "",
JOptionPane.WARNING_MESSAGE);
else if (birthDate_JTextField.getText().toString().equals(""))
JOptionPane.showMessageDialog(null, "", "",
JOptionPane.WARNING_MESSAGE);
else if (college_JTextField.getText().toString().equals(""))
JOptionPane.showMessageDialog(null, "", "",
JOptionPane.WARNING_MESSAGE);
else if (major_JTextField.getText().toString().equals(""))
JOptionPane.showMessageDialog(null, "", "",
JOptionPane.WARNING_MESSAGE);
else if (joinTime_JTextField.getText().toString().equals(""))
JOptionPane.showMessageDialog(null, "", "",
JOptionPane.WARNING_MESSAGE);
else if (hobby_JTextArea.getText().toString().equals(""))
JOptionPane.showMessageDialog(null, "", "",
JOptionPane.WARNING_MESSAGE);
// connect
connect("add" + "%" + name_JTextField.getText().toString()
+ "%" + password.getText().toString() + "%"
+ age_JTextField.getText().toString() + "%"
+ selectedJRadio + "%"
+ fileName + "%"
+ grade_JTextField.getText().toString() + "%"
+ birthDate_JTextField.getText().toString() + "%"
+ college_JTextField.getText().toString() + "%"
+ major_JTextField.getText().toString() + "%"
+ hobby_JTextArea.getText().toString() + "%"
+ joinTime_JTextField.getText().toString());
}
});
// cancel_Btn
cancel_Btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
name_JTextField.setText("");
password.setText("");
age_JTextField.setText("");
pic.setVisible(false);
grade_JTextField.setText("");
birthDate_JTextField.setText("");
college_JTextField.setText("");
major_JTextField.setText("");
hobby_JTextArea.setText("");
joinTime_JTextField.setText("");
}
});
// male_JRadioBtn
male_JRadioBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
selectedJRadio = "F";
}
});
// female_JRadioBtn
female_JRadioBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
selectedJRadio = "M";
}
});
setSize(250, 400);
this.setVisible(true);
}
// connect
@SuppressWarnings("deprecation")
public void connect(String s) {
Socket socket;
try {
// Socket8080
socket = new Socket(InetAddress.getByName(""), 8080);
//
output = new PrintStream(new BufferedOutputStream(
socket.getOutputStream()));
input = new DataInputStream(new BufferedInputStream(
socket.getInputStream()));
//
output.println(s);
output.flush();
try {
//
message = (String) input.readLine();
if (message.equals("AddSuccess")) {
id_JTextField.setText("");
name_JTextField.setText("");
password.setText("");
age_JTextField.setText("");
pic.setVisible(false);
fileName=null;
grade_JTextField.setText("");
birthDate_JTextField.setText("");
college_JTextField.setText("");
major_JTextField.setText("");
joinTime_JTextField.setText("");
hobby_JTextArea.setText("");
JOptionPane.showMessageDialog(null, "", "",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "", "",
JOptionPane.INFORMATION_MESSAGE);
}
} catch (IOException e) {
System.out.println("");
}
// socket
output.close();
input.close();
socket.close();
} catch (EOFException eof) {
System.out.println("");
} catch (IOException e) {
e.printStackTrace();
}
}
}
|