我目前正在使用GUI开发计算器。
这就是我的意思,以及它应该如何工作。
例如,用户单击:123.45 + 456.2 = 1”。屏幕应如下所示:
123.45+
456.2 =由用户输入
579.65由您的程序计算并显示
这就是我希望计算器显示以前的输入并在单击数学运算符后转到新行的方式。请注意,我也尝试过追加,但是没有用。
代码:
const express = require('express');
const config = require('./config/config');
const TelegramBot = require('node-telegram-bot-api');
const bodyParser = require('body-parser');
require('./config/configDB');
const app = express();
const bot = new TelegramBot (config.TOKEN);
bot.setWebHook(config.URL);
const dictionary = require('./controllers/dictionaryControllers')(bot);
app.listen(3000);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));
app.post('/', (request, response)=>{
let req = request.body;
response.send().status(200);
bot.processUpdate(req);
});
const start = /\/start/;
bot.onText(start, dictionary.start);
这是我的计算器的外观。
计算器:
关于如何显示上面示例中的输出的任何帮助。
谢谢。
答案 0 :(得分:0)
由于赋值运算符,您的代码将仅返回您的答案。尝试使用多个变量来存储您的值,然后使用“ \ n”在想要中断的位置断开它们。
答案 1 :(得分:0)
您希望数字只要在0到9之间就行,对吗?这样就可以了。与其创建10个JButton对象,不如创建一个JButton对象数组并使用循环对其进行初始化,然后向其添加操作侦听器。
private JButton[] buttons;
buttons = new JButton[10];
for(int i = 0; i < buttons.length; i++) {
// Make all the buttons and add them to the panel
panel1.add(buttons[i] = new JButton(String.valueOf(i)));
// Add an actionlistener to each of them
buttons[i].addActionListener(this);
}
以下是将actionListener接口用于这些按钮的方式(请确保首先在您的CalculatorFrame类中实现它):
@Override
public void actionPerformed(ActionEvent e) {
for(int i = 0; i < buttons.length; i++) {
// Check if the button pressed was a button0 - button 9
if(e.getSource() == buttons[i]) {
// whichever button (0-9) was pressed, append its result into display string
display += String.valueOf(i);
}
}
// Now set the result into your text area
textArea.setText(display);
}
现在,每次按下按钮时,它都会在同一行而不是新行中,因为您没有直接更改textArea的值,而是在其中放置了一个字符串,每次都会附加您按下一个按钮。
因此,最初,显示变量的值为空。当您按1时,它将变为1,并显示在textArea中。现在,当您按2时,显示的值将变为display = display +“ 2”。这次,当您将显示变量传递给textArea时,它不仅会丢失值,因为不会直接对其进行编辑。
您可以使用此逻辑来修复其他方法。同样,由于所有值都是字符串,因此要执行计算,您将需要将字符串转换为整数。在这种情况下,您可以使用Integer.valueOf(display)。
希望这会有所帮助。
答案 2 :(得分:0)
display = textArea.getText();
textArea.setText(display + "3");
Don't use getText() and setText(). It is not very efficient.
Every time you use getText() the text area needs to parse the Document to create a string. Every time you use setText() the text area needs to parse the String to create the Document.
Instead you just use:
textArea.append( "3" );
Even better yet, don't use custom listeners for each button. You can share a generic listener for the number buttons. See: How to add a shortcut key for a jbutton in java?的原始域,以帮助您入门。
我希望计算器显示以前的输入并在单击数学运算符后转到新行。
然后,附加ActionListener(例如)将执行以下操作:
textArea.append( "+\n" );
您需要为所有操作员执行此操作。
答案 3 :(得分:0)
要在如上所述的操作之后显示文本,可以通过在setText方法中添加一个必要的文本来完成此操作:textArea.setText(display +“ + n由用户输入\ n”)或类似的东西。但这不会帮助您获得结果,并且会收到错误消息。为什么?因为您在读取第二个变量“ equalTemp = Double.parseDouble(textArea.getText())”时遇到问题。实际上,此方法会删除textArea中显示的所有值/字符/等,因此会给您一条错误消息,因为不可能将它们全部转换为Double格式。要解决此问题,您必须更改输入值的保存方式。例如,您可以将textArea中的所有文本转换为String,然后使用特定的符号split()将其保存,然后将剩余的值另存为Double。
class BlinkingTextFieldVC: UIViewController {
var blinkingTextField: UITextField!
override func onViewDidLoad() {
setupView()
}
func setupView() {
blinkingTextField = UITextField()
blinkingTextField.inputView = UIView() // empty view will be shown as input method
blinkingTextField.becomeFirstResponder()
let tapGuesture = UITapGestureRecognizer(target: self, action: #selector(blinkingTextFieldTapped(_:)))
blinkingTextField.addGestureRecognizer(tapGuesture)
}
func blinkingTextFieldTapped(_ gesture: UITapGestureRecognizer) {
if gesture.state == .ended {
view.endEditing(true)
blinkingTextField.inputView = nil // set your custom input view or nil for default keyboard
blinkingTextField.becomeFirstResponder()
}
}
}
例如,您将得到下一个结果为9.25和23.7的总和:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.util.regex.PatternSyntaxException;
class CalculatorFrame extends JFrame {
/**
* All the buttons that will be used in the calculator have been initialized
*/
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
private JButton button5;
private JButton button6;
private JButton button7;
private JButton button8;
private JButton button9;
private JButton button0;
private JButton buttonEqual;
private JButton buttonDot;
private JButton buttonClearLast;
private JButton buttonClearAll;
private JButton buttonAdd;
private JButton buttonSub;
private JButton buttonMul;
private JButton buttonDiv;
private JTextArea textArea;
private JScrollPane scrollPane;
String display = "";
String[] arr;
private double result;
Boolean additionBoolean = false;
Boolean subtractionBoolean = false;
Boolean multiplicationBoolean = false;
Boolean divisionBoolean = false;
Boolean equals = false;
public CalculatorFrame(){
JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayout(1,1));
panel2.add(buttonClearLast = new JButton ("Clear Last"));
panel2.add(buttonClearAll = new JButton ("Clear All"));
add(panel2, BorderLayout.PAGE_START);
JPanel panel3 = new JPanel();
textArea = new JTextArea(10, 20);
scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
add(scrollPane);
add(panel3, BorderLayout.AFTER_LAST_LINE);
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(4,4));
panel1.add(button7 = new JButton ("7"));
panel1.add(button8 = new JButton ("8"));
panel1.add(button9 = new JButton ("9"));
panel1.add(buttonAdd = new JButton ("+"));
panel1.add(button4 = new JButton ("4"));
panel1.add(button5 = new JButton ("5"));
panel1.add(button6 = new JButton ("6"));
panel1.add(buttonSub = new JButton ("-"));
panel1.add(button1 = new JButton ("1"));
panel1.add(button2 = new JButton ("2"));
panel1.add(button3 = new JButton ("3"));
panel1.add(buttonMul = new JButton ("*"));
panel1.add(button0 = new JButton ("0"));
panel1.add(buttonDot = new JButton ("."));
panel1.add(buttonEqual = new JButton ("="));
panel1.add(buttonDiv = new JButton ("/"));
add(panel1, BorderLayout.PAGE_END);
pack();
// buttonClearLast.addActionListener(new ListenToClearLast());
buttonClearAll.addActionListener(new ListenToClearAll());
button1.addActionListener(e -> {textArea.setText(display() + "1");});
button2.addActionListener(e -> {textArea.setText(display() + "2");});
button3.addActionListener(e -> {textArea.setText(display() + "3");});
button4.addActionListener(e -> {textArea.setText(display() + "4");});
button5.addActionListener(e -> {textArea.setText(display() + "5");});
button6.addActionListener(e -> {textArea.setText(display() + "6");});
button7.addActionListener(e -> {textArea.setText(display() + "7");});
button8.addActionListener(e -> {textArea.setText(display() + "8");});
button9.addActionListener(e -> {textArea.setText(display() + "9");});
button0.addActionListener(e -> {textArea.setText(display() + "0");});
buttonAdd.addActionListener(e -> {textArea.setText(display() + "+ enterd by user\n"); additionBoolean = true;});
buttonSub.addActionListener(e -> {textArea.setText(display() + "- enterd by user\n"); subtractionBoolean = true;});
buttonMul.addActionListener(e -> {textArea.setText(display() + "* enterd by user\n"); multiplicationBoolean = true;});
buttonDiv.addActionListener(e -> {textArea.setText(display() + "/ enterd by user\n"); divisionBoolean = true;});
buttonDot.addActionListener(e -> {textArea.setText(display() + ".");});
buttonEqual.addActionListener(e -> {calculation();
textArea.setText(display() + "= enterd by user\n" + result + " this is your result");
});
}
private String display() {
display = textArea.getText();
return display;
}
private void calculation() {
String str = display();
if (additionBoolean == true) {
arr = str.split("\\+ enterd by user");
result = Double.parseDouble(arr[0]) + Double.parseDouble(arr[1]);
}
else if (subtractionBoolean == true) {
arr = str.split("- enterd by user");
result = Double.parseDouble(arr[0]) - Double.parseDouble(arr[1]);
}
else if (multiplicationBoolean == true) {
arr = str.split("\\* enterd by user");
result = Double.parseDouble(arr[0]) * Double.parseDouble(arr[1]);
}
else if (divisionBoolean == true) {
arr = str.split("/ enterd by user");
result = Double.parseDouble(arr[0]) / Double.parseDouble(arr[1]);
}
}
/**
* This is where the action listener listens to all the button being pressed
* Once heard, it will show case it to the TextArea of the calculator.
*/
public class ListenToClearAll implements ActionListener{
public void actionPerformed (ActionEvent e){
textArea.setText("");
additionBoolean = false;
subtractionBoolean = false;
multiplicationBoolean = false;
divisionBoolean = false;
}
}
}