자바 단순 계산기 기능 구현
최근 자바 로 계산 기 를 만 들 었 습 니 다.메뉴 표시 줄 의 기능 은 실현 되 지 않 았 습 니 다.하나의 레이아웃 만 쓰 고 계산 기능 만 실 현 했 습 니 다.메뉴 표시 줄 기능 을 실현 하려 면 이 코드 를 바탕 으로 스스로 실현 할 수 있 습 니 다.
말 이 많 지 않 으 면 바로 코드 를 붙인다.
package test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class calculate extends JFrame implements ActionListener {
private Container ct;
private JButton[] jbs;
private String[] str;
private JTextField jTextField;
//cal[0]
//cal[1] (+,-,*,/,%)
//cal[2]
//cal[3]
//cal[4] MS
//cal[5] ( cal[4] )
private String[] cal = {"","","","","",""};
//_count _count1 , _count true cal[0] cal[0] , cal[2]
// cal[3] cal[0]
private boolean _count = true;
private boolean _count1 = false;
public calculate() {
super(" ");
jbs = new JButton[28];
ct = this.getContentPane();
this.setBounds(100, 100, 315, 400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
ct.setLayout(null);
//
JMenuBar jMenuBar = new JMenuBar();
JMenu jm = new JMenu(" (V)");
JMenu jm1 = new JMenu(" (E)");
JMenu jm2 = new JMenu(" (H)");
jMenuBar.add(jm);
jMenuBar.add(jm1);
jMenuBar.add(jm2);
this.setJMenuBar(jMenuBar);
//
jTextField = new JTextField("0");
jTextField.setFont(new Font(" ", Font.BOLD, 25));
jTextField.setEditable(false);
jTextField.setHorizontalAlignment(JTextField.RIGHT);
jTextField.setBounds(5, 5, 285, 65);
ct.add(jTextField);
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
str = new String[]{"MC", "MR", "MS", "M+", "M-", "←", "CE", "C", "±", "√", "7", "8", "9", "/", "%", "4", "5", "6", "*", "1/x", "1", "2", "3", "-", "=", "0", ".", "+"};
int k = 0;
for (int i = 75; i <= 195; i = i + 40) {
for (int init = 10; init <= 290; init = init + 57) {
JButton jButton = new JButton(str[k]);
jbs[k] = jButton;
jButton.setBounds(init, i, 52, 35);
ct.add(jButton);
k++;
System.out.println(k);
}
}
for (int i = 235; i < 265; i = i + 40) {
for (int j = 10; j <= 181; j = j + 57) {
JButton jButton = new JButton(str[k]);
jButton.setBounds(j, i, 52, 35);
ct.add(jButton);
jbs[k] = jButton;
k++;
System.out.println(k);
}
}
JButton jButton = new JButton(str[k]);
jbs[k] = jButton;
k++;
System.out.println(k);
jButton.setBounds(240, 235, 52, 75);
ct.add(jButton);
JButton jButton2 = new JButton(str[k]);
System.out.println(str[k]);
jButton2.setBounds(10, 275, 109, 35);
ct.add(jButton2);
jbs[k] = jButton2;
k++;
for (int i = 275; i < 310; i = i + 40) {
for (int j = 124; j <= 181; j = j + 57) {
JButton jButton1 = new JButton(str[k]);
jButton1.setBounds(j, i, 52, 35);
ct.add(jButton1);
jbs[k] = jButton1;
k++;
}
}
for (int i = 0; i < 28; i++) {
jbs[i].addActionListener(this);
}
this.setVisible(true);
}
public static void main(String[] args) {
new calculate();
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (actionEvent.getActionCommand().equals(".")){
method(actionEvent);
}else if (actionEvent.getActionCommand().equals("0")){
method(actionEvent);
} else if (actionEvent.getActionCommand().equals("1")){
method(actionEvent);
}else if(actionEvent.getActionCommand().equals("2")){
method(actionEvent);
}else if (actionEvent.getActionCommand().equals("3")){
method(actionEvent);
}else if (actionEvent.getActionCommand().equals("4")){
method(actionEvent);
}else if (actionEvent.getActionCommand().equals("5")){
method(actionEvent);
}else if (actionEvent.getActionCommand().equals("6")){
method(actionEvent);
}else if (actionEvent.getActionCommand().equals("7")){
method(actionEvent);
}else if (actionEvent.getActionCommand().equals("8")){
method(actionEvent);
}else if (actionEvent.getActionCommand().equals("9")){
method(actionEvent);
}else if (actionEvent.getActionCommand().equals("±")){
if(_count){
if (cal[0].contains(".")){
cal[0] = String.valueOf(Double.parseDouble(cal[0]) * (-1));
jTextField.setText(cal[0]);
}else {
cal[0] = String.valueOf(Integer.parseInt(cal[0]) * (-1));
jTextField.setText(cal[0]);
}
}else {
if (cal[2].contains(".")){
cal[2] = String.valueOf(Double.parseDouble(cal[2]) * (-1));
jTextField.setText(cal[2]);
}else {
cal[2] = String.valueOf(Integer.parseInt(cal[2]) * (-1));
jTextField.setText(cal[2]);
}
}
}else if(actionEvent.getActionCommand().equals("√")){
if(_count){
cal[0] = String.valueOf(Math.pow(Double.parseDouble(cal[0]),0.5));
jTextField.setText(cal[0]);
}else {
cal[2] = String.valueOf(Math.pow(Double.parseDouble(cal[2]),0.5));
jTextField.setText(cal[2]);
}
}else if(actionEvent.getActionCommand().equals("1/x")){
if(_count){
cal[0] = String.valueOf(1/Double.parseDouble(cal[0]));
jTextField.setText(cal[0]);
}else {
cal[2] = String.valueOf(1/Double.parseDouble(cal[2]));
jTextField.setText(cal[2]);
}
}else if(actionEvent.getActionCommand().equals("←")){
if(_count){
cal[0] = cal[0].substring(0,cal[0].length()-1);
if(cal[0].equals("")){
jTextField.setText("0");
}else {
jTextField.setText(cal[0]);
}
}else {
cal[2] = cal[2].substring(0,cal[2].length()-1);
if(cal[2].equals("")){
jTextField.setText("0");
}else {
jTextField.setText(cal[2]);
}
}
}else if(actionEvent.getActionCommand().equals("CE")){
if (_count){
cal[0]="";
jTextField.setText("0");
}else {
cal[2]="";
jTextField.setText("0");
}
}else if(actionEvent.getActionCommand().equals("C")){
_count = true;
cal[0]="";
cal[1]="";
cal[2]="";
cal[3]="";
jTextField.setText("0");
}else if (actionEvent.getActionCommand().equals("+")){
if (!cal[0].equals("")&&!cal[2].equals("")){
if (cal[0].contains(".") || cal[2].contains(".")){
cal[0]=String.valueOf(Double.parseDouble(cal[0]) + Double.parseDouble(cal[2]));
}else {
cal[0] = String.valueOf(Integer.parseInt(cal[0]) + Integer.parseInt(cal[2]));
}
cal[2]="";
}
cal[1] = "+";
_count=false;
jTextField.setText(cal[1]);
}else if (actionEvent.getActionCommand().equals("-")){
if (!cal[0].equals("")&&!cal[2].equals("")){
if (cal[0].contains(".") || cal[2].contains(".")){
cal[0]=String.valueOf(Double.parseDouble(cal[0]) - Double.parseDouble(cal[2]));
}else {
cal[0] = String.valueOf(Integer.parseInt(cal[0]) - Integer.parseInt(cal[2]));
}
cal[2]="";
}
cal[1] = "-";
_count=false;
jTextField.setText(cal[1]);
}else if (actionEvent.getActionCommand().equals("*")){
if (!cal[0].equals("")&&!cal[2].equals("")){
if (cal[0].contains(".") || cal[2].contains(".")){
cal[0]=String.valueOf(Double.parseDouble(cal[0]) * Double.parseDouble(cal[2]));
}else {
cal[0] = String.valueOf(Integer.parseInt(cal[0]) * Integer.parseInt(cal[2]));
}
cal[2]="";
}
cal[1] = "*";
_count=false;
jTextField.setText(cal[1]);
}else if (actionEvent.getActionCommand().equals("/")){
if (!cal[0].equals("")&&!cal[2].equals("")){
if (cal[0].contains(".") || cal[2].contains(".")){
cal[0]=String.valueOf(Double.parseDouble(cal[0]) / Double.parseDouble(cal[2]));
}else {
cal[0] = String.valueOf(Integer.parseInt(cal[0]) / Integer.parseInt(cal[2]));
}
cal[2]="";
}
cal[1] = "/";
_count=false;
jTextField.setText(cal[1]);
}else if (actionEvent.getActionCommand().equals("%")){
cal[0]=String.valueOf(Integer.parseInt(cal[0]) % Integer.parseInt(cal[2]));
cal[2]="";
cal[1] = "%";
_count=false;
jTextField.setText(cal[1]);
}else if (actionEvent.getActionCommand().equals("=")){
if(cal[0].equals("")&&cal[2].equals("")){
jTextField.setText("0");
}else if(!cal[0].equals("")&&cal[2].equals("")){
jTextField.setText(cal[0]);
}else if(!cal[0].equals("")&&!cal[2].equals("")){
if(cal[1].equals("+")){
if (cal[0].contains(".") || cal[2].contains(".")){
cal[3]=String.valueOf(Double.parseDouble(cal[0]) + Double.parseDouble(cal[2]));
}else {
cal[3] = String.valueOf(Integer.parseInt(cal[0]) + Integer.parseInt(cal[2]));
}
jTextField.setText(cal[3]);
}else if (cal[1].equals("-")){
if (cal[0].contains(".") || cal[2].contains(".")){
cal[3]=String.valueOf(Double.parseDouble(cal[0]) - Double.parseDouble(cal[2]));
}else {
cal[3] = String.valueOf(Integer.parseInt(cal[0]) - Integer.parseInt(cal[2]));
}
jTextField.setText(cal[3]);
}else if(cal[1].equals("*")){
if (cal[0].contains(".") || cal[2].contains(".")){
cal[3]=String.valueOf(Double.parseDouble(cal[0]) * Double.parseDouble(cal[2]));
}else {
cal[3] = String.valueOf(Integer.parseInt(cal[0]) * Integer.parseInt(cal[2]));
}
jTextField.setText(cal[3]);
}else if (cal[1].equals("/")){
if (cal[0].contains(".") || cal[2].contains(".")){
cal[3]=String.valueOf(Double.parseDouble(cal[0]) / Double.parseDouble(cal[2]));
}else {
cal[3] = String.valueOf(Integer.parseInt(cal[0]) / Integer.parseInt(cal[2]));
}
jTextField.setText(cal[3]);
}else if (cal[1].equals("%")){
cal[3]=String.valueOf(Integer.parseInt(cal[0]) % Integer.parseInt(cal[2]));
jTextField.setText(cal[3]);
}
}
cal[0]=cal[3];
cal[2]="";
_count=true;
_count1 = true;
cal[5]=cal[0];
}else if(actionEvent.getActionCommand().equals("MC")){
cal[4]="";
}else if(actionEvent.getActionCommand().equals("MR")){
if(_count){
if(!cal[4].equals("")){
cal[0]=cal[4];
jTextField.setText(cal[0]);
}
}else {
if(!cal[4].equals("")){
cal[2]=cal[4];
jTextField.setText(cal[2]);
}
}
}else if(actionEvent.getActionCommand().equals("MS")){
if(_count){
cal[0]="";
}
cal[4]=cal[5];
System.out.println("cal[5]"+cal[5]);
System.out.println("ms:"+cal[4]);
}else if(actionEvent.getActionCommand().equals("M+")){
if(!cal[4].equals("")){
if (cal[4].contains(".") || cal[5].contains(".")){
cal[4]=String.valueOf(Double.parseDouble(cal[4]) + Double.parseDouble(cal[5]));
}else {
cal[4] = String.valueOf(Integer.parseInt(cal[4]) + Integer.parseInt(cal[5]));
}
}
}else if(actionEvent.getActionCommand().equals("M-")){
if(!cal[4].equals("")){
if (cal[4].contains(".") || cal[5].contains(".")){
cal[4]=String.valueOf(Double.parseDouble(cal[4]) - Double.parseDouble(cal[5]));
}else {
cal[4] = String.valueOf(Integer.parseInt(cal[4]) - Integer.parseInt(cal[5]));
}
}
}
}
public void method(ActionEvent actionEvent){
if(_count){
if (_count1){
cal[0]="";
_count1=false;
}
cal[0] += actionEvent.getActionCommand();
cal[5]=cal[0];
System.out.println(cal[5]);
}else {
cal[2] += actionEvent.getActionCommand();
cal[5]=cal[2];
}
jTextField.setText(actionEvent.getActionCommand());
}
}
효과 그림:이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.