Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. import java.awt.EventQueue;
  2.  
  3. import javax.swing.JFrame;
  4. import javax.swing.JButton;
  5. import javax.swing.JLabel;
  6. import javax.swing.JOptionPane;
  7. import javax.swing.JTextField;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.ActionEvent;
  10.  
  11. public class zad1 {
  12.  
  13. private JFrame frame;
  14. private JTextField textField;
  15. private JLabel lblO;
  16.  
  17. /**
  18. * Launch the application.
  19. */
  20. public static void main(String[] args) {
  21. EventQueue.invokeLater(new Runnable() {
  22. public void run() {
  23. try {
  24. zad1 window = new zad1();
  25. window.frame.setVisible(true);
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. });
  31. }
  32.  
  33. /**
  34. * Create the application.
  35. */
  36. public zad1() {
  37. initialize();
  38. }
  39.  
  40. /**
  41. * Initialize the contents of the frame.
  42. */
  43. private void initialize() {
  44. frame = new JFrame();
  45. frame.setTitle("\u041C\u0438\u043D. \u043E\u0442 \u043D\u0435\u0447\u0435\u0442\u043D\u0438\u0442\u0435");
  46. frame.setBounds(100, 100, 343, 169);
  47. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  48. frame.getContentPane().setLayout(null);
  49.  
  50. JLabel lblM = new JLabel("m:");
  51. lblM.setBounds(158, 11, 46, 14);
  52. frame.getContentPane().add(lblM);
  53.  
  54. textField = new JTextField();
  55. textField.setBounds(118, 36, 91, 20);
  56. frame.getContentPane().add(textField);
  57. textField.setColumns(10);
  58.  
  59. lblO = new JLabel("O\u0442\u0433\u043E\u0432\u043E\u0440:");
  60. lblO.setBounds(118, 111, 197, 20);
  61. frame.getContentPane().add(lblO);
  62.  
  63. JButton button = new JButton("\u0412\u044A\u0432\u0435\u0434\u0438");
  64. button.addActionListener(new ActionListener() {
  65. public void actionPerformed(ActionEvent arg0) {
  66. int m = Integer.parseInt(textField.getText());
  67. if (m<=0){
  68. lblO.setText("Невалидни данни.");
  69. }else{
  70. int[] arr = new int[m];
  71. for (int i = 0;i<m;i++){
  72. arr[i]=Integer.parseInt(JOptionPane.showInputDialog("Число "+(i+1)));
  73. }
  74. int min=Integer.MAX_VALUE;
  75. int count = 0;
  76. for (int i = 0;i<m;i++){
  77. if (arr[i]<min && arr[i]%2!=0){
  78. min = arr[i];
  79. count++;
  80. }
  81. }
  82. if (count!=0){
  83. lblO.setText(Integer.toString(min));
  84. }
  85. else{
  86. lblO.setText("Няма нечетни числа.");
  87. }
  88. }
  89. }
  90. });
  91. button.setBounds(118, 79, 91, 23);
  92. frame.getContentPane().add(button);
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement