Advertisement
Guest User

Java

a guest
Sep 23rd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.66 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package JavaGUI_Code;
  7.  
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import javax.swing.*;
  11. import javax.swing.event.DocumentEvent;
  12. import javax.swing.event.DocumentListener;
  13. import javax.swing.text.Document;
  14. import javax.swing.text.Element;
  15.  
  16. /**
  17. *
  18. * @author minhdt
  19. */
  20. public class Lesson1Exercise extends JFrame implements ActionListener, ItemListener,FocusListener
  21. , DocumentListener{
  22. // khai báo các control
  23. private JLabel lblTitle, lblMa, lblTen, lblGender, lblChuc,lblSalary, lblInfo;
  24. private JTextField txtMa, txtName, txtSalary;
  25. private JButton btnOK, btnReset, btnExit;
  26. private JRadioButton rdNam, rdNu;
  27. private JComboBox cboChucVu;
  28. private List lstInfo;
  29. // design GUI application
  30. public Lesson1Exercise() {
  31. super("Bai thuc hanh so 1");
  32. // khởi tạo các control
  33. lblTitle = new JLabel("THONG TIN NHAN VIEN");
  34. lblTitle.setForeground(Color.BLUE);
  35. lblTitle.setFont(new Font("Cambria",Font.BOLD, 25));
  36. Font f1 = new Font("Cambria", Font.BOLD, 20);
  37. lblMa = new JLabel("Ma nhan vien: ");
  38. lblMa.setForeground(Color.blue);
  39. lblMa.setFont(new Font("Cambria", Font.BOLD, 20));
  40.  
  41. lblTen = new JLabel("Ten nhan vien: ");
  42. lblTen.setForeground(Color.blue);
  43. lblTen.setFont(new Font("Cambria", Font.BOLD, 20));
  44.  
  45. lblGender = new JLabel("Gioi tinh: ");
  46. lblGender.setForeground(Color.blue);
  47. lblGender.setFont(new Font("Cambria", Font.BOLD, 20));
  48.  
  49. lblChuc = new JLabel("Chuc vu: ");
  50. lblChuc.setForeground(Color.blue);
  51. lblChuc.setFont(new Font("Cambria", Font.BOLD, 20));
  52.  
  53. lblSalary = new JLabel("Luong: ");
  54. lblSalary.setForeground(Color.blue);
  55. lblSalary.setFont(f1);
  56.  
  57. lblInfo = new JLabel("Thong tin: ");
  58. lblInfo.setForeground(Color.blue);
  59. lblInfo.setFont(f1);
  60.  
  61. txtMa = new JTextField(10);
  62. txtMa.setFont(f1);
  63. txtMa.setForeground(Color.pink);
  64. txtMa.addFocusListener(this);
  65. // sự kiện: if value of text is not null -> reset button
  66. txtMa.getDocument().addDocumentListener(this);
  67.  
  68. txtName = new JTextField(10);
  69. txtName.setFont(f1);
  70. txtName.setForeground(Color.pink);
  71. txtName.addFocusListener(this);
  72. txtName.getDocument().addDocumentListener(this);
  73.  
  74. txtSalary = new JTextField(10);
  75. txtSalary.setFont(f1);
  76. txtSalary.setForeground(Color.pink);
  77. txtSalary.setEnabled(false);
  78. txtSalary.getDocument().addDocumentListener(this);
  79.  
  80.  
  81. btnOK = new JButton("OK");
  82. Font f2 = new Font("Cambria", Font.BOLD + Font.ITALIC, 20);
  83. btnOK.setFont(f2);
  84. btnOK.setForeground(Color.red);
  85. btnOK.addActionListener(this);
  86. btnOK.setEnabled(false); // disable
  87.  
  88. btnReset = new JButton("Reset");
  89. btnReset.setFont(f2);
  90. btnReset.setForeground(Color.red);
  91. btnReset.addActionListener(this);
  92. btnReset.setEnabled(false);
  93.  
  94. btnExit = new JButton("Exit");
  95. btnExit.setFont(f2);
  96. btnExit.setForeground(Color.red);
  97. btnExit.addActionListener(this);
  98. // @Override
  99. // public void actionPerformed(ActionEvent e) {
  100. // if (JOptionPane.showConfirmDialog(null, "Do you want to exit? ", "Alarm", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
  101. // System.exit(0);
  102. // }
  103. // });
  104.  
  105. rdNam = new JRadioButton("Nam");
  106. rdNam.setFont(f1);
  107. rdNu = new JRadioButton("Nu");
  108. rdNu.setFont(f1);
  109.  
  110. // tickbox chỉ tick được 1 trong 2
  111. ButtonGroup gender = new ButtonGroup();
  112. gender.add(rdNam);
  113. gender.add(rdNu);
  114.  
  115. cboChucVu = new JComboBox();
  116. cboChucVu.setFont(f1);
  117. cboChucVu.addItem("Giam doc");
  118. cboChucVu.addItem("Pho Giam Doc");
  119. cboChucVu.addItem("Truong phong");
  120. cboChucVu.addItem("Ke toan");
  121. cboChucVu.addItem("Nhan vien");
  122. cboChucVu.addItem("Bao ve");
  123. cboChucVu.addItemListener(this);
  124. // button Actionlistener: actionperformed
  125. // text field
  126. // check box item listener: itemState changed
  127.  
  128. lstInfo = new List(5);
  129.  
  130. lstInfo.setFont(f1);
  131.  
  132. // bước 2: bố trí vào trong cửa sổ
  133. //2.1: tạo 4 panel để chứa control
  134. JPanel pNorth = new JPanel();
  135. pNorth.add(lblTitle);
  136.  
  137. JPanel pWest = new JPanel();
  138. pWest.setLayout(new GridLayout(5, 1));
  139. JPanel k1 = new JPanel();
  140. k1.add(lblMa);
  141. pWest.add(k1);
  142. JPanel k2 = new JPanel();
  143. k2.add(lblTen);
  144. pWest.add(k2);
  145. JPanel k3 = new JPanel();
  146. k3.add(lblGender);
  147. pWest.add(k3);
  148. JPanel k4 = new JPanel();
  149. k4.add(lblChuc);
  150. pWest.add(k4);
  151. JPanel k5 = new JPanel();
  152. k5.add(lblSalary);
  153. pWest.add(k5);
  154.  
  155. JPanel pEast = new JPanel();
  156. pEast.setLayout(new GridLayout(5, 1));
  157. JPanel p1 = new JPanel();
  158. p1.add(txtMa);
  159. JPanel pName = new JPanel();
  160. pName.add(txtName);
  161. JPanel pCB = new JPanel();
  162. pCB.add(cboChucVu);
  163. JPanel pS = new JPanel();
  164. pS.add(txtSalary);
  165. pEast.add(p1);
  166. pEast.add(pName);
  167. JPanel pGender = new JPanel();
  168. pGender.add(rdNam);
  169. pGender.add(rdNu);
  170. pEast.add(pGender);
  171. pEast.add(pCB);
  172. pEast.add(pS);
  173.  
  174. JPanel pSouth = new JPanel();
  175. pSouth.setLayout(new BorderLayout());
  176. JPanel pButton = new JPanel();
  177. pButton.add(btnOK);
  178. pButton.add(btnReset);
  179. pButton.add(btnExit);
  180. pSouth.add(pButton, BorderLayout.NORTH);
  181. JPanel q1 = new JPanel();
  182. q1.add(lblInfo);
  183. pSouth.add(q1, BorderLayout.WEST);
  184. JPanel q2 = new JPanel();
  185. q2.add(lstInfo);
  186. pSouth.add(q2, BorderLayout.CENTER);
  187. this.add(pNorth,BorderLayout.NORTH);
  188. this.add(pWest,BorderLayout.WEST);
  189. this.add(pEast,BorderLayout.EAST);
  190. this.add(pSouth, BorderLayout.SOUTH);
  191.  
  192. setSize(450, 500);
  193. setVisible(true);
  194. }
  195.  
  196. public static void main(String[] args) {
  197. Lesson1Exercise a = new Lesson1Exercise();
  198. }
  199.  
  200. @Override
  201. public void actionPerformed(ActionEvent e) {
  202. if (e.getSource() == btnOK)
  203. {
  204. lstInfo.add(txtMa.getText());
  205. lstInfo.add(txtName.getText());
  206. if (rdNam.isSelected())
  207. lstInfo.add(rdNam.getText());
  208. else
  209. lstInfo.add(rdNu.getText());
  210. lstInfo.add(cboChucVu.getSelectedItem().toString());
  211. lstInfo.add(txtSalary.getText());
  212. }
  213.  
  214. if (e.getSource() == btnReset)
  215. {
  216. txtMa.setText("");
  217. txtName.setText("");
  218. txtSalary.setText("");
  219. }
  220. }
  221.  
  222. @Override
  223. public void itemStateChanged(ItemEvent e) {
  224. if (cboChucVu.getSelectedIndex() == 0 )
  225. txtSalary.setText("10000000");
  226. if (cboChucVu.getSelectedIndex() == 1 )
  227. txtSalary.setText("7000000");
  228. if (cboChucVu.getSelectedIndex() == 2 )
  229. txtSalary.setText("6000000");
  230. if (cboChucVu.getSelectedIndex() == 3 )
  231. txtSalary.setText("5000000");
  232. if (cboChucVu.getSelectedIndex() == 4 )
  233. txtSalary.setText("4000000");
  234.  
  235. }
  236.  
  237. @Override
  238. public void focusGained(FocusEvent e) {
  239. if (e.getSource() == txtMa)
  240. txtMa.setBackground(Color.green);
  241. if (e.getSource() == txtName)
  242. txtName.setBackground(Color.green);
  243. }
  244.  
  245. @Override
  246. public void focusLost(FocusEvent e) {
  247. if (e.getSource() == txtMa)
  248. txtMa.setBackground(Color.WHITE);
  249. if (e.getSource() == txtName)
  250. txtName.setBackground(Color.white);
  251. }
  252.  
  253. @Override
  254. public void insertUpdate(DocumentEvent e) {
  255. // nếu insert text value thì sự kiện xảy ra
  256.  
  257. if (!txtMa.getText().isEmpty() || !txtName.getText().isEmpty() || !txtSalary.getText().isEmpty())
  258. btnReset.setEnabled(true);
  259. else
  260. btnReset.setEnabled(false);
  261. if (!txtMa.getText().isEmpty() && !txtName.getText().isEmpty() && !txtSalary.getText().isEmpty())
  262. btnOK.setEnabled(true);
  263. else
  264. btnOK.setEnabled(false);
  265.  
  266. }
  267.  
  268. @Override
  269. public void removeUpdate(DocumentEvent e) {
  270. // nếu remove any character, sự kiện xảy ra
  271. if (!txtMa.getText().isEmpty() || !txtName.getText().isEmpty() || !txtSalary.getText().isEmpty())
  272. btnReset.setEnabled(true);
  273. else
  274. btnReset.setEnabled(false);
  275. if (!txtMa.getText().isEmpty() && !txtName.getText().isEmpty() && !txtSalary.getText().isEmpty())
  276. btnOK.setEnabled(true);
  277. else
  278. btnOK.setEnabled(false);
  279.  
  280. }
  281.  
  282. @Override
  283. public void changedUpdate(DocumentEvent e) {
  284. // nếu thay đổi, sự kiện xảy ra
  285.  
  286. }
  287.  
  288.  
  289.  
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement