java做一个计算器窗口详解教程_怎么用JAVA编写一个计算器窗口,并实现他的功能,求给出代码,对的话一定高分回报,谢谢了。。。...

展开全部

JAVA 计算器程序62616964757a686964616fe78988e69d8331333264643161

Cadev.java

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.LinkedList;

import java.text.NumberFormat;

import javax.swing.border.*;

public class Cadev extends JFrame implements ActionListener

{

Nbtn nbtn[];

Obtn obtn[];

Button dotbtn,PlusOrMinusbtn,Backspacebtn,

recbtn,equbtn,clbtn;

JPanel panel;

JTextField resultShow;

String Oper[]={"+","-","*","/"};

LinkedList link;

boolean sfcequ=false;

public Cadev()

{

super("计算器");

link=new LinkedList();

nbtn=new Nbtn[10];

for(int i=0;i<=9;i++)

{

nbtn[i]=new Nbtn(i);

nbtn[i].addActionListener(this);

}

obtn=new Obtn[4];

for(int i=0;i<4;i++)

{

obtn[i]=new Obtn(Oper[i]);

obtn[i].addActionListener(this);

}

dotbtn=new Button(".");

PlusOrMinusbtn=new Button("+/-");

equbtn=new Button("=");

recbtn=new Button("1/x");

Backspacebtn=new Button("退格");

clbtn=new Button("C");

recbtn.setForeground(Color.blue);

dotbtn.setForeground(Color.blue);

PlusOrMinusbtn.setForeground(Color.blue);

equbtn.setForeground(Color.red);

Backspacebtn.setForeground(Color.red);

clbtn.setForeground(Color.red);

dotbtn.addActionListener(this);

PlusOrMinusbtn.addActionListener(this);

equbtn.addActionListener(this);

recbtn.addActionListener(this);

Backspacebtn.addActionListener(this);

clbtn.addActionListener(this);

resultShow=new JTextField(10);

resultShow.setHorizontalAlignment(JTextField.RIGHT);

resultShow.setFont(new Font("TimesRoman",Font.PLAIN,14));

resultShow.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));

resultShow.setBackground(Color.white);

resultShow.setEditable(false);

panel=new JPanel();

panel.setLayout(new GridLayout(4,5));

panel.add(nbtn[1]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的Java计算器界面的代码实现: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Calculator implements ActionListener { JFrame frame; JTextField textField; JButton[] buttons; String[] buttonLabels = { "7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", "0", ".", "=", "/" }; JPanel panel; public Calculator() { frame = new JFrame("Calculator"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 300); textField = new JTextField(10); frame.add(textField, BorderLayout.NORTH); buttons = new JButton[buttonLabels.length]; panel = new JPanel(new GridLayout(4, 4)); for (int i = 0; i < buttonLabels.length; i++) { buttons[i] = new JButton(buttonLabels[i]); buttons[i].addActionListener(this); panel.add(buttons[i]); } frame.add(panel, BorderLayout.CENTER); frame.setVisible(true); } public void actionPerformed(ActionEvent e) { String buttonText = e.getActionCommand(); if (buttonText.equals("=")) { String expression = textField.getText(); textField.setText(evaluate(expression)); } else { textField.setText(textField.getText() + buttonText); } } private String evaluate(String expression) { // TODO: implement the evaluation logic here return "0"; } public static void main(String[] args) { Calculator calc = new Calculator(); } } ``` 这个代码实现一个基本的计算器界面,包括数字按钮、运算符按钮和一个文本框用于显示输入和输出结果。当用户点击“=”按钮时,程序会调用`evaluate`方法计算结果并将其显示在文本框中。目前`evaluate`方法还没有实现,需要根据具体需进行实现

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值