GUI加减乘除计算


刚刚接触到GUI的时候,似懂非懂。不过后来写的多了,做的多了,也就明白啦! 下面的程序大家可以亲自运行一下啊

packagea;

//importjava.awt.*;

importjava.awt.Button;

importjava.awt.Choice;

importjava.awt.Component;

importjava.awt.Frame;

importjava.awt.GridLayout;

importjava.awt.Label;

importjava.awt.TextField;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.util.Vector;

 

importjavax.swing.ComboBoxModel;

importjavax.swing.JComboBox;

importorg.omg.CORBA.PUBLIC_MEMBER;

 

importcom.sun.org.apache.bcel.internal.generic.NEW;

importcom.sun.xml.internal.bind.v2.schemagen.xmlschema.List;

publicclass AddFrame extends Frame

{

  public static void main(String args[])

  {

     AddFrame  b=new AddFrame ();

  }

  

  TextField num1=new TextField();

  TextField num2=new TextField();

  TextField num3=new TextField();

  Choice osChoice=new Choice();

 

  public AddFrame(){

     setName("addFrame");

     setSize(500,500);

     setVisible(true);

     initView();

      }

  

   void initView()

   {

      Button okButton=newButton("计算");

      Button exitButton=newButton("退出");

      this.setLayout(newGridLayout(5,2));

      add(newLabel("第一个数:"));

      add(num1);

     

      add(newLabel("符号:"));

     

     osChoice.add("+");

     osChoice.add("-");

     osChoice.add("*");

     osChoice.add("/");

     osChoice.add("%");

     add(osChoice); 

     

     

      add(newLabel("第二个数:"));

      add(num2);

     

      add(newLabel("结果:"));

      add(num3);

     

      add(okButton);

     add(exitButton);

     

     

     

      AddListener addListener=newAddListener();

     okButton.addActionListener(addListener);

     

   

      ExitListener listener=newExitListener();

     exitButton.addActionListener(listener);

     

      }

   class AddListener implements ActionListener

   {

 

     private static final boolean String = false;

 

     public void actionPerformed(ActionEvent e)

       {

       String str1=num1.getText();

       String str2=num2.getText();

       if("+"==osChoice.getSelectedItem())

       {

       intresult=Integer.parseInt(str1)+Integer.parseInt(str2);

       num3.setText(Integer.toString(result));

       }

       if("-"==osChoice.getSelectedItem())

       {

         intresult=Integer.parseInt(str1)-Integer.parseInt(str2);

         num3.setText(Integer.toString(result));

       }

       if("*"==osChoice.getSelectedItem())

       {

         intresult=Integer.parseInt(str1)*Integer.parseInt(str2);

         num3.setText(Integer.toString(result));

       }

       if("/"==osChoice.getSelectedItem())

       {

         doubleresult=(Integer.parseInt(str1)/Integer.parseInt(str2));

         num3.setText(Double.toString(result));

       }

       if("%"==osChoice.getSelectedItem())

       {

         intresult=Integer.parseInt(str1)%Integer.parseInt(str2);

         num3.setText(Integer.toString(result));

       }

     }

   }

   class ExitListener implements ActionListener

   {

 

      public voidactionPerformed(ActionEvent e)

      {

 

        System.exit(0);

      }

  

  }

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简易的 Java 窗体计算器,支持加减乘除四则运算: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Calculator extends JFrame implements ActionListener { private JTextField tfNum1, tfNum2, tfResult; private JButton btnAdd, btnSub, btnMul, btnDiv; public Calculator() { super("Calculator"); setSize(400, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new GridLayout(4, 2)); JLabel lblNum1 = new JLabel("Number 1:"); tfNum1 = new JTextField(10); add(lblNum1); add(tfNum1); JLabel lblNum2 = new JLabel("Number 2:"); tfNum2 = new JTextField(10); add(lblNum2); add(tfNum2); JLabel lblResult = new JLabel("Result:"); tfResult = new JTextField(10); tfResult.setEditable(false); add(lblResult); add(tfResult); btnAdd = new JButton("Add"); add(btnAdd); btnAdd.addActionListener(this); btnSub = new JButton("Subtract"); add(btnSub); btnSub.addActionListener(this); btnMul = new JButton("Multiply"); add(btnMul); btnMul.addActionListener(this); btnDiv = new JButton("Divide"); add(btnDiv); btnDiv.addActionListener(this); setVisible(true); } public void actionPerformed(ActionEvent e) { double num1 = Double.parseDouble(tfNum1.getText()); double num2 = Double.parseDouble(tfNum2.getText()); double result = 0; if (e.getSource() == btnAdd) { result = num1 + num2; } else if (e.getSource() == btnSub) { result = num1 - num2; } else if (e.getSource() == btnMul) { result = num1 * num2; } else if (e.getSource() == btnDiv) { result = num1 / num2; } tfResult.setText(Double.toString(result)); } public static void main(String[] args) { new Calculator(); } } ``` 该计算使用了 Java 的 Swing GUI 库来创建窗体界面,包含两个文本框用于输入数字,一个文本框用于显示结果,以及四个按钮用于进行四则运算。在点击按钮后,通过 `ActionListener` 接口中的 `actionPerformed` 方法来计算并显示结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值