簡體   English   中英

有一個電話鍵盤,我想使其成為一個簡單的加法計算器

[英]Have a phone keypad and I want to make it a simple addition calculator

我做了一個簡單的電話鍵盤,我想更改一個按鈕以添加數字。 現在, +按鈕只是在頂部顯示的數字中添加+ ,但是我希望添加輸入的數字。 就像在計算器上一樣, +也一樣。 我想將清除按鈕更改為enter按鈕。 到目前為止,這是我的伴侶的代碼,我編寫了鍵盤:

public class Keypad extends JPanel implements ActionListener {

JLabel display;
JButton numButton;
JButton clearButton;
String displayContent = "";
String[] numPadContent = {"1","2","3","4","5","6","7","8","9","+","0"};
ArrayList<JButton> buttonList;

// Keypad constructor class
public Keypad(Container pane) {
    // sets the size of the Keypad display
    pane.setPreferredSize(new Dimension(320, 335));

    // initialize display to hold displayContent
    display = new JLabel(displayContent);
    display.setPreferredSize(new Dimension(320, 25));
    // create lowered bevel border around the display
    display.setBorder(BorderFactory.createLoweredBevelBorder());
    // add the display to the panel
    pane.add(display, BorderLayout.PAGE_START);

    // initialize the buttonList
    buttonList = new ArrayList<JButton>(12);
    JPanel numberPanel = new JPanel();
    // set the numberPanel to have a 4row by 3col grid layout
    numberPanel.setLayout(new GridLayout(4,3,0,0));
    // set the size of the numberPanel
    numberPanel.setPreferredSize(new Dimension(320,260));
    // create the buttons and add them to the buttonList, properly displaying the numbers 
    for (int i = 0; i < numPadContent.length; i++) {
        numButton = new JButton(numPadContent[i]);
        buttonList.add(numButton);
    }
    // add the buttonList to the number panel
    for (int n = 0; n < buttonList.size(); n++) {
        buttonList.get(n).addActionListener(this);
        numberPanel.add(buttonList.get(n));
    }

    // create black border around the number panel
    numberPanel.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, Color.black));
    // add number panel to center part of display
    pane.add(numberPanel, BorderLayout.LINE_END);

    // create Clear button that is actionable
    clearButton = new JButton("Clear");
    clearButton.setPreferredSize(new Dimension(320, 30));
    clearButton.addActionListener(this);
    // add Clear button to bottom of display
    pane.add(clearButton, BorderLayout.PAGE_END);
}
// update the display depending on clicked button(s)
public void actionPerformed(ActionEvent e) {
    String textThere = display.getText();
    String additionalText = "";
    // add clicked number button text to display
    for (int a = 0; a < buttonList.size(); a++) {
        if (e.getSource().equals(buttonList.get(a))) {
            additionalText = buttonList.get(a).getText();
        }
    }

    // clear display if "Clear" button is clicked
    if (e.getSource().equals(clearButton)) {
        textThere = "";
    }
    display.setText(textThere.concat(additionalText));
}

public static void main(String[] args) {

    //create and set up the window.
    JFrame frame = new JFrame("Keypad");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //set up the content pane.
    frame.getContentPane().add(new Keypad(frame));

    frame.pack();
    frame.setVisible(true);
}

查看此鏈接: Java中是否有eval()函數?

您可以導入javax.script並使用ScriptEngine's eval()方法來評估表達式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM