使用同一jTextfield中的两位数来执行计算

时间:2011-02-09 23:51:45

标签: java

我已完成基本计算器作为作业的一部分。 我目前正在使用两个文本字段进入opperands。然后我按下操作员显示答案。

但是,两个整数都应该从同一个文本域输入,那么我该怎么做呢?

我考虑过使用数组或堆栈,但我无法让它工作。

我在下面包含了我的代码...计算器准备就绪,一切正常,它会进行乘法除法加法和减法,并在文本区域显示答案,要添加或减去的两个数字......键入两个单独的字段,一旦按下opperand计算完成(反向波兰表示法),但是,如何消除第二个字段的使用,只需在文本字段中输入第一个值,按Enter然后插入第二个值进入同一文本字段并按Enter键。将第一个保存为valA,将第二个保存为val B(整数)。但它必须是相同的文本域。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Calcu.java
 *
 * Created on Feb 9, 2011, 10:11:37 PM
 */

/**
 *
 * @author Halaseh
 */
public class Calcu extends javax.swing.JFrame {

    /** Creates new form Calcu */
    public Calcu() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });

        jButton1.setText("+");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("-");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("*");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("/");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jTextField2, javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jButton3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jButton1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton2))))
                .addContainerGap(222, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jButton4))
                .addContainerGap(69, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int valA = Integer.parseInt(jTextField1.getText());
int valB = Integer.parseInt(jTextField2.getText());        // TODO add your handling code here:

        int valC = valA+valB;
        jTextArea1.append(Integer.toString(valC));
        jTextArea1.append("\n");


    }

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {

 int valA = Integer.parseInt(jTextField1.getText());
    }

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
int valB = Integer.parseInt(jTextField2.getText());        // TODO add your handling code here:
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
int valA = Integer.parseInt(jTextField1.getText());
int valB = Integer.parseInt(jTextField2.getText());        // TODO add your handling code here:

        int valC = valA-valB;
        jTextArea1.append(Integer.toString(valC));
        jTextArea1.append("\n");        // TODO add your handling code here:
    }

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
int valA = Integer.parseInt(jTextField1.getText());
int valB = Integer.parseInt(jTextField2.getText());        // TODO add your handling code here:

        int valC = valA*valB;
        jTextArea1.append(Integer.toString(valC));
        jTextArea1.append("\n");        // TODO add your handling code here:
    }

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
int valA = Integer.parseInt(jTextField1.getText());
int valB = Integer.parseInt(jTextField2.getText());        // TODO add your handling code here:

        int valC = valA/valB;
        jTextArea1.append(Integer.toString(valC));
        jTextArea1.append("\n");        // TODO add your handling code here:
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Calcu().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration

}

4 个答案:

答案 0 :(得分:2)

您必须读入输入并以某种方式解析字符串。

如果您只需要1 + 3或4 * 5等形式的东西,您可以通过询问是否存在任何操作符来检查操作

if(inputString.indexOf("+") >= 0) { 
    //handle addition
}

然后你可以使用以下方法获得这两个数字:

String[] numbers = inputString.split("+");
Integer numberOne = Integer.parseInt(numbers[0]);
...

这做了很多假设,一般来说你应该检查潜在的问题,但这只取决于项目的指定方式。

答案 1 :(得分:0)

包括提交按钮或侦听要按下的回车键,让程序从文本字段中读取数字并将其存储在变量中。然后你可以在其他地方显示输入的号码?

不完全确定你的作业需要什么,但我会考虑让用户输入整个等式(“1 + 2”)然后解析它。

答案 2 :(得分:0)

在输入完整字符串后解析整个字符串将是最好的,以防用户改变主意。尝试将其转换为char数组,然后查找单个或连续的数字和操作数。

答案 3 :(得分:0)

可悲的是,我还不能评论..

但是要在jTextField中侦听回车键,请使用以下代码:

EntryBox.addKeyListener(
            new KeyAdapter() {

                public void keyPressed(KeyEvent e) {
                    int key = e.getKeyCode();
                    if (key == KeyEvent.VK_ENTER) {
                        ActionToPerform(null);
                    }
                }
            });

EntryBox是我的jTextField,而ActionToPerform是在触发Enter键时调用的方法。在ActionToPerform中,您可以将EntryBox中的文本/数字复制到变量中。另外,我建议用

结束ActionToPerform
EntryBox.setText("");

这会清除文本框,因此可以接受另一个号码。