1 Bài 2 - Buổi 4 Mon Nov 12, 2012 6:02 pm
Admin
Admin
- Code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Locale;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.MouseInputAdapter;
import javax.swing.event.MouseInputListener;
public class Bai2 {
JFrame frame;
JLabel avatar;
JLabel name,bd,sex,married,c_name,c_bd,c_sex,c_married,t_name,t_sex,t_married,t_bd,empty;
JPanel field,info,show,option;
int x1,y1,x2,y2,x3,y3;
String[] choose_option = {"Male","Female","Other"};
JLabel mess,note;
JTextField text;
Bai2(){
frame = new JFrame();
frame.setLayout(new FlowLayout());
frame.setTitle("Infomation");
frame.setSize(new Dimension(500, 210));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(250, 200);
avatar = new JLabel();
avatar.setIcon(new ImageIcon("cat.jpg"));
field = new JPanel(new BorderLayout());
info = new JPanel(new GridLayout(4,1,22,22));
show = new JPanel(new GridLayout(4, 1,22,22));
show.setPreferredSize(new Dimension(200, 20));
option = new JPanel(new GridLayout(4, 1,22,22));
//Create show
t_name = new JLabel("");
t_bd = new JLabel("");
t_sex = new JLabel("");
t_married = new JLabel("");
//Create Label
name = new JLabel("Name: ");
bd = new JLabel("Birthday: ");
sex = new JLabel("Sex: ");
married = new JLabel("Married: ");
// Create Change Option
c_name = new JLabel("Change");
c_name.setForeground(Color.blue);
c_bd = new JLabel("Change");
c_bd.setForeground(Color.blue);
c_sex = new JLabel("Change");
c_sex.setForeground(Color.blue);
c_married = new JLabel("Change");
c_married.setForeground(Color.blue);
c_name.addMouseListener(new MouseInputAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int messageType = JOptionPane.INFORMATION_MESSAGE;
String name = JOptionPane.showInputDialog(frame,"What's you name?","Change name", messageType);
if (name!=null && !name.equals("")){
t_name.setText(name);
}
}
});
c_bd.addMouseListener(new MouseInputAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
final JDialog Birthday = new JDialog();
Birthday.setLayout(new FlowLayout());
Birthday.setSize(new Dimension(300, 200));
Birthday.setLocation(300, 300);
mess = new JLabel("Please enter your birthday!");
note = new JLabel(" dd/mm/yy");
text = new JTextField(15);
JButton b = new JButton("OK");
Birthday.add(mess);
Birthday.add(text);
Birthday.add(note);
Birthday.add(b);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String d = text.getText();
t_bd.setText(d);
Birthday.setVisible(false);
}
});
Birthday.setVisible(true);
}
});
c_sex.addMouseListener(new MouseInputAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int messageType = JOptionPane.QUESTION_MESSAGE;
int code = JOptionPane.showOptionDialog(frame, "Please choose your sex!!!", "Change sex",0, JOptionPane.QUESTION_MESSAGE, null, choose_option, "Male");
if (code==0){
t_sex.setText("Male");
}
else
if (code==1)
t_sex.setText("Female");
else
t_sex.setText("Other");
}
});
c_married.addMouseListener(new MouseInputAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int pos = JOptionPane.showConfirmDialog(frame, "Do you get married!", "Change married status", 0);
if (pos==JOptionPane.YES_OPTION)
t_married.setText("Yes");
else
t_married.setText("No");
}
});
//Add info
info.add(name);
info.add(bd);
info.add(sex);
info.add(married);
//Add show
show.add(t_name);
show.add(t_bd);
show.add(t_sex);
show.add(t_married);
//Add option
option.add(c_name);
option.add(c_bd);
option.add(c_sex);
option.add(c_married);
//Add field
field.setBorder(BorderFactory.createTitledBorder("My personal info"));
field.add(info,BorderLayout.WEST);
field.add(show,BorderLayout.CENTER);
field.add(option,BorderLayout.EAST);
frame.add(avatar);
frame.add(field);
frame.setVisible(true);
}
public static void main(String[] args) {
Bai2 obj = new Bai2();
}
}