• java实现简单的加法器


    1.匿名类实现接口的加法器

    2.内部类实现接口的加法器

    3.本类实现接口

    1.匿名类实现接口的加法器

     1 package inclass;
     2 
     3 import java.awt.FlowLayout;
     4 import java.awt.event.ActionEvent;
     5 import java.awt.event.ActionListener;
     6 //匿名类类实现接口
     7 import javax.swing.*;
     8 
     9 class Add1 extends JFrame implements ActionListener{
    10     JButton jButton = new JButton("=");
    11     JLabel jLabel = new JLabel("+");
    12     JTextField j1 = new JTextField(4);
    13     JTextField j2 = new JTextField(4);
    14     JTextField j3 = new JTextField(4);
    15     
    16     public Add1() {
    17         setSize(300,100);
    18         setVisible(true);
    19         setDefaultCloseOperation(EXIT_ON_CLOSE);
    20         setLayout(new FlowLayout());
    21         add(j1);
    22         add(jLabel);
    23         add(j2);
    24         add(jButton);
    25         add(j3);
    26         validate();
    27         jButton.addActionListener(this);
    28     }
    29     public void actionPerformed(ActionEvent event)
    30     {
    31         try {
    32             double t1 = Double.parseDouble(j1.getText());
    33             double t2 = Double.parseDouble(j2.getText());
    34             j3.setText(Double.toString(t1+t2));
    35         }
    36         catch (NumberFormatException e) {
    37             JOptionPane.showMessageDialog(null, e);
    38         }
    39     }
    40 }
    41 
    42 public class Error{
    43     public static void main(String[] args) {
    44         new Add1();
    45     }
    46 }

    2.内部类实现接口的加法器

     1 package inclass;
     2 import java.awt.FlowLayout;
     3 import java.awt.event.ActionEvent;
     4 import java.awt.event.ActionListener;
     5 //内部类实现接口
     6 import javax.swing.*;
     7 
     8 class Add extends JFrame{
     9     JButton jButton = new JButton("=");
    10     JLabel jLabel = new JLabel("+");
    11     JTextField j1 = new JTextField(4);
    12     JTextField j2 = new JTextField(4);
    13     JTextField j3 = new JTextField(4);
    14     
    15     public Add() {
    16         setSize(300,100);
    17         setVisible(true);
    18         setDefaultCloseOperation(EXIT_ON_CLOSE);
    19         setLayout(new FlowLayout());
    20         add(j1);
    21         add(jLabel);
    22         add(j2);
    23         add(jButton);
    24         add(j3);
    25         validate();
    26         jButton.addActionListener(new Tt());
    27     }
    28     class Tt implements ActionListener{
    29         public void actionPerformed(ActionEvent event)
    30         {
    31             try {
    32                 double t1 = Double.parseDouble(j1.getText());
    33                 double t2 = Double.parseDouble(j2.getText());
    34                 j3.setText(Double.toString(t1+t2));
    35             }
    36             catch (NumberFormatException e) {
    37                 JOptionPane.showMessageDialog(null, e);
    38             }
    39         }
    40     }
    41 }
    42 
    43 
    44 
    45 public class Text1 {
    46     public static void main(String[] args) {
    47         new Add();
    48     }
    49 
    50 }

    3.本类实现接口

     1 package inclass;
     2 import java.awt.FlowLayout;
     3 import java.awt.event.ActionEvent;
     4 import java.awt.event.ActionListener;
     5 //内部类实现接口
     6 import javax.swing.*;
     7 
     8 class Add extends JFrame{
     9     JButton jButton = new JButton("=");
    10     JLabel jLabel = new JLabel("+");
    11     JTextField j1 = new JTextField(4);
    12     JTextField j2 = new JTextField(4);
    13     JTextField j3 = new JTextField(4);
    14     
    15     public Add() {
    16         setSize(300,100);
    17         setVisible(true);
    18         setDefaultCloseOperation(EXIT_ON_CLOSE);
    19         setLayout(new FlowLayout());
    20         add(j1);
    21         add(jLabel);
    22         add(j2);
    23         add(jButton);
    24         add(j3);
    25         validate();
    26         jButton.addActionListener(new Tt());
    27     }
    28     class Tt implements ActionListener{
    29         public void actionPerformed(ActionEvent event)
    30         {
    31             try {
    32                 double t1 = Double.parseDouble(j1.getText());
    33                 double t2 = Double.parseDouble(j2.getText());
    34                 j3.setText(Double.toString(t1+t2));
    35             }
    36             catch (NumberFormatException e) {
    37                 JOptionPane.showMessageDialog(null, e);
    38             }
    39         }
    40     }
    41 }
    42 public class Text1 {
    43     public static void main(String[] args) {
    44         new Add();
    45     }
    46 
    47 }

    返回顶部

  • 相关阅读:
    iBatis——自动生成DAO层接口提供操作函数(详解)
    【Spring Boot项目】Win7+JDK8+Tomcat8环境下的War包部署
    MySQL使用小记
    DB迁移:从SQL Server 2005到MySQL
    【文章学习】监控网页卡顿、崩溃
    为什么执行x in range(y)如此之快
    python笔试题(三)
    python笔试题(二)
    python笔试题(-)
    rest-framework(2)
  • 原文地址:https://www.cnblogs.com/nanfengnan/p/12746143.html
Copyright © 2020-2023  润新知