计算器界面与监听事件

Posted zhlhuanl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了计算器界面与监听事件相关的知识,希望对你有一定的参考价值。

package 计算器;
import java.awt.BorderLayout; 
import java.awt.Container; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyAdapter; 
import java.awt.event.KeyEvent; 
 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
 
public class Jisuanqi extends JFrame { 
     
    public static void main(String[] args) {  
     Jisuanqi frame = new Jisuanqi(); 
         
        frame.setTitle("计算器"); 
        frame.setSize(300,400);
        frame.setResizable(false); 
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);  
        frame.init();  
        frame.setVisible(true); 
    } 
     
    private void init()  
    { 
        textField = new JTextField();    
        textField.setEditable(false); 
        textField.setHorizontalAlignment (JTextField.RIGHT); 
        JPanel panel = new JPanel(); 
        panel.setLayout(new GridLayout(4, 4)); 
         
        Container container = getContentPane(); 
        container.add(textField, BorderLayout.NORTH); 
        container.add(panel, BorderLayout.CENTER); 
         
        panel.add(useButton(‘7‘)); 
        panel.add(useButton(‘8‘)); 
        panel.add(useButton(‘9‘)); 
        panel.add(useButton(‘+‘)); 
        panel.add(useButton(‘4‘)); 
        panel.add(useButton(‘5‘)); 
        panel.add(useButton(‘6‘)); 
        panel.add(useButton(‘-‘)); 
        panel.add(useButton(‘1‘)); 
        panel.add(useButton(‘2‘)); 
        panel.add(useButton(‘3‘)); 
        panel.add(useButton(‘*‘)); 
        panel.add(useButton(‘0‘)); 
        panel.add(useButton(‘.‘)); 
        panel.add(useButton(‘=‘)); 
        panel.add(useButton(‘/‘)); 
    } 
     
    public JButton useButton (final char key)  
    { 
        JButton button = new JButton(String.valueOf(key)); 
         
        button.addActionListener(new ActionListener() 
        { 
            public void actionPerformed(ActionEvent event) 
            { 
                JButton btn = (JButton) event.getSource(); 
                char key2 = btn.getText().charAt(0); 
                 
                action(key2); 
            } 
        }); 
        button.addKeyListener(new KeyAdapter() 
        { 
            public void keyReleased(KeyEvent event) 
            { 
                char key2 = event.getKeyChar (); 
                action(key2); 
            } 
        }); 
         
        return button; 
    } 
     
    private void action(char key2) 
    { 
        if(reop) 
        { 
            textField.setText(""); 
            reop = false; 
        } 
         
        switch(key2) 
        { 
            case ‘+‘: 
                a1 = Double.parseDouble(textField.getText()); 
                textField.setText(""); 
                operator = ‘+‘; 
                break; 
            case ‘-‘: 
                a1 = Double.parseDouble(textField.getText()); 
                textField.setText(""); 
                operator = ‘-‘; 
                break; 
            case ‘*‘: 
                a1= Double.parseDouble(textField.getText()); 
                textField.setText(""); 
                operator = ‘*‘; 
                break; 
            case ‘/‘: 
                 a1= Double.parseDouble(textField.getText());  
                textField.setText(""); 
                operator = ‘/‘; 
                break; 
            case ‘=‘: 
                reop = true; 
                a2 = Double.parseDouble(textField.getText()); 
                switch(operator) 
                { 
                    case ‘+‘: 
                        result = a1 + a2; 
                        break; 
                    case ‘-‘: 
                     result= a1 - a2; 
                        break; 
                    case ‘*‘: 
                     result = a1 * a2; 
                        break; 
                    case ‘/‘: 
                     result = a1 / a2; 
                        break; 
                    default: ; 
                } 
                textField.setText(String.valueOf(result)); 
                break; 
           case ‘1‘: 
            case ‘2‘: 
            case ‘3‘: 
            case ‘4‘: 
            case ‘5‘: 
            case ‘6‘: 
            case ‘7‘: 
            case ‘8‘: 
            case ‘9‘: 
            case ‘0‘: 
            case ‘.‘: 
                String text = textField.getText() + key2; 
                textField.setText(text); 
                break; 
            default: ; 
        } 
    } 
    private JTextField textField; 
    private double a1, a2, result; 
    private char operator; 
    private boolean reop = false; 
}}


import java.awt.*;

import java.awt.event.*;

public class mm {

    public static void main (String arg[])
{
    Frame f1=new Frame("个人信息");
    f1.setSize(300,200);
    f1.setLocation(300, 240);
    f1.setBackground(Color.blue);
    f1.setLayout(new FlowLayout());
    
    f1.add(new Label("姓名"));
    f1.add(new TextField("zhl",25));
    f1.add(new Label("手机号"));
    f1.add(new TextField("3318816990",25));
    f1.add(new Label("性别"));
    f1.add(new TextField("女",25));
    f1.add(new Label("籍贯"));
    f1.add(new TextField("中国",25));
    Button button1=new Button("ok");
    button1.addActionListener(new abc());
    f1.add( button1,BorderLayout.SOUTH);
    Button button2=new Button("EXIT");
    f1.add(button2,BorderLayout.EAST);
    button2.addActionListener(new abc());
    
    f1.addWindowListener(new close());
    f1.setVisible(true);
    
}

package zhl;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridBagLayout;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class abc extends mm implements ActionListener 
{
    
    @Override
    public void actionPerformed(ActionEvent ev) {
        // TODO Auto-generated method stub
        
      //  System.out.println("hello world!");
        Frame f2=new Frame("OK");
        f2.setSize(300,200);
        f2.setLocation(300, 240);
        f2.setBackground(Color.lightGray);
        f2.setLayout(new GridBagLayout());
        f2.add(new  Label("hello  world!"));
        
        f2.addWindowListener(new close());
        f2.setVisible(true);
    }
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class close implements WindowListener {

    @Override
    public void windowActivated(WindowEvent e) {
        // TODO Auto-generated method stub
        System.out.println("窗口激活成功!");
    }

    @Override
    public void windowClosed(WindowEvent e) {
        // TODO Auto-generated method stub
        System.out.println("窗口关闭后成功!");
    }

    @Override
    public void windowClosing(WindowEvent e) {
        // TODO Auto-generated method stub

        System.out.println("窗口关闭成功!");
        System.exit(0);
    }

    @Override
    public void windowDeactivated(WindowEvent e) {
        // TODO Auto-generated method stub
        System.out.println("窗口不活动!");
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
        // TODO Auto-generated method stub
        System.out.println("窗口小化再恢复成功!");
    }

    @Override
    public void windowIconified(WindowEvent e) {
        // TODO Auto-generated method stub
        System.out.println("窗口最小化成功!");
    }

    @Override
    public void windowOpened(WindowEvent e) {
        // TODO Auto-generated method stub
        System.out.println("窗口打开成功!");
    }

}















































































































































































































































以上是关于计算器界面与监听事件的主要内容,如果未能解决你的问题,请参考以下文章

Java程序(事件监听与计算机界面)

事件监听 计算器界面

事件监听和计算器界面

事件监听 计算机界面

窗体界面与事件监听 Monitor Interface

Java AWT 图形界面编程事件处理机制 ① ( 事件处理步骤 | 创建事件源对象 -> 自定义事件监听器 -> 创建监听器实例对象 -> 事件源绑定事件监听器 )