K16T1 Nhóm 1
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.


Hãy tưởng tượng, chúng ta đang cùng ở trên một con thuyền, tất cả đều phải cùng chèo để đưa con thuyền đến đích!
 
Trang ChínhGalleryLatest imagesTìm kiếmĐăng kýĐăng Nhập

Thông báo mới nhất, click vào để xem chi tiết: Đăng kí thành viên nhóm network!

Nhạc
Latest topics
» Assignment 02: Simple Calculating
by cambeme 18/10/2011, 23:50

» CHƯƠNG V: HỌC THUYẾT GIÁ TRỊ THẶNG DƯ - Trang 47-hết
by anhkhoamc083 5/10/2011, 08:51

» Bài tập SwingDemo
by cambeme 16/9/2011, 12:22

» ass3 cau 2
by nguyentam 29/5/2011, 10:46

» Assignment 09: Classes
by cambeme 27/5/2011, 17:57

» GROUP EXERCISE 03
by cambeme 24/5/2011, 18:23

» Bản kiểm điểm
by nhoc_vet 20/5/2011, 19:29

» Trình bày GROUP EXERCISE 02
by cambeme 20/5/2011, 00:01

» Khát là gì?
by cambeme 18/5/2011, 22:01

» Assignment 08: Methods
by cambeme 15/5/2011, 23:48

» GROUP EXERCISE 02
by cambeme 11/5/2011, 22:14

» Website nhóm 1
by cambeme 10/5/2011, 20:06

» Giao diện trang chủ
by cambeme 10/5/2011, 13:00

» Cấu hình DHCP RELAY AGENT - cấp cho 2 mạng LAN
by cambeme 9/5/2011, 23:59

» Slide 16
by cambeme 6/5/2011, 23:09

» Slide 15
by cambeme 6/5/2011, 23:08

» Slide 14
by cambeme 6/5/2011, 23:07

» Assignment 07: Control and Flow
by cambeme 6/5/2011, 22:33

» Trình bày GROUP EXERCISE 01
by cambeme 6/5/2011, 00:36

» GROUP EXERCISE 01
by cambeme 5/5/2011, 15:26

» Bai 1
by nguyentam 4/5/2011, 19:24

» cau thu 3
by cambeme 1/5/2011, 15:00

» day la bai so 2
by nguyentam 30/4/2011, 17:06

» chi tiet lam DNS
by nguyentam 30/4/2011, 16:40

» Tài liệu Windows Server 2003
by cambeme 30/4/2011, 08:40

» Phần mềm
by cambeme 29/4/2011, 12:35

» Công cụ tạo máy ảo VMware Workstation 7.1.4 Build 385546 Final
by cambeme 25/4/2011, 20:08

» Đề kiểm tra giữa kỳ Network
by cambeme 15/4/2011, 11:53

» Higher Computing Systems Networking
by cambeme 15/4/2011, 10:40

» Bài tập chương 6
by cambeme 12/4/2011, 17:41

No copy

Share | 
 

 Bài tập SwingDemo

Xem chủ đề cũ hơn Xem chủ đề mới hơn Go down 
Tác giảThông điệp
cambeme
Admin
Admin
cambeme


Giới tính Giới tính : Nam
Cung : Nhân Mã
Con giáp : Mùi
Bài gửi Bài gửi : 174
Điểm Điểm : 5243
Thanks Thanks : 0
Sinh nhật Sinh nhật : 05/12/1991
Ngày gia nhập Ngày gia nhập : 25/03/2011
Tuổi Tuổi : 32
Đến từ Đến từ : Thành phố Hồ Chí minh
Nghề nghiệp/ước mơ Nghề nghiệp/ước mơ : IT
Tâm trạng Tâm trạng : Hưng phắn
Châm ngôn sống Châm ngôn sống : Không có gì quý hơn hột vịt thịt kho!
Cao nhân tắt thở vô phương trị!


Bài tập SwingDemo Empty
Bài gửiTiêu đề: Bài tập SwingDemo   Bài tập SwingDemo I_icon_minitime16/9/2011, 12:22

Bài tập SwingDemo Titleb10 16/9/2011, 12:22 » Bài tập SwingDemo Bài tập SwingDemo Titleb12
Bài 1:
Code:
import javax.swing.*;

public class cSwingEx1 extends JFrame {
    JLabel lblUsername = new JLabel("Name"), lblPassword = new JLabel("Pwd");
    JTextField txtUsername = new JTextField();
    JPasswordField txtPassword = new JPasswordField();
    JButton bttOK = new JButton("OK");
    JButton bttCancel = new JButton("Cancel");

    /*
    * constructor
    */
    public cSwingEx1() {
        // set window title
        setTitle("Swing Demo");
        // set size
        setSize(250, 150);
        // reset null layout
        setLayout(null);
        // add labels to window
        add(lblUsername);
        add(lblPassword);
        // add textfield to window
        add(txtPassword);
        add(txtUsername);
        txtUsername.setHorizontalAlignment(4);
        // add button to window
        add(bttOK);
        add(bttCancel);
        // set location
        lblUsername.setBounds(20, 20, 60, 25);
        lblPassword.setBounds(20, 50, 60, 25);
        txtUsername.setBounds(75, 20, 150, 25);
        txtPassword.setBounds(75, 50, 150, 25);
        bttOK.setBounds(20, 80, 100, 25);
        bttCancel.setBounds(130, 80, 100, 25);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // new window
        cSwingEx1 wMain = new cSwingEx1();
        // end program as closing window.
        wMain.setDefaultCloseOperation(EXIT_ON_CLOSE);
        // show the window
        wMain.setVisible(true);
    }
}
Bài 2:
Code:
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.jws.Oneway;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class cSwingEx2 extends JFrame implements ActionListener {
    JTextField txtNumber = new JTextField();
    JButton btnTranslate = new JButton("trans");
    JTextField txtEnglish = new JTextField();
    /*
    * constructor
    */
    public cSwingEx2() {
        // set window title
        setTitle("Translate");
        // set size
        setSize(162, 192);
        // reset null layout
        setLayout(null);
        // add textfield to window
        add(txtNumber);
        add(txtEnglish);
        // add button to window
        add(btnTranslate);
        // set location
        txtNumber.setBounds(23, 50, 100, 25);
        txtNumber.setHorizontalAlignment(JTextField.RIGHT);
        btnTranslate.setBounds(23, 100, 100, 25);
        btnTranslate.addActionListener(this);
        txtEnglish.setEnabled(false);
        txtEnglish.setBounds(23, 125, 100, 25);
        txtEnglish.setHorizontalAlignment(JTextField.RIGHT);
        txtEnglish.setForeground(Color.BLACK);   
    }

    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        String strVar = txtNumber.getText();
        if (strVar.equals("9")) {
            txtEnglish.setText("NINE");
        } else {
            if (strVar.equals("8")) {
                txtEnglish.setText("EIGHT");
            } else {
                if (strVar.equals("7")) {
                    txtEnglish.setText("SEVEN");
                } else {
                    if (strVar.equals("6")) {
                        txtEnglish.setText("SIX");
                    } else {
                        if (strVar.equals("5")) {
                            txtEnglish.setText("FIVE");
                        } else {
                            if (strVar.equals("4")) {
                                txtEnglish.setText("FOUR");
                            } else {
                                if (strVar.equals("3")) {
                                    txtEnglish.setText("THREE");
                                } else {
                                    if (strVar.equals("2")) {
                                        txtEnglish.setText("TWO");
                                    } else {
                                        if (strVar.equals("1")) {
                                            txtEnglish.setText("ONE");
                                        } else {
                                            txtEnglish.setText("");
                                            txtNumber.setText("");
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // new window
        cSwingEx2 wMain = new cSwingEx2();
        // end program as closing window.
        wMain.setDefaultCloseOperation(EXIT_ON_CLOSE);
        // show the window
        wMain.setVisible(true);
    }

}
Bài 3:
Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class cSwingEx3 extends JFrame implements ActionListener {
    JTextField txtText = new JTextField();
    JTextField txtAnalysis = new JTextField();
    JButton bttAnalyse = new JButton("analyse");
    JButton bttClear = new JButton("clear");
    JLabel lblNoWs = new JLabel("N.o.Ws:");
    JTextArea txtArea = new JTextArea();

    /*
    * constructor
    */
    public cSwingEx3() {
        // set window title
        setTitle("Words Analysis");
        // set size
        setSize(300, 400);
        // reset null layout
        setLayout(null);
        // add textfield to window
        add(txtText);
        add(txtAnalysis);
        // add button to window
        add(bttClear);
        add(bttAnalyse);
        // add label to window
        add(lblNoWs);
        // add textarea to window
        add(txtArea);
        // set location
        txtText.setBounds(15, 35, 270, 25);
        txtText.setHorizontalAlignment(JTextField.RIGHT);
        txtAnalysis.setBounds(160, 105, 125, 25);
        bttClear.setBounds(160, 70, 125, 25);
        bttClear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                txtText.setText("");
                txtArea.setText("");
                txtAnalysis.setText("");
            }
        });
        bttAnalyse.addActionListener(this);
        bttAnalyse.setBounds(15, 70, 125, 25);
        lblNoWs.setBounds(15, 105, 125, 25);
        txtArea.setBounds(15, 140, 265, 300);
    }

    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        String strVar = txtText.getText().trim();
        // First
        String sM = "";
        int iVar = 0;
        for (int i = 0; i < strVar.length(); i++) {
            if (strVar.charAt(i) != ' ') {
                sM = sM + strVar.charAt(i) + "\n";
                iVar++;
            }
        }
        txtArea.setText(sM);
        txtAnalysis.setText("" + iVar);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // new window
        cSwingEx3 wMain = new cSwingEx3();
        // end program as closing window.
        wMain.setDefaultCloseOperation(EXIT_ON_CLOSE);
        // show the window
        wMain.setVisible(true);
    }
}
Bài 4: (Riêng bài này mình làm theo dạng Frame không giống 3 bài kia lắm Very Happy)
Code:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Math;

public class cSwingEx4 extends JFrame implements ActionListener {
    /*
    * constructor
    */
    public cSwingEx4() {
        // add frame to window
        frame = new JFrame("Calculator");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 110);
        frame.setLayout(new FlowLayout());
        // add label to frame
        operation = new JLabel("Chon phep tinh");
        result = new JLabel("Ket qua");
        is = new JLabel("=");
        // add panel to frame
        buttons = new JPanel();
        center = new JPanel();
        // add textfield to frame
        number1 = new JTextField(15);
        number2 = new JTextField(15);
        // add button to frame
        Cong = new JButton("+");
        Tru = new JButton("-");
        Nhan = new JButton("*");
        Chia = new JButton("/");
        sin = new JButton("sinx");
        cos = new JButton("cosx");
        buttons.add(Cong);
        buttons.add(Tru);
        buttons.add(Nhan);
        buttons.add(Chia);
        buttons.add(sin);
        buttons.add(cos);
        center.add(number1);
        center.add(operation);
        center.add(number2);
        center.add(is);
        center.add(result);
        frame.add(center);
        frame.add(buttons);
        frame.setVisible(true);
    }

    /*
    * * Activating action for buttons
    */
    public void activateEvents() {
        Cong.addActionListener(this);
        Tru.addActionListener(this);
        Nhan.addActionListener(this);
        Chia.addActionListener(this);
        sin.addActionListener(this);
        cos.addActionListener(this);
    }

    public void actionPerformed(ActionEvent al) {
        // Verify that the correct values are given
        try {
            Double.parseDouble(number1.getText());
            Double.parseDouble(number2.getText());
            if (al.getSource() == Cong) {
                operation.setText("Cong");
                double pole1 = Double.parseDouble(number1.getText());
                double pole2 = Double.parseDouble(number2.getText());
                double resultd = pole1 + pole2;
                result.setText(Double.toString(resultd));
            }
            if (al.getSource() == Tru) {
                operation.setText("Tru");
                double pole1 = Double.parseDouble(number1.getText());
                double pole2 = Double.parseDouble(number2.getText());
                double resultd = pole1 - pole2;
                result.setText(Double.toString(resultd));
            }
            if (al.getSource() == Nhan) {
                operation.setText("Nhan");
                double pole1 = Double.parseDouble(number1.getText());
                double pole2 = Double.parseDouble(number2.getText());
                double resultd = pole1 * pole2;
                result.setText(Double.toString(resultd));
            }
            if (al.getSource() == Chia) {
                operation.setText("Cong");
                double pole1 = Double.parseDouble(number1.getText());
                double pole2 = Double.parseDouble(number2.getText());
                if (pole2 == 0) {
                    JOptionPane.showMessageDialog(null,
                            "Khong the chia cho 0!", "Error!",
                            JOptionPane.ERROR_MESSAGE);
                    double resultd = 0;
                    result.setText(Double.toString(resultd));
                    return;
                }
                double resultd = pole1 / pole2;
                result.setText(Double.toString(resultd));
            }
            if (al.getSource() == sin) {
                operation.setText("sinx");
                double pole1 = Double.parseDouble(number1.getText());
                double resultd = Math.sin(pole1);
            }
            if (al.getSource() == cos) {
                operation.setText("cosx");
                double pole1 = Double.parseDouble(number1.getText());
                double resultd = Math.cos(pole1);
                result.setText(Double.toString(resultd));
            }
        } catch (NumberFormatException nfe) {
            JOptionPane.showMessageDialog(null, "Xin dien so dung "
                    + "vao ca hai khung", "Error!", JOptionPane.ERROR_MESSAGE);
        }

    }

    private JFrame frame;
    private JPanel center, buttons;
    private JLabel operation, result, is;
    private JTextField number1, number2;
    private JButton Cong, Tru, Nhan, Chia, sin, cos;

    public static void main(String[] args) {
        cSwingEx4 wMain = new cSwingEx4();
        wMain.setDefaultCloseOperation(EXIT_ON_CLOSE);
        wMain.activateEvents();
        wMain.setVisible(false);
    }
}



Bài tập SwingDemo Border11 Bài tập SwingDemo Border14
Về Đầu Trang Go down
https://nhom1.forum-viet.com
 

Bài tập SwingDemo

Xem chủ đề cũ hơn Xem chủ đề mới hơn Về Đầu Trang 
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
K16T1 Nhóm 1 :: Các môn học :: Năm 2 :: Java 2-
Chuyển đến 
Đầu trang
Giữa trang
Cuối trang
Free forum | ©phpBB | Free forum support | Báo cáo lạm dụng | Thảo luận mới nhất