设计一个简单的计算器界面

源程序:

package next;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class jisuanqi extends JFrame {
 private int add=1,sub=2,mul=3,div=4;
 private int op=0;
 boolean ifOp;
 private String output="0";
 private Button[] jba=new Button[]{
 new Button("7"),new Button("8"),new Button("9"),new Button("+"),
 new Button("4"),new Button("5"),new Button("6"),new Button("-"),
 new Button("1"),new Button("2"),new Button("3"),new Button("*"),
 new Button("0"),new Button("."),new Button("="),new Button("/")};
 private JPanel jpt=new JPanel();
 private JPanel jpb=new JPanel();
 private JTextField jtf=new JTextField("");
 private jisuanqi(){
  jpt.setLayout(new BorderLayout());
  jpt.add(jtf);
  this.add(jpt,BorderLayout.NORTH);
  jpb.setLayout(new GridLayout(4,4));
  for(int i=0;i<jba.length;i++){
  jpb.add(jba[i]);
  if(i==3||i==7||i==11||i==15||i==14)
  jba[i].addActionListener(new setOperate_Act());
  else
  jba[i].addActionListener(new setLabel_Act());
  }
  this.add(jpb,BorderLayout.CENTER);
  this.setSize(200, 200); 
  this.setResizable(false);
  this.setVisible(true);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
 private void operate(String x){double x1=Double.valueOf(x);
 double y=Double.valueOf(output);
 switch(op){
 case 0:output=x;break;
 case 1:output=String.valueOf(y+x1);break;
 case 2:output =String.valueOf(y-x1);break;
 case 3:output =String.valueOf(y*x1);break;
 case 4:
 if(x1!=0) output=String.valueOf(y/x1);
 else output="不能为0";
 break;
 }
 }
 public String add(String x){
 operate(x);
 op=add;
 return output;
 }
 public String subtract(String x){
  operate(x);
  op=sub;
 return output;
 }
 public String multiply(String x){
  operate(x);
  op=mul;
 return output;
 }
 public String divide(String x){
  operate(x);
  op=div;
 return output;
 }
 public String Equals(String x){
  operate(x);
  op=0;
 return output;
 }
 public void opClean(){
  op=0;
  output="0";
 }
 class setOperate_Act implements ActionListener{
 public void actionPerformed(ActionEvent e) {

 }
 }
 class setLabel_Act implements ActionListener{
 public void actionPerformed(ActionEvent e) {
 }
 }
 
 public static void main(String[] args) {
  // TODO 自动生成的方法存根
  new jisuanqi();
 }
}
运行结果:
 
 
实验心得:此次实验是设计一个计算器界面,没有让设计出来的这个计算器拥有计算的能力,但虽然是这样也让我废了好的功夫;
其中界面的排序问题使用了for循环语句实现;最后别忘了还有让其可视化setvisible()。

转载于:https://www.cnblogs.com/java-17/p/10942682.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
由于我是AI语言模型,无法直接提供图形界面,但是可以提供一个简单Java代码示例,来展示如何设计一个简易计算器界面。 ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Calculator extends JFrame implements ActionListener { private JTextField textField; private JButton button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, buttonPlus, buttonMinus, buttonMultiply, buttonDivide, buttonClear, buttonEquals; private JPanel panel; // 构造方法 public Calculator() { super("Calculator"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300, 300); // 创建文本框 textField = new JTextField(20); textField.setEditable(false); // 禁止手动输入 // 创建按钮 button0 = new JButton("0"); button1 = new JButton("1"); button2 = new JButton("2"); button3 = new JButton("3"); button4 = new JButton("4"); button5 = new JButton("5"); button6 = new JButton("6"); button7 = new JButton("7"); button8 = new JButton("8"); button9 = new JButton("9"); buttonPlus = new JButton("+"); buttonMinus = new JButton("-"); buttonMultiply = new JButton("*"); buttonDivide = new JButton("/"); buttonClear = new JButton("C"); buttonEquals = new JButton("="); // 设置监听器 button0.addActionListener(this); button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); button4.addActionListener(this); button5.addActionListener(this); button6.addActionListener(this); button7.addActionListener(this); button8.addActionListener(this); button9.addActionListener(this); buttonPlus.addActionListener(this); buttonMinus.addActionListener(this); buttonMultiply.addActionListener(this); buttonDivide.addActionListener(this); buttonClear.addActionListener(this); buttonEquals.addActionListener(this); // 创建面板 panel = new JPanel(new GridLayout(4, 4)); panel.add(button7); panel.add(button8); panel.add(button9); panel.add(buttonPlus); panel.add(button4); panel.add(button5); panel.add(button6); panel.add(buttonMinus); panel.add(button1); panel.add(button2); panel.add(button3); panel.add(buttonMultiply); panel.add(buttonClear); panel.add(button0); panel.add(buttonEquals); panel.add(buttonDivide); // 添加组件 add(textField, BorderLayout.NORTH); add(panel, BorderLayout.CENTER); setVisible(true); } // 监听器方法 public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); String text = textField.getText(); if (command.equals("C")) { textField.setText(""); } else if (command.equals("=")) { try { int result = evaluate(text); textField.setText(String.valueOf(result)); } catch (Exception ex) { textField.setText("Error"); } } else { textField.setText(text + command); } } // 计算表达式 private int evaluate(String text) throws Exception { String[] tokens = text.split(" "); if (tokens.length != 3) { throw new Exception(); } int operand1 = Integer.parseInt(tokens[0]); int operand2 = Integer.parseInt(tokens[2]); if (tokens[1].equals("+")) { return operand1 + operand2; } else if (tokens[1].equals("-")) { return operand1 - operand2; } else if (tokens[1].equals("*")) { return operand1 * operand2; } else if (tokens[1].equals("/")) { return operand1 / operand2; } else { throw new Exception(); } } public static void main(String[] args) { new Calculator(); } } ``` 这个Java代码示例实现了一个简单计算器界面,包含数字按钮、操作符按钮和清除、等号按钮。用户可以通过点击按钮来输入数字和操作符,最后按下等号按钮计算结果并显示在文本框中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值