事件监听与计算机界面

1.事件监听

package 事件监听;
import java.awt.*;
import java.awt.event.*;

public  class Frame2 extends Frame implements ActionListener{
 private Button button1;
 public Frame2()
 {
  super("个人信息");
      this.setSize(250,200);//设计组件的尺寸
      this.setLocation(800,600);//设计组件显示的位置
      this.setBackground(Color.blue);//设计背景颜色
      this.setLayout(new FlowLayout());//设计容器为流布局,居中
      this.add(new Label("姓名"));
      this.add(new TextField("唯梦",20));
      this.add(new Label("性别"));
      this.add(new TextField("女",20));
      this.add(new Label("民族"));
      this.add(new TextField("汉",20));
      this.add(new Label("年龄"));
      this.add(new TextField("20",20));
      button1=new Button("OK");
      this.add(button1);
      button1.addActionListener(this);
      this.addWindowListener(new WinClose());
      this.setVisible(true);
 }
 public void actionPerformed(ActionEvent ev)
 {
  if(ev.getSource()==button1)
  {
   System.out.print("welcome");
  }
 }
 public static void main(String arg[])
 {
  new Frame2();
 }
     class WinClose implements WindowListener
 {
  public void windowClosing(WindowEvent ev)
  {
   System.exit(0);
  }
  public void windowOpened(WindowEvent ev) {}
  public void windowActivated(WindowEvent ev) {}
  public void windowDeactivated(WindowEvent ev) {}
  public void windowClose(WindowEvent ev) {}
  public void windowIconified(WindowEvent ev) {}
  public void windowDeiconified(WindowEvent ev) {}
  @Override
  public void windowClosed(WindowEvent arg0) {
   // TODO 自动生成的方法存根
   
  }
 } 
 }
 

2.计算器界面

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; 
}}

原文地址:https://www.cnblogs.com/baoyan/p/9220979.html

时间: 2024-08-29 03:42:04

事件监听与计算机界面的相关文章

事件监听 计算机界面

1.事件监听: package 事件监听;import java.awt.*;import java.awt.event.*;public  class Frame2 extends Frame implements ActionListener{ private Button button1; public Frame2() {  super("个人信息");      this.setSize(250,220);//设计组件的尺寸      this.setLocation(800

窗体界面与事件监听 Monitor Interface

日常生活中窗体随处可见,各种聊天.直播.游戏.音影.都是各种窗体组成的软件. 一般而言一个窗体界面就是一个JFrame对象. JFrame的位置在我们的一个javax.swing.JFrame. 先创建一个界面并设置可见 //创建一个界面类 public class Interface {//显示界面的方法 public void intUI(){ //创建一个顶级容器 javax.swing.JFrame frame=new javax.swing.JFrame("登陆界面"); /

UI界面事件监听

事件监听方法 using UnityEngine; using System.Collections; using UnityEngine.EventSystems; using System; using UnityEngine.UI; /// <summary> /// 按钮监听 /// </summary> public class EventTriggerListener : UnityEngine.EventSystems.EventTrigger { public de

事件监听 计算器界面

1.事件监听: package 事件监听;import java.awt.*;import java.awt.event.*;public  class Frame2 extends Frame implements ActionListener{ private Button button1; public Frame2() {  super("个人信息");      this.setSize(250,220);//设计组件的尺寸      this.setLocation(800

WebView使用详解(二)——WebViewClient与常用事件监听

登录|注册     关闭 启舰 当乌龟有了梦想-- 目录视图 摘要视图 订阅 异步赠书:Kotlin领衔10本好书      免费直播:AI时代,机器学习如何入门?      程序员8月书讯      每周荐书:Java Web.Python极客编程(评论送书) WebView使用详解(二)--WebViewClient与常用事件监听 2016-05-28 11:24 20083人阅读 评论(13) 收藏 举报  分类: 5.andriod开发(148)  版权声明:本文为博主原创文章,未经博主

JAVA GUI 事件监听事件 详解 和 案例.

GUI(图形用户界面) Graphical User Interface(图形用户接口) 用图形的 方式, 来显示 计算机操作的 界面, 这样更方便 更直观. CLI Command Line User Interface(命令行用户接口) 就是常见的 Dos 命令行操作. 需要记忆一些常用的命令, 操作不直观. Java 为 GUI 提供的 对象 都存在 java.awt 和 javax.swing 两个包中. Java 图形化界面的 产品: eclipse 这个产品使用 纯Java 语言 编

[基础控件]---状态切换控件CompoundButton及其子类CheckBox、RadioButton、ToggleButton、switch事件监听与场景使用

一.事件监听 对于普通的Button,对其进行事件监听Google官方给出了常见的三种监听方式:1.对每一个button设置事件监听器button.setOnClickListener(View.OnclickListener  listener);此种方法当button按钮较多时代码显得多.乱.不够简洁明了. 2.在Activity中实现接口View.OnclickListener,然后重写void onClick(View v)方法,在方法中通过switch(v.getId())予以区分不同

Java中的事件监听机制

鼠标事件监听机制的三个方面: 1.事件源对象: 事件源对象就是能够产生动作的对象.在Java语言中所有的容器组件和元素组件都是事件监听中的事件源对象.Java中根据事件的动作来区分不同的事件源对象,动作发生在哪个组件上,那么该组件就是事件源对象 2.事件监听方法: addMouseListener(MouseListener ml) ;该方法主要用来捕获鼠标的释放,按下,点击,进入和离开的动作:捕获到相应的动作后,交由事件处理类(实现MouseListener接口)进行处理. addAction

UI事件监听的击穿

什么是UI事件监听的击穿 在游戏视图中,有两个UI界面叠在一起的时候,单击一个空白处,却触发了被覆盖在下层了UI界面中的单击事件,这就是单击击穿了上层界面. 假设场景中放置了一个箱子,单击箱子会触发一个开箱事件,如果单击一个UI,恰好UI在视觉上将箱子覆盖了,那么它也许就会触发箱子的单击事件. 如何避免和解决UI事件监听的击穿 第一种方法:用一层BoxCollider覆盖,进行遮挡. 在界面底板上Attach一个BoxCollider. 第二种方法:使用EventMask Unity的Camer