有一个电话键盘,我想让它成为一个简单的加法计算器

时间:2016-05-23 00:02:51

标签: java

我制作了一个简单的电话键盘,我想更改一个按钮来添加数字。现在,+按钮只是在顶部显示的数字上添加+,但我希望添加输入的数字。就像在计算器上一样,+正是如此。我想将清除按钮更改为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);
}

1 个答案:

答案 0 :(得分:0)

请参阅此链接:Is there an eval() function in Java?

您可以导入javax.script并使用{{1}} eval()方法来评估您的表达式。