과제의 일부로 기본 계산기를 완료했습니다. 나는 현재 두 개의 텍스트 feild를 사용하여 opperands를 입력하고 있습니다. 연산자를 눌러 답을 표시합니다.동일한 jTextfield의 두 자리를 사용하여 계산 수행
그러나 두 정수는 동일한 텍스트 필드에서 입력해야합니다. 어떻게 처리합니까?
나는 배열이나 스택을 사용하는 것을 고려해 보았지만 작동시키지 못했습니다.
아래에 제 코드가 포함되어 있습니다 ... 계산기가 준비되었으며 모든 것이 잘 작동하고 곱셈 나누기 더하기 및 빼기를 수행하고 텍스트 영역에 답변을 표시합니다.이 두 숫자를 더하거나 뺍니다. opper를 누르고 계산이 완료되면 (Reverse Polish Notation), 두 번째 필드의 사용을 제거하고 텍스트 필드에 첫 번째 값을 입력하고 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
}
오, 나는 오늘 자바를 배우기 시작 했으므로,이 계산기를 처음부터 새로 작성한 것으로 생각합니다.하지만 혼자하는 법을 이해하지 못할 수도 있습니다. 예를 들어 도움이 될 수 있습니다. – Kay