Java: Demo-Calculator-V1
Giải phóng thời gian, khai phóng năng lực
Code demo chương trình tạo máy tính bỏ túi.
package swing1;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.*;
/**
*
* @author LongDT
*/
public class Lab2 extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public Lab2() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
hienThi = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
s1 = new javax.swing.JButton();
s2 = new javax.swing.JButton();
s3 = new javax.swing.JButton();
div = new javax.swing.JButton();
jButton19 = new javax.swing.JButton();
s4 = new javax.swing.JButton();
s5 = new javax.swing.JButton();
s6 = new javax.swing.JButton();
mul = new javax.swing.JButton();
jButton13 = new javax.swing.JButton();
s7 = new javax.swing.JButton();
s8 = new javax.swing.JButton();
s9 = new javax.swing.JButton();
sub = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
s0 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
add = new javax.swing.JButton();
equal = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
hienThi.setBackground(new java.awt.Color(255, 255, 255));
hienThi.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
hienThi.setForeground(new java.awt.Color(255, 0, 0));
hienThi.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
hienThi.setText("0");
hienThi.setMaximumSize(new java.awt.Dimension(521, 398));
hienThi.setMinimumSize(new java.awt.Dimension(521, 398));
hienThi.setPreferredSize(new java.awt.Dimension(521, 398));
jPanel1.setLayout(new java.awt.GridLayout(4, 5));
s1.setText("1");
s1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
s1ActionPerformed(evt);
}
});
jPanel1.add(s1);
s2.setText("2");
s2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
s2ActionPerformed(evt);
}
});
jPanel1.add(s2);
s3.setText("3");
s3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
s3ActionPerformed(evt);
}
});
jPanel1.add(s3);
div.setText("/");
div.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
divActionPerformed(evt);
}
});
jPanel1.add(div);
jButton19.setText("sqrt");
jPanel1.add(jButton19);
s4.setText("4");
s4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
s4ActionPerformed(evt);
}
});
jPanel1.add(s4);
s5.setText("5");
s5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
s5ActionPerformed(evt);
}
});
jPanel1.add(s5);
s6.setText("6");
s6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
s6ActionPerformed(evt);
}
});
jPanel1.add(s6);
mul.setText("*");
mul.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mulActionPerformed(evt);
}
});
jPanel1.add(mul);
jButton13.setText("%");
jPanel1.add(jButton13);
s7.setText("7");
s7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
s7ActionPerformed(evt);
}
});
jPanel1.add(s7);
s8.setText("8");
s8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
s8ActionPerformed(evt);
}
});
jPanel1.add(s8);
s9.setText("9");
s9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
s9ActionPerformed(evt);
}
});
jPanel1.add(s9);
sub.setText("-");
sub.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
subActionPerformed(evt);
}
});
jPanel1.add(sub);
jButton3.setText("1/x");
jPanel1.add(jButton3);
s0.setText("0");
s0.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
s0ActionPerformed(evt);
}
});
jPanel1.add(s0);
jButton8.setText("+/-");
jPanel1.add(jButton8);
jButton7.setText("C");
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});
jPanel1.add(jButton7);
add.setText("+");
add.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addActionPerformed(evt);
}
});
jPanel1.add(add);
equal.setText("=");
equal.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
equalActionPerformed(evt);
}
});
jPanel1.add(equal);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(hienThi, javax.swing.GroupLayout.PREFERRED_SIZE, 462, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(hienThi, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 307, javax.swing.GroupLayout.PREFERRED_SIZE))
);
pack();
}// </editor-fold>
private void s1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
concatNumber(s1.getText());
}
private void s2ActionPerformed(java.awt.event.ActionEvent evt) {
concatNumber(s2.getText());
}
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
hienThi.setText("0");
}
private void s3ActionPerformed(java.awt.event.ActionEvent evt) {
concatNumber(s3.getText());
}
private void s4ActionPerformed(java.awt.event.ActionEvent evt) {
concatNumber(s4.getText());
}
private void s5ActionPerformed(java.awt.event.ActionEvent evt) {
concatNumber(s5.getText());
}
private void s6ActionPerformed(java.awt.event.ActionEvent evt) {
concatNumber(s6.getText());
}
private void s7ActionPerformed(java.awt.event.ActionEvent evt) {
concatNumber(s7.getText());
}
private void s8ActionPerformed(java.awt.event.ActionEvent evt) {
concatNumber(s8.getText());
}
private void s9ActionPerformed(java.awt.event.ActionEvent evt) {
concatNumber(s9.getText());
}
private void concatNumber(String str) {
if ("0".equals(hienThi.getText())) {
hienThi.setText(str);
} else {
hienThi.setText(hienThi.getText() + str);
}
}
private void s0ActionPerformed(java.awt.event.ActionEvent evt) {
if ("0".equals(hienThi.getText())) {
hienThi.setText(s0.getText());
} else {
hienThi.setText(hienThi.getText() + s0.getText());
}
}
private void addActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
concatStr(add.getText());
}
private void subActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
concatStr(sub.getText());
}
private void mulActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
concatStr(mul.getText());
}
private void divActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
concatStr(div.getText());
}
private void equalActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
s = hienThi.getText();
Object obj = null;
try {
obj = doCalc(s);
} catch (ScriptException ex) {
Logger.getLogger(Lab2.class.getName()).log(Level.SEVERE, null, ex);
}
hienThi.setText(obj.toString());
}
private void concatStr(String operator) {
s = hienThi.getText();
char lastchar = s.charAt(hienThi.getText().length() - 1);
if (!(lastchar >= '0' && lastchar <= '9')) {
s = s.substring(0, s.length() - 1);
}
s = s.concat(operator);
hienThi.setText(s);
}
public Object doCalc(String str) throws ScriptException {
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
return engine.eval(str);
}
String s = "";
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<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 | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Lab2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(() -> {
new Lab2().setVisible(true);
});
}
// Variables declaration - do not modify
private javax.swing.JButton add;
private javax.swing.JButton div;
private javax.swing.JButton equal;
private javax.swing.JLabel hienThi;
private javax.swing.JButton jButton13;
private javax.swing.JButton jButton19;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JPanel jPanel1;
private javax.swing.JButton mul;
private javax.swing.JButton s0;
private javax.swing.JButton s1;
private javax.swing.JButton s2;
private javax.swing.JButton s3;
private javax.swing.JButton s4;
private javax.swing.JButton s5;
private javax.swing.JButton s6;
private javax.swing.JButton s7;
private javax.swing.JButton s8;
private javax.swing.JButton s9;
private javax.swing.JButton sub;
// End of variables declaration
}
Giải phóng thời gian, khai phóng năng lực