Hỗ trợ lập trình Java
Hãy đăng nhập để tham gia thảo luận và chia sẻ!
Hỗ trợ lập trình Java
Hãy đăng nhập để tham gia thảo luận và chia sẻ!
Hỗ trợ lập trình Java

Diễn đàn hỏi đáp Java, hướng dẫn bài tập Java, Giúp đỡ về Java


You are not connected. Please login or register

Xem chủ đề cũ hơn Xem chủ đề mới hơn Go down  Thông điệp [Trang 1 trong tổng số 1 trang]

1Bài 2 - Buổi 4 Empty Bài 2 - Buổi 4 Mon Nov 12, 2012 6:02 pm

Admin


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();
    }
}

https://javasupport.forumvi.com

2Bài 2 - Buổi 4 Empty Re: Bài 2 - Buổi 4 Wed Nov 21, 2012 1:16 pm

m1_huy


Vịt mới lớn
Vịt mới lớn
tấn công qua bài 2

show.setPreferredSize(new Dimension(200, 20)); thằng nào có tác dụng gì vậy admin?


-----
final JDialog Birthday = new JDialog();

tại sao phải final, nếu ko xài final thì có sao ko ta?

3Bài 2 - Buổi 4 Empty Re: Bài 2 - Buổi 4 Wed Nov 21, 2012 2:34 pm

Admin


Admin
Admin
Code:
show.setPreferredSize(new Dimension(200, 20));
Đoạn này dùng để set size cho panel show, vì không set size bằng cách show.setsize(new Dimension(w,h)) được. Bạn test thử nhé.

Code:
final JDialog Birthday = new JDialog();
Khi một inner class sử dụng một local variable bên ngoài class đó thì local variable phải được khai báo là final
Ví dụ:
Tui đặt đoạn này trong class listener
Birthday.setVisible(false);

https://javasupport.forumvi.com

4Bài 2 - Buổi 4 Empty Re: Bài 2 - Buổi 4 Wed Nov 21, 2012 7:32 pm

m1_huy


Vịt mới lớn
Vịt mới lớn
uh, cái vụ final mình thông rồi

còn thằng này:
int code = JOptionPane.showOptionDialog(frame, "Please choose your sex!!!", "Change sex",0, JOptionPane.QUESTION_MESSAGE, null, choose_option, "Male");

tham số theo trình tự thì:
-thằng 0 đóng vai trò gì
-thằng JOptionPane.QUESTION_MESSAGE đóng vai trò gì admin?

5Bài 2 - Buổi 4 Empty Re: Bài 2 - Buổi 4 Wed Nov 21, 2012 7:44 pm

m1_huy


Vịt mới lớn
Vịt mới lớn
lúc birthday mình change, làm sao mình cho hiện cái dòng tựa đề "Change birthday" như cái hình của cô ta, bài của mình còn thiếu cái đó thì phải

6Bài 2 - Buổi 4 Empty Re: Bài 2 - Buổi 4 Wed Nov 21, 2012 9:31 pm

Admin


Admin
Admin
Lúc tạo JDialog thì nó y hệt frame rồi, nên cách thức settitle, setsize,...đề tương tự frame.
Bạn thêm vào dòng sau:
Code:
Birthday.setTitle("Change Birthday");

thân,

https://javasupport.forumvi.com

Sponsored content


Xem chủ đề cũ hơn Xem chủ đề mới hơn Về Đầu Trang  Thông điệp [Trang 1 trong tổng số 1 trang]

Permissions in this forum:
Bạn không có quyền trả lời bài viết