C0712I - FPT Aptech
Bạn có muốn phản ứng với tin nhắn này? Vui lòng đăng ký diễn đàn trong một vài cú nhấp chuột hoặc đăng nhập để tiếp tục.

C0712I - FPT Aptech

Diễn đàn của lớp C0712i - FPT - Aptech
 
Trang ChínhPortalGalleryTìm kiếmLatest imagesĐăng kýĐăng Nhập

 

 Bai assignment Djava Guestinfo

Go down 
Tác giảThông điệp
Admin
Admin
Admin
Admin


Tổng số bài gửi : 99
Join date : 16/01/2008
Age : 42

Bai assignment Djava Guestinfo Empty
Bài gửiTiêu đề: Bai assignment Djava Guestinfo   Bai assignment Djava Guestinfo I_icon_minitimeThu Oct 30, 2008 10:44 pm

import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.util.regex.*;
import java.awt.event.*;
class minh extends Frame implements WindowListener,ActionListener{
String mail="";
JButton bt1,bt2;
JTextField txt1,txt2;
JComboBox com;
JRadioButton ra1,ra2;
JCheckBox check1,check2,check3,check4;
JTextArea ta;
public minh()
{
super("Assignment Swing");

setLayout(null);
//Add component into content panel
JLabel lab1=new JLabel("Guest Name: ");
lab1.setBounds(50,50,75,25);
add(lab1);
txt1=new JTextField(25);
txt1.setFont(new Font("Times New Roman", Font.BOLD, 16));
txt1.setBounds(130,50,200,25);
add(txt1);
JLabel lab2=new JLabel("\nEmail: ");
lab2.setBounds(50,100,75,25);
add(lab2);
txt2= new JTextField(20);
txt2.setFont(new Font("Times New Roman",Font.BOLD,16));
txt2.setBounds(130,100,200,25);
add(txt2);
JLabel lab3=new JLabel("Country: ");
lab3.setBounds(50,150,75,25);
add(lab3);
String x[]={"VietNam","China","USA","Others"};
com= new JComboBox(x);
http://com.setModel(new javax.swing.DefaultComboBoxModel(new String[]={"VietNam","China","Usa"}));

//JPopupMenu pop = new JPopupMenu();
com.setBounds(130,150,75,25);
add(com);
JLabel lab4=new JLabel("Sex: ");
lab4.setBounds(50,200,75,25);
add(lab4);
ra1=new JRadioButton("Male",false);
ra2=new JRadioButton("FeMale",false);
ButtonGroup g=new ButtonGroup();
g.add(ra1);
g.add(ra2);
ra1.setBounds(130,200,75,25);
ra2.setBounds(220,200,75,25);
add(ra1);
add(ra2);
JLabel lab5=new JLabel("Qualification: ");
lab5.setBounds(50,240,200,25);
add(lab5);
check1=new JCheckBox("Undergraduate",false);
check1.setBounds(130,250,200,25);
check2=new JCheckBox("Graduate",false);
check2.setBounds(130,270,200,25);
check3=new JCheckBox("Postgraduate",false);
check3.setBounds(130,290,200,25);
check4=new JCheckBox("Doctorate",false);
check4.setBounds(130,310,200,25);
add(check1);
add(check2);
add(check3);
add(check4);
ta=new JTextArea(10,20);
ta.setFont(new Font("Times new roman",Font.ITALIC,14));
ta.setLineWrap(true);
JScrollPane p=new JScrollPane(ta,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
p.setBounds(130,360,200,100);
add(p);
bt1=new JButton("Submit");
bt1.setBounds(130,500,75,25);
add(bt1);
bt2=new JButton("Reset");
bt2.setBounds(220,500,75,25);
add(bt2);

//AddItemListener and AddActionListener to components and button
txt2.setText("Enter an Email");

bt1.addActionListener(this);
bt2.addActionListener(this);
addWindowListener(this);
setBackground(Color.yellow);
setVisible(true);
setSize(500,600);
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

/*public void actionPerformed(ActionEvent e)
{
String stAction;
if(stAction=e.getSource()==bt2)
{
System.out.println("Hello");
}
}*/


}
public void windowOpened(WindowEvent e)
{
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowClosed(WindowEvent e)
{
}
public void windowIconified(WindowEvent e)
{
}
public void windowDeiconified(WindowEvent e)
{
}
public void windowActivated(WindowEvent e)
{
}
public void windowDeactivated(WindowEvent e)
{
}
//Cac event cua cac button
public void actionPerformed(ActionEvent e)
{






String stAction1 = e.getActionCommand();
if(stAction1.equals("Reset"))

{
String s1="";
txt1.setText(s1);
// System.out.println(txt1);
txt2.setText(s1);
ta.setText(s1);
}
String stAction2 = e.getActionCommand();
if(stAction1.equals("Submit"))

{

mail=txt2.getText();
int mPos, nPos,kPos,len;boolean ok;
String strPattern = "[^a-z.@_0-9]"; // not include character a->z, @, _ and number digits
boolean findOk;
Pattern p;Matcher m;
int flag = Pattern.CASE_INSENSITIVE;
p = Pattern.compile(strPattern,flag);
m = p.matcher(mail);
findOk = m.find();
ok=true;String strMessage="";
if(findOk==true) {ok = false;}

mPos = mail.indexOf("@");//Vi tri cua @ trong chuoi
nPos = mail.indexOf(".");//Vi tri cua . trong chuoi
kPos = mail.indexOf(" ");//Vi tri cua dau cach trong chuoi
len=mail.length();

if(mPos==-1) //Khong co @
{
ok=false;

}
if(nPos==-1) //Khong co .
{
ok=false;

}

if(nPos>=0 && mPos>nPos) //Ky hieu @ dung sau dau .
{
ok=false;

}
if(mPos==0 || mPos==len-1) //Ky hieu @ dung dau hoac cuoi chuoi.
{
ok=false;

}
if(nPos==0 || nPos==len-1) //Dau . dung dau hoac cuoi chuoi.
{
ok=false;

}
if(!ok)
JOptionPane.showMessageDialog(null,"Email khong hop le", "Email khong hop le", JOptionPane.OK_OPTION);

//strMessage="\nDia chi email khong hop le:\n\n" +strMessage;
else

//Kiem tra cac nut radio button da dc chon chua
if(!(ra1.isSelected() || ra2.isSelected()))
JOptionPane.showMessageDialog(null,"Sex does not chosen", "Sex does not chosen", JOptionPane.OK_OPTION);

//Kiem tra cac nut checkbox da dc chon chua
if(!(check1.isSelected() ||check2.isSelected() || check3.isSelected()||check4.isSelected()))
JOptionPane.showMessageDialog(null,"Qualification does not chosen", "Qualification does not chosen", JOptionPane.OK_OPTION);

String ten = txt1.getText();
String email=txt2.getText();
String sex="";
String q="";
String comm=ta.getText();
String country=(String)com.getSelectedItem();
if(ra1.isSelected())
{

sex=ra1.getText();}

if(ra2.isSelected())
{

sex=ra2.getText();}


if(check1.isSelected()){

q=check1.getText();}

if(check2.isSelected())
{q=check2.getText();
}


if(check3.isSelected())
{ q=check3.getText();
}

if(check4.isSelected())
{
q=check4.getText();
}


try{
FileWriter f = new FileWriter("Guest.txt",true);
BufferedWriter b = new BufferedWriter(f);


b.write("----------------------------------------------------------------------");
b.newLine();
b.write("Guest name: "+ten+"\n");
b.newLine();
b.write("Email:"+email+"\n");
b.newLine();
b.write("Sex: "+sex+"\n");
b.newLine();
b.write("Qualification: "+ q +"\n");
b.newLine();
b.write("Country: "+country+"\n");
b.newLine();
b.write("Comment: "+comm);
b.newLine();
b.write("----------------------------------------------------------------------");
b.newLine();
b.newLine();
b.close();
f.close();

}
catch (FileNotFoundException ex)
{
System.out.println("The requested file cannot be found.");
}
catch (IOException ex)
{
System.out.println("An error occurred while writing to the file.");
}
JOptionPane.showMessageDialog(null,"Saved file to Guest.txt", "Saved file to Guest.txt", JOptionPane.OK_OPTION);
}

}



}

public class AssignmentSwing
{
public static void main(String[] args)
{
new minh();
}
}
Về Đầu Trang Go down
https://c0712i.forumvi.com
 
Bai assignment Djava Guestinfo
Về Đầu Trang 
Trang 1 trong tổng số 1 trang
 Similar topics
-
» Examples va slide Djava
» Examples moi cua Djava thay vua gui! anh em down ve xem
» Bai Djava lam Calculator! Moi nguoi xem tham khao!

Permissions in this forum:Bạn không có quyền trả lời bài viết
C0712I - FPT Aptech :: Tai lieu hoc tap-
Chuyển đến