Вывести диалоговое окно, если в строке появилось определенное значение? - Java
Формулировка задачи:
Здравствуйте! Нужна помощь!Я программирую калькулятор и мне стало интересно каким образом можно вывести диалоговое окно, если в JTextField появилась, допустим цифра 30?
Я пытался сделать вот так, но ничего не происходит, не пойму почему. Буду признателен за помощь)
Полный код.
Листинг программы
- import javax.swing.*;
- import javax.swing.event.AncestorEvent;
- import javax.swing.event.AncestorListener;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.*;
- public class Calculator extends JFrame {
- JTextField display = new JTextField(); //дисплей калькулятора
- Calculator() {
- super("Calculator");
- this.setBounds(350, 350, 350, 350);
- this.setLayout(new BorderLayout());
- add(display, BorderLayout.NORTH);
- this.setVisible(true);
- this.setResizable(false);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setLocationRelativeTo(null);
- display.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- display.setText(firstValue + "");
- if (firstValue == 30){
- display.getText();
- JOptionPane.showMessageDialog(null, "Цифра 30");
- }
- }
- }
Листинг программы
- package com.company;
- import javax.swing.*;
- import javax.swing.event.AncestorEvent;
- import javax.swing.event.AncestorListener;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.*;
- public class Calculator extends JFrame {
- int firstValue = 0;
- String operation = "+";
- JTextField display = new JTextField(); //дисплей калькулятора
- JPanel buttonPanel = new JPanel(new GridLayout(6, 0));
- JButton button0 = new JButton("0");//кнопка 0
- JButton button1 = new JButton("1");//кнопка 1
- JButton button2 = new JButton("2");//кнопка 2
- JButton button3 = new JButton("3");//кнопка 3
- JButton button4 = new JButton("4");//кнопка 4
- JButton button5 = new JButton("5");//кнопка 5
- JButton button6 = new JButton("6");//кнопка 6
- JButton button7 = new JButton("7");//кнопка 7
- JButton button8 = new JButton("8");//кнопка 8
- JButton button9 = new JButton("9");//кнопка 9
- JButton buttonSum = new JButton("+");//кнопка +
- JButton buttonSub = new JButton("-");//кнопка -
- JButton buttonDivide = new JButton("/");//кнопка /
- JButton buttonMul = new JButton("*");//кнопка *
- JButton buttonBack = new JButton("в†ђ");//кнопка BackSpace
- JButton buttonClear = new JButton("C");//кнопка С
- JButton buttonStart = new JButton("=");//кнопка =
- Calculator() {
- super("Calculator");
- this.setBounds(350, 350, 350, 350);
- this.setLayout(new BorderLayout());
- add(display, BorderLayout.NORTH);
- add(buttonPanel, BorderLayout.CENTER);
- add(buttonRud, BorderLayout.SOUTH);
- buttonPanel.add(button0);
- buttonPanel.add(button1);
- buttonPanel.add(button2);
- buttonPanel.add(button3);
- buttonPanel.add(button4);
- buttonPanel.add(button5);
- buttonPanel.add(button6);
- buttonPanel.add(button7);
- buttonPanel.add(button8);
- buttonPanel.add(button9);
- buttonPanel.add(buttonSum);
- buttonPanel.add(buttonSub);
- buttonPanel.add(buttonMul);
- buttonPanel.add(buttonBack);
- buttonPanel.add(buttonDivide);
- buttonPanel.add(buttonClear);
- buttonPanel.add(buttonStart);
- this.setVisible(true);
- this.setResizable(false);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setLocationRelativeTo(null);
- display.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- display.setText(firstValue + "");
- if (firstValue == 30){
- display.getText();
- JOptionPane.showMessageDialog(null, "Цифра 30");
- }
- }
- });
- button0.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- display.setText(display.getText() + "0");
- }
- });
- button1.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- display.setText(display.getText() + "1");
- }
- });
- button2.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- display.setText(display.getText() + "2");
- }
- });
- button3.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- display.setText(display.getText() + "3");
- }
- });
- button4.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- display.setText(display.getText() + "4");
- }
- });
- button5.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- display.setText(display.getText() + "5");
- }
- });
- button6.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- display.setText(display.getText() + "6");
- }
- });
- button7.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- display.setText(display.getText() + "7");
- }
- });
- button8.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- display.setText(display.getText() + "8");
- }
- });
- button9.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- display.setText(display.getText() + "9");
- }
- });
- buttonBack.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- String temp = display.getText();
- display.setText(temp.substring(0, temp.length() - 1));
- }
- });
- buttonClear.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- display.setText(null);
- }
- });
- buttonSum.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- firstValue = Integer.valueOf(display.getText());
- display.setText("");
- operation = "+";
- }
- });
- buttonSub.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- firstValue = Integer.valueOf(display.getText());
- display.setText("");
- operation = "-";
- }
- });
- buttonMul.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- firstValue = Integer.valueOf(display.getText());
- display.setText("");
- operation = "*";
- }
- });
- buttonDivide.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- firstValue = Integer.valueOf(display.getText());
- display.setText("");
- operation = "/";
- }
- });
- buttonStart.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- int secondValue = Integer.valueOf(display.getText());
- if ("+".equals(operation)) {
- display.setText((firstValue + secondValue) + "");
- }
- }
- });
- buttonStart.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- int secondValue = Integer.valueOf(display.getText());
- if ("-".equals(operation)) {
- display.setText((firstValue - secondValue) + "");
- }
- }
- });
- buttonStart.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- int secondValue = Integer.valueOf(display.getText());
- if ("*".equals(operation)) {
- display.setText((firstValue * secondValue) + "");
- }
- }
- });
- buttonStart.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- int secondValue = Integer.valueOf(display.getText());
- if ("/".equals(operation)) {
- display.setText((firstValue / secondValue) + "");
- }
- }
- });
- }
- public static void main(String[] args) {
- new Calculator();
- }
- }
Решение задачи: «Вывести диалоговое окно, если в строке появилось определенное значение?»
textual
Листинг программы
- display.getDocument().addDocumentListener(new DocumentListener() {
- @Override
- public void removeUpdate(DocumentEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void insertUpdate(DocumentEvent e) {
- //System.out.println("bbb: " + display.getText());
- if (display.getText().equals("30")){
- JOptionPane.showMessageDialog(null, "Цифра 30");
- }
- }
- @Override
- public void changedUpdate(DocumentEvent e) {
- // TODO Auto-generated method stub
- }
- });
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д