Вывести диалоговое окно, если в строке появилось определенное значение? - Java

Узнай цену своей работы

Формулировка задачи:

Здравствуйте! Нужна помощь!Я программирую калькулятор и мне стало интересно каким образом можно вывести диалоговое окно, если в JTextField появилась, допустим цифра 30? Я пытался сделать вот так, но ничего не происходит, не пойму почему. Буду признателен за помощь)
Листинг программы
  1. import javax.swing.*;
  2. import javax.swing.event.AncestorEvent;
  3. import javax.swing.event.AncestorListener;
  4. import java.awt.*;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.*;
  8. public class Calculator extends JFrame {
  9. JTextField display = new JTextField(); //дисплей калькулятора
  10. Calculator() {
  11. super("Calculator");
  12. this.setBounds(350, 350, 350, 350);
  13. this.setLayout(new BorderLayout());
  14. add(display, BorderLayout.NORTH);
  15. this.setVisible(true);
  16. this.setResizable(false);
  17. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18. this.setLocationRelativeTo(null);
  19. display.addActionListener(new ActionListener() {
  20. @Override
  21. public void actionPerformed(ActionEvent e) {
  22. display.setText(firstValue + "");
  23. if (firstValue == 30){
  24. display.getText();
  25. JOptionPane.showMessageDialog(null, "Цифра 30");
  26. }
  27. }
  28. }
Полный код.
Листинг программы
  1. package com.company;
  2. import javax.swing.*;
  3. import javax.swing.event.AncestorEvent;
  4. import javax.swing.event.AncestorListener;
  5. import java.awt.*;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.awt.event.*;
  9. public class Calculator extends JFrame {
  10. int firstValue = 0;
  11. String operation = "+";
  12. JTextField display = new JTextField(); //дисплей калькулятора
  13. JPanel buttonPanel = new JPanel(new GridLayout(6, 0));
  14. JButton button0 = new JButton("0");//кнопка 0
  15. JButton button1 = new JButton("1");//кнопка 1
  16. JButton button2 = new JButton("2");//кнопка 2
  17. JButton button3 = new JButton("3");//кнопка 3
  18. JButton button4 = new JButton("4");//кнопка 4
  19. JButton button5 = new JButton("5");//кнопка 5
  20. JButton button6 = new JButton("6");//кнопка 6
  21. JButton button7 = new JButton("7");//кнопка 7
  22. JButton button8 = new JButton("8");//кнопка 8
  23. JButton button9 = new JButton("9");//кнопка 9
  24. JButton buttonSum = new JButton("+");//кнопка +
  25. JButton buttonSub = new JButton("-");//кнопка -
  26. JButton buttonDivide = new JButton("/");//кнопка /
  27. JButton buttonMul = new JButton("*");//кнопка *
  28. JButton buttonBack = new JButton("в†ђ");//кнопка BackSpace
  29. JButton buttonClear = new JButton("C");//кнопка С
  30. JButton buttonStart = new JButton("=");//кнопка =
  31. Calculator() {
  32. super("Calculator");
  33. this.setBounds(350, 350, 350, 350);
  34. this.setLayout(new BorderLayout());
  35. add(display, BorderLayout.NORTH);
  36. add(buttonPanel, BorderLayout.CENTER);
  37. add(buttonRud, BorderLayout.SOUTH);
  38. buttonPanel.add(button0);
  39. buttonPanel.add(button1);
  40. buttonPanel.add(button2);
  41. buttonPanel.add(button3);
  42. buttonPanel.add(button4);
  43. buttonPanel.add(button5);
  44. buttonPanel.add(button6);
  45. buttonPanel.add(button7);
  46. buttonPanel.add(button8);
  47. buttonPanel.add(button9);
  48. buttonPanel.add(buttonSum);
  49. buttonPanel.add(buttonSub);
  50. buttonPanel.add(buttonMul);
  51. buttonPanel.add(buttonBack);
  52. buttonPanel.add(buttonDivide);
  53. buttonPanel.add(buttonClear);
  54. buttonPanel.add(buttonStart);
  55. this.setVisible(true);
  56. this.setResizable(false);
  57. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  58. this.setLocationRelativeTo(null);
  59. display.addActionListener(new ActionListener() {
  60. @Override
  61. public void actionPerformed(ActionEvent e) {
  62. display.setText(firstValue + "");
  63. if (firstValue == 30){
  64. display.getText();
  65. JOptionPane.showMessageDialog(null, "Цифра 30");
  66. }
  67. }
  68. });
  69. button0.addActionListener(new ActionListener() {
  70. @Override
  71. public void actionPerformed(ActionEvent e) {
  72. display.setText(display.getText() + "0");
  73. }
  74. });
  75. button1.addActionListener(new ActionListener() {
  76. @Override
  77. public void actionPerformed(ActionEvent e) {
  78. display.setText(display.getText() + "1");
  79. }
  80. });
  81. button2.addActionListener(new ActionListener() {
  82. @Override
  83. public void actionPerformed(ActionEvent e) {
  84. display.setText(display.getText() + "2");
  85. }
  86. });
  87. button3.addActionListener(new ActionListener() {
  88. @Override
  89. public void actionPerformed(ActionEvent e) {
  90. display.setText(display.getText() + "3");
  91. }
  92. });
  93. button4.addActionListener(new ActionListener() {
  94. @Override
  95. public void actionPerformed(ActionEvent e) {
  96. display.setText(display.getText() + "4");
  97. }
  98. });
  99. button5.addActionListener(new ActionListener() {
  100. @Override
  101. public void actionPerformed(ActionEvent e) {
  102. display.setText(display.getText() + "5");
  103. }
  104. });
  105. button6.addActionListener(new ActionListener() {
  106. @Override
  107. public void actionPerformed(ActionEvent e) {
  108. display.setText(display.getText() + "6");
  109. }
  110. });
  111. button7.addActionListener(new ActionListener() {
  112. @Override
  113. public void actionPerformed(ActionEvent e) {
  114. display.setText(display.getText() + "7");
  115. }
  116. });
  117. button8.addActionListener(new ActionListener() {
  118. @Override
  119. public void actionPerformed(ActionEvent e) {
  120. display.setText(display.getText() + "8");
  121. }
  122. });
  123. button9.addActionListener(new ActionListener() {
  124. @Override
  125. public void actionPerformed(ActionEvent e) {
  126. display.setText(display.getText() + "9");
  127. }
  128. });
  129. buttonBack.addActionListener(new ActionListener() {
  130. @Override
  131. public void actionPerformed(ActionEvent e) {
  132. String temp = display.getText();
  133. display.setText(temp.substring(0, temp.length() - 1));
  134. }
  135. });
  136. buttonClear.addActionListener(new ActionListener() {
  137. @Override
  138. public void actionPerformed(ActionEvent e) {
  139. display.setText(null);
  140. }
  141. });
  142. buttonSum.addActionListener(new ActionListener() {
  143. @Override
  144. public void actionPerformed(ActionEvent e) {
  145. firstValue = Integer.valueOf(display.getText());
  146. display.setText("");
  147. operation = "+";
  148. }
  149. });
  150. buttonSub.addActionListener(new ActionListener() {
  151. @Override
  152. public void actionPerformed(ActionEvent e) {
  153. firstValue = Integer.valueOf(display.getText());
  154. display.setText("");
  155. operation = "-";
  156. }
  157. });
  158. buttonMul.addActionListener(new ActionListener() {
  159. @Override
  160. public void actionPerformed(ActionEvent e) {
  161. firstValue = Integer.valueOf(display.getText());
  162. display.setText("");
  163. operation = "*";
  164. }
  165. });
  166. buttonDivide.addActionListener(new ActionListener() {
  167. @Override
  168. public void actionPerformed(ActionEvent e) {
  169. firstValue = Integer.valueOf(display.getText());
  170. display.setText("");
  171. operation = "/";
  172. }
  173. });
  174. buttonStart.addActionListener(new ActionListener() {
  175. @Override
  176. public void actionPerformed(ActionEvent e) {
  177. int secondValue = Integer.valueOf(display.getText());
  178. if ("+".equals(operation)) {
  179. display.setText((firstValue + secondValue) + "");
  180. }
  181. }
  182. });
  183. buttonStart.addActionListener(new ActionListener() {
  184. @Override
  185. public void actionPerformed(ActionEvent e) {
  186. int secondValue = Integer.valueOf(display.getText());
  187. if ("-".equals(operation)) {
  188. display.setText((firstValue - secondValue) + "");
  189. }
  190. }
  191. });
  192. buttonStart.addActionListener(new ActionListener() {
  193. @Override
  194. public void actionPerformed(ActionEvent e) {
  195. int secondValue = Integer.valueOf(display.getText());
  196. if ("*".equals(operation)) {
  197. display.setText((firstValue * secondValue) + "");
  198. }
  199. }
  200. });
  201. buttonStart.addActionListener(new ActionListener() {
  202. @Override
  203. public void actionPerformed(ActionEvent e) {
  204. int secondValue = Integer.valueOf(display.getText());
  205. if ("/".equals(operation)) {
  206. display.setText((firstValue / secondValue) + "");
  207. }
  208. }
  209. });
  210. }
  211. public static void main(String[] args) {
  212. new Calculator();
  213. }
  214. }

Решение задачи: «Вывести диалоговое окно, если в строке появилось определенное значение?»

textual
Листинг программы
  1.             display.getDocument().addDocumentListener(new DocumentListener() {
  2.            
  3.             @Override
  4.             public void removeUpdate(DocumentEvent e) {
  5.                 // TODO Auto-generated method stub
  6.                
  7.             }
  8.            
  9.             @Override
  10.             public void insertUpdate(DocumentEvent e) {
  11.                 //System.out.println("bbb: " + display.getText());
  12.                 if (display.getText().equals("30")){
  13.                     JOptionPane.showMessageDialog(null, "Цифра 30");
  14.                 }
  15.                
  16.             }
  17.            
  18.             @Override
  19.             public void changedUpdate(DocumentEvent e) {
  20.                 // TODO Auto-generated method stub
  21.                
  22.             }
  23.         });

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

7   голосов , оценка 4.429 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы