This question has already been answered:

import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.swing.*; // * Created by 1 on 25.11.2017. public class Calculator extends JFrame { JFrame jframe = new JFrame("ΠšΠ°Π»ΡŒΠΊΡƒΠ»ΡΡ‚ΠΎΡ€"); //Ρ„ΠΎΡ€ΠΌΠ° //Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ ΠΏΠ°Π½Π΅Π»ΠΈ для ΠΊΠ½ΠΎΠΏΠΎΠΊ Π³Ρ€ΠΈΠ΄ лояут(ΠœΠ΅Π½Π΅Π΄ΠΆΠ΅Ρ€ располоТСния) JPanel panel = new JPanel(new GridLayout(4, 4, 5, 5)); JPanel batton_equally = new JPanel(new BorderLayout(1, 1)); //Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ панСль для тСкста JPanel panel_text = new JPanel(new BorderLayout()); //Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ тСкстовоС ΠΏΠΎΠ»Π΅ JTextField jtextfield = new JTextField(20); //Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ ΠΊΠ½ΠΎΠΏΠΊΠΈ JButton bt1 = new JButton("7"); JButton bt2 = new JButton("8"); JButton bt3 = new JButton("9"); JButton bt31 = new JButton("Π‘"); JButton bt4 = new JButton("4"); JButton bt5 = new JButton("5"); JButton bt6 = new JButton("6"); JButton bt61 = new JButton("/"); JButton bt7 = new JButton("1"); JButton bt8 = new JButton("2"); JButton bt9 = new JButton("3"); JButton bt91 = new JButton("*"); JButton bt10 = new JButton("."); JButton bt11 = new JButton("0"); JButton bt12 = new JButton("="); JButton bt13 = new JButton("+"); JButton bt14 = new JButton("-"); public Calculator() { double[] first_value = {0}; double second_value = 0; String[] operation = {"+"}; jframe.setSize(300, 300); //ΠžΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° Ρ‰Π΅Π»Ρ‡ΠΊΠ° Π½Π° систСмной ΠΊΠ½ΠΎΠΏΠΊΠ΅ закрытия ΠΎΠΊΠ½Π° jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jframe.setLocationRelativeTo(null); //? jframe.setLayout(new BorderLayout()); //Π’Ρ‹Ρ€Π°Π²Π½ΠΈΠ²Π°Π½ΠΈΠ΅ тСкста Π² ΠΏΠΎΠ»Π΅ ΠΏΠΎ ΠΏΡ€Π°Π²ΠΎΠΌΡƒ ΠΊΡ€Π°ΡŽ jtextfield.setHorizontalAlignment(JTextField.RIGHT); //Установка ΡˆΡ€ΠΈΡ„Ρ‚Π° ΠΈ Ρ€Π°Π·ΠΌΠ΅Ρ€ тСкстового поля jtextfield.setFont(new Font("Times New Roman", Font.PLAIN, 20)); //Π—Π°Π΄Π°Π΅ΠΌ Π²ΠΈΠ΄ ΠΈ Ρ€Π°Π·ΠΌΠ΅Ρ€ ΡˆΡ€ΠΈΡ„Ρ‚Π° ΠΊΠ½ΠΎΠΏΠΎΠΊ bt31.setFont(new Font("Tahoma", Font.BOLD, 15)); bt61.setFont(new Font("Tahoma", Font.BOLD, 15)); bt91.setFont(new Font("Tahoma", Font.BOLD, 15)); bt10.setFont(new Font("Tahoma", Font.BOLD, 15)); bt12.setFont(new Font("Tahoma", Font.BOLD, 20)); bt13.setFont(new Font("Tahoma", Font.BOLD, 15)); bt14.setFont(new Font("Tahoma", Font.BOLD, 20)); //Π”ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ ΠΊΠ½ΠΎΠΏΠΊΠΈ Π½Π° панСль panel.add(bt1); panel.add(bt2); panel.add(bt3); panel.add(bt31); panel.add(bt4); panel.add(bt5); panel.add(bt6); panel.add(bt61); panel.add(bt7); panel.add(bt8); panel.add(bt9); panel.add(bt91); panel.add(bt10); panel.add(bt11); panel.add(bt13); panel.add(bt14); batton_equally.add(bt12); //Ρ†Π²Π΅Ρ‚ ΠΏΠ°Π½Π΅Π»ΠΈ panel.setBackground(Color.CYAN); //ДобавляСм тСкстовоС ΠΏΠΎΠ»Π΅ Π½Π° панСль panel_text.add(jtextfield, BorderLayout.NORTH); //ДобавляСм панСль тСкста Π½Π° Ρ„ΠΎΡ€ΠΌΡƒ jframe.add(panel_text, BorderLayout.NORTH); //ДобавляСм панСль ΠΊΠ½ΠΎΠΏΠΎΠΊ Π½Π° Ρ„ΠΎΡ€ΠΌΡƒ jframe.add(panel, BorderLayout.CENTER); jframe.add(batton_equally, BorderLayout.SOUTH); //Π’ΠΈΠ΄ΠΈΠΌΠΎΡΡ‚ΡŒ Ρ„ΠΎΡ€ΠΌΡ‹ jframe.setVisible(true); //ΠžΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠΈ события "НаТатиС Π½Π° ΠΊΠ½ΠΎΠΏΠΊΡƒ" //ActionListener - ΡΠ»ΡƒΡˆΠ°Ρ‚Π΅Π»ΡŒ bt1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jtextfield.setText(jtextfield.getText() + "7"); } }); bt2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jtextfield.setText(jtextfield.getText() + "8"); } }); bt3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jtextfield.setText(jtextfield.getText() + "9"); } }); bt4.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jtextfield.setText(jtextfield.getText() + "4"); } }); bt5.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jtextfield.setText(jtextfield.getText() + "5"); } }); bt6.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jtextfield.setText(jtextfield.getText() + "6"); } }); bt7.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jtextfield.setText(jtextfield.getText() + "1"); } }); bt8.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jtextfield.setText(jtextfield.getText() + "2"); } }); bt9.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jtextfield.setText(jtextfield.getText() + "3"); } }); bt10.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jtextfield.setText(jtextfield.getText() + "."); } }); bt11.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jtextfield.setText(jtextfield.getText() + "0"); } }); //ΠŸΡ€ΠΈ Π½Π°ΠΆΠ°Ρ‚ΠΈΠΈ Π½Π° "C" удаляСм послСднюю Ρ†ΠΈΡ„Ρ€Ρƒ bt31.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String temp = jtextfield.getText(); //ΠΎΠ±Ρ€Π°Π±Π°Ρ‚Ρ‹Π²Π°Π΅ΠΌ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅, Ρ‡Ρ‚ΠΎΠ±Ρ‹ Π½Π΅ Π±Ρ‹Π»ΠΎ возмоТности ΡΡ‚Π΅Ρ€Π΅Ρ‚ΡŒ пустоС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ try { jtextfield.setText(temp.substring(0, temp.length() - 1)); } catch (Exception e1) { jtextfield.setText(jtextfield.getText() + "ΠŸΡƒΡΡ‚ΠΎ!"); //jtextfield.setForeground(Color.RED); //bug } } }); // Π Π°Π±ΠΎΡ‚Π°Π΅ΠΌ с ΠΊΠ½ΠΎΠΏΠΊΠ°ΠΌΠΈ ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ // Для ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ арифмСтичСского дСйствия Π·Π°ΠΏΠΎΠΌΠ½ΠΈΡ‚ΡŒ Π΅Π³ΠΎ, ΡΠΎΡ…Ρ€Π°Π½ΠΈΡ‚ΡŒ Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π΅ число Π² ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ first_value ΠΈ // ΡΠ±Ρ€Π°ΡΡ‹Π²Π°Ρ‚ΡŒ число для Π²Π²ΠΎΠ΄Π° Π½ΠΎΠ²ΠΎΠ³ΠΎ bt14.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { first_value[0] = Double.valueOf(jtextfield.getText()); }catch (Exception e1) {} jtextfield.setText(""); operation[0] = "-"; } }); bt61.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { first_value[0] = Double.valueOf(jtextfield.getText()); } catch (Exception e1) {} jtextfield.setText(""); operation[0] = "/"; } }); bt91.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { first_value[0] = Double.valueOf(jtextfield.getText()); }catch (Exception e1) {} jtextfield.setText(""); operation[0] = "*"; } }); bt13.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { first_value[0] = Double.valueOf(jtextfield.getText()); }catch (Exception e1) {} jtextfield.setText(""); operation[0] = "+"; } }); // ΠŸΡ€ΠΈ Π½Π°ΠΆΠ°Ρ‚ΠΈΠΈ Π½Π° Ρ€Π°Π²Π½ΠΎ, считаСм Π²Ρ‚ΠΎΡ€ΠΎΠ΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ с тСкстового поля bt12.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { double secondValue = Double.valueOf(jtextfield.getText()); if ("+".equals(operation[0])) { jtextfield.setText((first_value[0] + secondValue) + ""); } if ("-".equals(operation[0])) { jtextfield.setText((first_value[0] - secondValue) + ""); } if ("/".equals(operation[0])) { jtextfield.setText((first_value[0] / secondValue) + ""); } if ("*".equals(operation[0])) { jtextfield.setText((first_value[0] * secondValue) + ""); } }catch (Exception e1) {} } }); Pattern p = Pattern.compile("[+-]?(?:\\d+(?:\\.\\d*)?|\\.\\d+)(?:[eE][+-]?\\d+)?\\s*([+-\\\\*/])\\s*[+-]?(?:\\d+(?:\\.\\d*)?|\\.\\d+)(?:[eE][+-]?\\d+)?$"); Matcher m = p.matcher(jtextfield.setText(jtextfield.getText())); return m.matches(); } public static void main(String[] args) { new Calculator(); } } 

Reported as a duplicate member of the Spirit Community ♦ Dec 24 '17 at 8:59 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

1 answer 1

^ (\ d +) ((\. {0,1}) (\ d +))? $ like this probably will go

Add a line button in the text field to the button handler.

 bt10.addActionListener(new ActionListener() { String text; @Override public void actionPerformed(ActionEvent e) { text = jtextfield.getText(); if(!text.contains(".")){ jtextfield.setText(jtextfield.getText() + "."); } } }); 

Here with regex:

 bt10.addActionListener(new ActionListener() { String text; String newText; @Override public void actionPerformed(ActionEvent e) { text = jtextfield.getText(); newText = jtextfield.getText() + "."; if(validinput(newText)){ jtextfield.setText(newText); } } }); private boolean validinput(String input){ Pattern p = Pattern.compile("^(\\d+)(\\.{0,1})?$"); return p.matcher(input).find(); } 
  • The expression has been corrected, but still it is possible to enter several points. Maybe something needs to be added to the cycle? - Alex Lip
  • If you enter more than one point, the text will not match the regular schedule. You can check: regex101.com - Nikolay Belyakov
  • I completed my answer - Nikolay Belyakov
  • Well, in general, so too, the result is not what happened. I still want to use regular expressions. - Alex Lip
  • I again changed the answer, this time I checked it - it works. - Nikolay Belyakov