java實現簡單的加法器


1.匿名類實現接口的加法器

2.內部類實現接口的加法器

3.本類實現接口

1.匿名類實現接口的加法器

 package inclass;
 
 import java.awt.FlowLayout;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 //匿名類類實現接口
 import javax.swing.*;
 class Add1 extends JFrame  {
 	JButton jButton = new JButton("=");
 	JLabel jLabel = new JLabel("+");
	JTextField j1 = new JTextField(4);
	JTextField j2 = new JTextField(4);
	JTextField j3 = new JTextField(4);

	public Add1() {
         setSize(300,100);
         setVisible(true);
         setDefaultCloseOperation(EXIT_ON_CLOSE);
         setLayout(new FlowLayout());         
         add(j1);         
         add(jLabel);
         add(j2);
         add(jButton);
         add(j3);
         validate();
         jButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				 try {
					  double t1 = Double.parseDouble(j1.getText());
					  double t2 = Double.parseDouble(j2.getText());
					  j3.setText(Double.toString(t1+t2));
					 }
				 catch (NumberFormatException w) {
					     JOptionPane.showMessageDialog(null, w);
					   }
			}
		});
		}
     
 }

	 class Error {
		
		public static void main(String[] args) {
         new Add1();
     }
}

 

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 }

返回頂部


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM