Advertisement
DidntRead

Untitled

Jan 20th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.EventQueue;
  3.  
  4. import javax.swing.JFrame;
  5. import javax.swing.JPanel;
  6. import javax.swing.border.EmptyBorder;
  7. import javax.swing.JTextField;
  8. import javax.swing.JLabel;
  9. import javax.swing.JOptionPane;
  10. import javax.swing.JButton;
  11. import java.awt.event.ActionListener;
  12. import java.awt.event.ActionEvent;
  13. import java.awt.event.KeyAdapter;
  14. import java.awt.event.KeyEvent;
  15.  
  16. public class Zadacha2 extends JFrame {
  17.  
  18. private JPanel contentPane;
  19. private JTextField textField;
  20.  
  21. /**
  22. * Launch the application.
  23. */
  24. public static void main(String[] args) {
  25. EventQueue.invokeLater(new Runnable() {
  26. public void run() {
  27. try {
  28. Zadacha2 frame = new Zadacha2();
  29. frame.setVisible(true);
  30. } catch (Exception e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. });
  35. }
  36.  
  37. /**
  38. * Create the frame.
  39. */
  40. public Zadacha2() {
  41. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  42. setBounds(100, 100, 450, 118);
  43. contentPane = new JPanel();
  44. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  45. setContentPane(contentPane);
  46. contentPane.setLayout(null);
  47.  
  48. JLabel lblNewLabel = new JLabel("");
  49. lblNewLabel.setBounds(10, 42, 46, 14);
  50. contentPane.add(lblNewLabel);
  51.  
  52. textField = new JTextField();
  53. textField.addKeyListener(new KeyAdapter() {
  54. @Override
  55. public void keyTyped(KeyEvent e) {
  56. if(e.getKeyChar() == '1' && textField.getText().endsWith(",")) {
  57. String[] num = textField.getText().split(",");
  58. int kratno = 0;
  59. int broi = 0;
  60. for(String n : num) {
  61. int chislo = Integer.valueOf(n);
  62. if(chislo % 4 == 0) {
  63. kratno += chislo;
  64. broi++;
  65. }
  66. }
  67.  
  68. lblNewLabel.setText(broi == 0 ? "Nqma kratni na 4" : String.valueOf(kratno / broi));
  69. }
  70. }
  71. });
  72. textField.setBounds(10, 11, 414, 20);
  73. contentPane.add(textField);
  74. textField.setColumns(10);
  75.  
  76. JButton btnNewButton = new JButton("Calc");
  77. btnNewButton.addActionListener(new ActionListener() {
  78. public void actionPerformed(ActionEvent e) {
  79. int kratno = 0;
  80. int broi = 0;
  81. while(true) {
  82. int chislo = Integer.parseInt(JOptionPane.showInputDialog("Vuvedete chislo"));
  83. if(chislo == 1) break;
  84. if(chislo % 4 == 0) {
  85. kratno += chislo;
  86. broi++;
  87. }
  88. }
  89.  
  90. lblNewLabel.setText(broi == 0 ? "Nqma kratni na 4" : String.valueOf(kratno / broi));
  91. }
  92. });
  93. btnNewButton.setBounds(335, 45, 89, 23);
  94. contentPane.add(btnNewButton);
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement