是的,我知道之前已经提出过这个问题;但是,我们执行这些功能的方法是不同的,因此我认为这不应该被标记为重复。
我正在尝试制作计算器GUI功能,但目前我只能让用户点击一个数字,然后将该数字打印到屏幕上。
当我点击除法功能(这是我目前唯一设置的功能)时,屏幕上会打印出“1”。显然这不是一个错误,因为我得到用户的输入(点击的数字)两次而没有改变。这个数字除以它(这基本上就是我现在所做的)是1。
这是我目前的代码:
package my.calculator;
public class CalculatorUI extends javax.swing.JFrame
{
public CalculatorUI()
{
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
Output = new javax.swing.JTextField();
One = new javax.swing.JButton();
Two = new javax.swing.JButton();
Three = new javax.swing.JButton();
Four = new javax.swing.JButton();
Five = new javax.swing.JButton();
Six = new javax.swing.JButton();
Seven = new javax.swing.JButton();
Eight = new javax.swing.JButton();
Nine = new javax.swing.JButton();
Zero = new javax.swing.JButton();
Divide = new javax.swing.JButton();
Multiply = new javax.swing.JButton();
Subtract = new javax.swing.JButton();
Add = new javax.swing.JButton();
Clear = new javax.swing.JButton();
Calc = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
Output.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
OutputPropertyChange(evt);
}
});
One.setText("1");
One.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
OneActionPerformed(evt);
}
});
Two.setText("2");
Two.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
TwoActionPerformed(evt);
}
});
Three.setText("3");
Three.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ThreeActionPerformed(evt);
}
});
Four.setText("4");
Four.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
FourActionPerformed(evt);
}
});
Five.setText("5");
Five.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
FiveActionPerformed(evt);
}
});
Six.setText("6");
Six.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SixActionPerformed(evt);
}
});
Seven.setText("7");
Seven.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SevenActionPerformed(evt);
}
});
Eight.setText("8");
Eight.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
EightActionPerformed(evt);
}
});
Nine.setText("9");
Nine.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
NineActionPerformed(evt);
}
});
Zero.setText("0");
Zero.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ZeroActionPerformed(evt);
}
});
Divide.setText("/");
Divide.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
DivideActionPerformed(evt);
}
});
Multiply.setText("X");
Subtract.setText("-");
Add.setText("+");
Clear.setText("C");
Clear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ClearActionPerformed(evt);
}
});
Calc.setText("=");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Output)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(One)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Two)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Three, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(Four)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Five)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Six, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(Seven)
.addComponent(Clear))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(Eight)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Nine, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(Zero)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Calc)))))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(Add)
.addGap(0, 0, Short.MAX_VALUE))
.addComponent(Subtract, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addGap(4, 4, 4)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Multiply, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Divide, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(Output, 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.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(Three, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(One)
.addComponent(Two))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Four)
.addComponent(Five)
.addComponent(Six)
.addComponent(Multiply))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Seven)
.addComponent(Eight)
.addComponent(Nine)
.addComponent(Subtract))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Zero)
.addComponent(Add)
.addComponent(Calc)
.addComponent(Clear)))
.addComponent(Divide, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 0, Short.MAX_VALUE))))
);
pack();
}// </editor-fold>
private void OneActionPerformed(java.awt.event.ActionEvent evt) {
Output.setText(String.valueOf(1));
}
private void ClearActionPerformed(java.awt.event.ActionEvent evt) {
Output.setText(String.valueOf(""));
}
private void TwoActionPerformed(java.awt.event.ActionEvent evt) {
Output.setText(String.valueOf(2));
}
private void ThreeActionPerformed(java.awt.event.ActionEvent evt) {
Output.setText(String.valueOf(3));
}
private void FourActionPerformed(java.awt.event.ActionEvent evt) {
Output.setText(String.valueOf(4));
}
private void FiveActionPerformed(java.awt.event.ActionEvent evt) {
Output.setText(String.valueOf(5));
}
private void SixActionPerformed(java.awt.event.ActionEvent evt) {
Output.setText(String.valueOf(6));
}
private void SevenActionPerformed(java.awt.event.ActionEvent evt) {
Output.setText(String.valueOf(7));
}
private void EightActionPerformed(java.awt.event.ActionEvent evt) {
Output.setText(String.valueOf(8));
}
private void NineActionPerformed(java.awt.event.ActionEvent evt) {
Output.setText(String.valueOf(9));
}
private void ZeroActionPerformed(java.awt.event.ActionEvent evt) {
Output.setText(String.valueOf(0));
}
private void DivideActionPerformed(java.awt.event.ActionEvent evt) {
int num1 = Integer.parseInt(Output.getText());
int number = Integer.parseInt(Output.getText());
int calculate = num1 / number;
Output.setText(Integer.toString(calculate));
}
private void OutputPropertyChange(java.beans.PropertyChangeEvent evt) {
Output.setEditable(false); // TODO add your handling code here:
}
public static void main(String args[])
{
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(CalculatorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(CalculatorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(CalculatorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(CalculatorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CalculatorUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton Add;
private javax.swing.JButton Calc;
private javax.swing.JButton Clear;
private javax.swing.JButton Divide;
private javax.swing.JButton Eight;
private javax.swing.JButton Five;
private javax.swing.JButton Four;
private javax.swing.JButton Multiply;
private javax.swing.JButton Nine;
private javax.swing.JButton One;
private javax.swing.JTextField Output;
private javax.swing.JButton Seven;
private javax.swing.JButton Six;
private javax.swing.JButton Subtract;
private javax.swing.JButton Three;
private javax.swing.JButton Two;
private javax.swing.JButton Zero;
// End of variables declaration
}
我还想要包含一个十进制函数(这样你可以有双打等),但是我不知道如何这样做。非常感谢有关这两个问题的任何和所有帮助。
谢谢。
答案 0 :(得分:0)
我知道这很诱人,但我认为计算器(即使只是基本算术)也不像初学者那样微不足道。你会怎么样解决像3 + 3 * 2这样的表达式?你需要的初学者是一个Parser / Tokenizer,一个中间格式,如波兰表示法和一个算法,以获得正确的顺序,例如shanting yard算法。