namereq

電卓:8桁制限

Aug 6th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.03 KB | None | 0 0
  1. package dentaku;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Dimension;
  5. import java.awt.GridLayout;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.math.BigDecimal;
  9.  
  10. import javax.swing.JButton;
  11. import javax.swing.JFrame;
  12. import javax.swing.JPanel;
  13. import javax.swing.JTextField;
  14.  
  15. public class DentakuFrame extends JFrame {
  16.     private static final long serialVersionUID = 1L;
  17.  
  18.     JPanel contentPane = new JPanel();
  19.     BorderLayout borderLayout1 = new BorderLayout();
  20.  
  21.     JTextField result = new JTextField("");
  22.  
  23.     double stackedValue = 0.0;
  24.     BigDecimal bd = new BigDecimal(stackedValue);
  25.     boolean isStacked = false;
  26.     boolean afterCalc = false;
  27.     String currentOp = "";
  28.  
  29.  
  30.     public DentakuFrame() {
  31.         contentPane.setLayout(borderLayout1);
  32.         this.setSize(new Dimension(300, 320));
  33.         this.setTitle("電卓");
  34.         this.setContentPane(contentPane);
  35.  
  36.         result.setHorizontalAlignment(JTextField.RIGHT);
  37.         contentPane.add(result, BorderLayout.NORTH);
  38.  
  39.         JPanel keyPanel = new JPanel();
  40.         keyPanel.setLayout(new GridLayout(4, 4));
  41.         contentPane.add(keyPanel, BorderLayout.CENTER);
  42.  
  43.         keyPanel.add(new NumberButton("7"), 0);
  44.         keyPanel.add(new NumberButton("8"), 1);
  45.         keyPanel.add(new NumberButton("9"), 2);
  46.         keyPanel.add(new CalcButton("+"), 3);
  47.         keyPanel.add(new NumberButton("4"), 4);
  48.         keyPanel.add(new NumberButton("5"), 5);
  49.         keyPanel.add(new NumberButton("6"), 6);
  50.         keyPanel.add(new CalcButton("-"), 7);
  51.         keyPanel.add(new NumberButton("1"), 8);
  52.         keyPanel.add(new NumberButton("2"), 9);
  53.         keyPanel.add(new NumberButton("3"), 10);
  54.         keyPanel.add(new CalcButton("×"), 11);
  55.         keyPanel.add(new NumberButton("0"), 12);
  56.         keyPanel.add(new CalcButton("="), 13);
  57.         keyPanel.add(new CalcButton("÷"), 14);
  58.         keyPanel.add(new CalcButton(""), 15);
  59.  
  60.         contentPane.add(new ClearButton(), BorderLayout.SOUTH);
  61.         this.setVisible(true);
  62.     }
  63.  
  64.  
  65.     public void appendResult(String c) {
  66.         if (!afterCalc) {
  67.             if (Integer.valueOf(result.getText() + c) <= 99999999) {
  68.                 result.setText(Integer.valueOf(result.getText() + c).toString());
  69.             }
  70.         } else {
  71.             result.setText(c);
  72.             afterCalc = false;
  73.         }
  74.     }
  75.  
  76.  
  77.     public class NumberButton extends JButton implements ActionListener {
  78.         private static final long serialVersionUID = 1L;
  79.  
  80.         public NumberButton(String keyTop) {
  81.             super(keyTop);
  82.             this.addActionListener(this);
  83.         }
  84.  
  85.         public void actionPerformed(ActionEvent evt) {
  86.             String keyNumber = this.getText();
  87.             appendResult(keyNumber);
  88.         }
  89.     }
  90.  
  91.     public class CalcButton extends JButton implements ActionListener {
  92.         private static final long serialVersionUID = 1L;
  93.  
  94.         public CalcButton(String op) {
  95.             super(op);
  96.             this.addActionListener(this);
  97.         }
  98.  
  99.         public void actionPerformed(ActionEvent e) {
  100.             if (isStacked) {
  101.                 double resultValue = (Double.valueOf(result.getText()))
  102.                 .doubleValue();
  103.  
  104.                 if (currentOp.equals("+"))
  105.                     stackedValue += resultValue;
  106.                 else if (currentOp.equals("-"))
  107.                     stackedValue -= resultValue;
  108.                 else if (currentOp.equals("×"))
  109.                     stackedValue *= resultValue;
  110.                 else if (currentOp.equals("÷"))
  111.                     stackedValue /= resultValue;
  112.  
  113.                 if((int)stackedValue - stackedValue == 0){
  114.                     result.setText(String.valueOf((int)stackedValue));
  115.                 }else{
  116.                     result.setText(String.valueOf((int)Math.round(stackedValue)));
  117.                 }
  118.             }
  119.             currentOp = this.getText();
  120.             stackedValue = (Double.valueOf(result.getText())).doubleValue();
  121.             if(stackedValue > 99999999) {
  122.                 stackedValue = 99999999;
  123.                 result.setText(String.valueOf((int)stackedValue));
  124.             }
  125.             afterCalc = true;
  126.             if (currentOp.equals("="))
  127.                 isStacked = false;
  128.             else
  129.                 isStacked = true;
  130.         }
  131.     }
  132.  
  133.     public class ClearButton extends JButton implements ActionListener {
  134.  
  135.         private static final long serialVersionUID = 1L;
  136.  
  137.         public ClearButton() {
  138.             super("C");
  139.             this.addActionListener(this);
  140.         }
  141.  
  142.         public void actionPerformed(ActionEvent evt) {
  143.             stackedValue = 0.0;
  144.             afterCalc = false;
  145.             isStacked = false;
  146.             result.setText("");
  147.         }
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment