Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package JavaGUI_Code;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.event.DocumentEvent;
- import javax.swing.event.DocumentListener;
- import javax.swing.text.Document;
- import javax.swing.text.Element;
- /**
- *
- * @author minhdt
- */
- public class Lesson1Exercise extends JFrame implements ActionListener, ItemListener,FocusListener
- , DocumentListener{
- // khai báo các control
- private JLabel lblTitle, lblMa, lblTen, lblGender, lblChuc,lblSalary, lblInfo;
- private JTextField txtMa, txtName, txtSalary;
- private JButton btnOK, btnReset, btnExit;
- private JRadioButton rdNam, rdNu;
- private JComboBox cboChucVu;
- private List lstInfo;
- // design GUI application
- public Lesson1Exercise() {
- super("Bai thuc hanh so 1");
- // khởi tạo các control
- lblTitle = new JLabel("THONG TIN NHAN VIEN");
- lblTitle.setForeground(Color.BLUE);
- lblTitle.setFont(new Font("Cambria",Font.BOLD, 25));
- Font f1 = new Font("Cambria", Font.BOLD, 20);
- lblMa = new JLabel("Ma nhan vien: ");
- lblMa.setForeground(Color.blue);
- lblMa.setFont(new Font("Cambria", Font.BOLD, 20));
- lblTen = new JLabel("Ten nhan vien: ");
- lblTen.setForeground(Color.blue);
- lblTen.setFont(new Font("Cambria", Font.BOLD, 20));
- lblGender = new JLabel("Gioi tinh: ");
- lblGender.setForeground(Color.blue);
- lblGender.setFont(new Font("Cambria", Font.BOLD, 20));
- lblChuc = new JLabel("Chuc vu: ");
- lblChuc.setForeground(Color.blue);
- lblChuc.setFont(new Font("Cambria", Font.BOLD, 20));
- lblSalary = new JLabel("Luong: ");
- lblSalary.setForeground(Color.blue);
- lblSalary.setFont(f1);
- lblInfo = new JLabel("Thong tin: ");
- lblInfo.setForeground(Color.blue);
- lblInfo.setFont(f1);
- txtMa = new JTextField(10);
- txtMa.setFont(f1);
- txtMa.setForeground(Color.pink);
- txtMa.addFocusListener(this);
- // sự kiện: if value of text is not null -> reset button
- txtMa.getDocument().addDocumentListener(this);
- txtName = new JTextField(10);
- txtName.setFont(f1);
- txtName.setForeground(Color.pink);
- txtName.addFocusListener(this);
- txtName.getDocument().addDocumentListener(this);
- txtSalary = new JTextField(10);
- txtSalary.setFont(f1);
- txtSalary.setForeground(Color.pink);
- txtSalary.setEnabled(false);
- txtSalary.getDocument().addDocumentListener(this);
- btnOK = new JButton("OK");
- Font f2 = new Font("Cambria", Font.BOLD + Font.ITALIC, 20);
- btnOK.setFont(f2);
- btnOK.setForeground(Color.red);
- btnOK.addActionListener(this);
- btnOK.setEnabled(false); // disable
- btnReset = new JButton("Reset");
- btnReset.setFont(f2);
- btnReset.setForeground(Color.red);
- btnReset.addActionListener(this);
- btnReset.setEnabled(false);
- btnExit = new JButton("Exit");
- btnExit.setFont(f2);
- btnExit.setForeground(Color.red);
- btnExit.addActionListener(this);
- // @Override
- // public void actionPerformed(ActionEvent e) {
- // if (JOptionPane.showConfirmDialog(null, "Do you want to exit? ", "Alarm", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
- // System.exit(0);
- // }
- // });
- rdNam = new JRadioButton("Nam");
- rdNam.setFont(f1);
- rdNu = new JRadioButton("Nu");
- rdNu.setFont(f1);
- // tickbox chỉ tick được 1 trong 2
- ButtonGroup gender = new ButtonGroup();
- gender.add(rdNam);
- gender.add(rdNu);
- cboChucVu = new JComboBox();
- cboChucVu.setFont(f1);
- cboChucVu.addItem("Giam doc");
- cboChucVu.addItem("Pho Giam Doc");
- cboChucVu.addItem("Truong phong");
- cboChucVu.addItem("Ke toan");
- cboChucVu.addItem("Nhan vien");
- cboChucVu.addItem("Bao ve");
- cboChucVu.addItemListener(this);
- // button Actionlistener: actionperformed
- // text field
- // check box item listener: itemState changed
- lstInfo = new List(5);
- lstInfo.setFont(f1);
- // bước 2: bố trí vào trong cửa sổ
- //2.1: tạo 4 panel để chứa control
- JPanel pNorth = new JPanel();
- pNorth.add(lblTitle);
- JPanel pWest = new JPanel();
- pWest.setLayout(new GridLayout(5, 1));
- JPanel k1 = new JPanel();
- k1.add(lblMa);
- pWest.add(k1);
- JPanel k2 = new JPanel();
- k2.add(lblTen);
- pWest.add(k2);
- JPanel k3 = new JPanel();
- k3.add(lblGender);
- pWest.add(k3);
- JPanel k4 = new JPanel();
- k4.add(lblChuc);
- pWest.add(k4);
- JPanel k5 = new JPanel();
- k5.add(lblSalary);
- pWest.add(k5);
- JPanel pEast = new JPanel();
- pEast.setLayout(new GridLayout(5, 1));
- JPanel p1 = new JPanel();
- p1.add(txtMa);
- JPanel pName = new JPanel();
- pName.add(txtName);
- JPanel pCB = new JPanel();
- pCB.add(cboChucVu);
- JPanel pS = new JPanel();
- pS.add(txtSalary);
- pEast.add(p1);
- pEast.add(pName);
- JPanel pGender = new JPanel();
- pGender.add(rdNam);
- pGender.add(rdNu);
- pEast.add(pGender);
- pEast.add(pCB);
- pEast.add(pS);
- JPanel pSouth = new JPanel();
- pSouth.setLayout(new BorderLayout());
- JPanel pButton = new JPanel();
- pButton.add(btnOK);
- pButton.add(btnReset);
- pButton.add(btnExit);
- pSouth.add(pButton, BorderLayout.NORTH);
- JPanel q1 = new JPanel();
- q1.add(lblInfo);
- pSouth.add(q1, BorderLayout.WEST);
- JPanel q2 = new JPanel();
- q2.add(lstInfo);
- pSouth.add(q2, BorderLayout.CENTER);
- this.add(pNorth,BorderLayout.NORTH);
- this.add(pWest,BorderLayout.WEST);
- this.add(pEast,BorderLayout.EAST);
- this.add(pSouth, BorderLayout.SOUTH);
- setSize(450, 500);
- setVisible(true);
- }
- public static void main(String[] args) {
- Lesson1Exercise a = new Lesson1Exercise();
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == btnOK)
- {
- lstInfo.add(txtMa.getText());
- lstInfo.add(txtName.getText());
- if (rdNam.isSelected())
- lstInfo.add(rdNam.getText());
- else
- lstInfo.add(rdNu.getText());
- lstInfo.add(cboChucVu.getSelectedItem().toString());
- lstInfo.add(txtSalary.getText());
- }
- if (e.getSource() == btnReset)
- {
- txtMa.setText("");
- txtName.setText("");
- txtSalary.setText("");
- }
- }
- @Override
- public void itemStateChanged(ItemEvent e) {
- if (cboChucVu.getSelectedIndex() == 0 )
- txtSalary.setText("10000000");
- if (cboChucVu.getSelectedIndex() == 1 )
- txtSalary.setText("7000000");
- if (cboChucVu.getSelectedIndex() == 2 )
- txtSalary.setText("6000000");
- if (cboChucVu.getSelectedIndex() == 3 )
- txtSalary.setText("5000000");
- if (cboChucVu.getSelectedIndex() == 4 )
- txtSalary.setText("4000000");
- }
- @Override
- public void focusGained(FocusEvent e) {
- if (e.getSource() == txtMa)
- txtMa.setBackground(Color.green);
- if (e.getSource() == txtName)
- txtName.setBackground(Color.green);
- }
- @Override
- public void focusLost(FocusEvent e) {
- if (e.getSource() == txtMa)
- txtMa.setBackground(Color.WHITE);
- if (e.getSource() == txtName)
- txtName.setBackground(Color.white);
- }
- @Override
- public void insertUpdate(DocumentEvent e) {
- // nếu insert text value thì sự kiện xảy ra
- if (!txtMa.getText().isEmpty() || !txtName.getText().isEmpty() || !txtSalary.getText().isEmpty())
- btnReset.setEnabled(true);
- else
- btnReset.setEnabled(false);
- if (!txtMa.getText().isEmpty() && !txtName.getText().isEmpty() && !txtSalary.getText().isEmpty())
- btnOK.setEnabled(true);
- else
- btnOK.setEnabled(false);
- }
- @Override
- public void removeUpdate(DocumentEvent e) {
- // nếu remove any character, sự kiện xảy ra
- if (!txtMa.getText().isEmpty() || !txtName.getText().isEmpty() || !txtSalary.getText().isEmpty())
- btnReset.setEnabled(true);
- else
- btnReset.setEnabled(false);
- if (!txtMa.getText().isEmpty() && !txtName.getText().isEmpty() && !txtSalary.getText().isEmpty())
- btnOK.setEnabled(true);
- else
- btnOK.setEnabled(false);
- }
- @Override
- public void changedUpdate(DocumentEvent e) {
- // nếu thay đổi, sự kiện xảy ra
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement