栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java实验十三 GUI编程-事件处理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java实验十三 GUI编程-事件处理

一、实验目的
  1. 掌握事件处理的概念
  2. 掌握基本的事件处理相关的类
实验内容:

必做

  1. 编写一个程序,在第一个文本框中输入第一个加数,在第二个文本框中输入第二个数,当鼠标单击按钮“=”时,在第三个文本框中显示前两个加数的和。
package ex13;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Totalizer2 extends JFrame implements ActionListener  {
	//继承了Frame,该类本身就是一个窗口。可用this替代创建面板
	JTextField t1,t2,t3;
	JButton b;
	JLabel label1,label2;
	
	public Totalizer2(){
		//窗口标题
		super("plus games!!");
		
		//Container c = getContentPane();
		
		//c.setLayout(new FlowLayout(FlowLayout.LEFT));
		this.setLayout(null);
		
		label1 = new JLabel("加法运算:");
		label1.setBounds(10,30, 70, 30);
		
		t1 = new JTextField();
		t1.setBounds(70, 30, 50, 30);
		label2 = new JLabel("+");
		label2.setBounds(130,30,50,30);
		
		t2 = new JTextField();
		t2.setBounds(150, 30, 50, 30);
		
		b = new JButton("=");
		b.setBounds(210, 30, 50, 30);
		//ActionListener al = new ActionListener();
		
		
		t3 = new JTextField();
		t3.setBounds(270, 30, 50, 30);
		
		this.add(label1);
		
		this.add(t1);
		
		this.add(label2);
		
		this.add(t2);
		
		this.add(b);
		
		this.add(t3);
		//关闭方式:点击关闭按钮
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//设置大小
		setSize(450,150);
		//设置可见
		setVisible(true);
		//不可调节窗口大小
		this.setResizable(false);
		//给按钮b注册监听者this
		b.addActionListener(this);
	}
	


	public static void main(String[] args) {
		//创建对象
		new Totalizer2();
	}

	 public void actionPerformed(ActionEvent e) {
	        if (e.getSource()==b) {
	        	// 如果事件源是b按钮
	            if (!this.t1.getText().matches("[0-9.-]+")) {
	                JOptionPane.showMessageDialog(getParent(),"请输入要计算的数字!");
	                t1.requestFocus();
	            }
	            else if (!this.t2.getText().matches("[0-9.-]+")) {
	                JOptionPane.showMessageDialog(getParent(),"请输入要计算的数字!");
	                t2.requestFocus();
	            }
	            else 
	            {
	              String a=this.t1.getText(),b=this.t2.getText(),c;
	              double one,two,sum;
	              one =Double.parseDouble(a);
	              two=Double.parseDouble(b);
	              sum=one+two;
	              c=String.valueOf(sum);
	              this.t3.setText(c);
	              // 标签setText 
	            }
	        }
	 }

}

运行结果:


2. 在第8章第7题的基础上,设计一个界面,第一行3个按钮,第二行中间一个文本框,第三行含有两个按钮,一个按钮显示“问好”字样,另一个按钮显示“再见”字样。当点击“问好”按钮时,第二行文本框中显示出“你好!*** ” 字样。点击“再见” 按钮时,文本框出现“再见,*** ”字样。

package ex13;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Button2 extends JFrame implements ActionListener{ // 一定要继承ActionListener才能用this
	//创建6个按钮
	JButton b1,b2,b3,b5,b6; ;
	//创建一个文本框
	JTextField t;
	public Button2() {
		
		super("按钮布局");
		
		//setResizable(false);
		setBounds(100,100,300,270);  
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//this.setLayout(null);
		setLayout(new GridLayout(3, 1));  // 3行1列
		//ActionListener a = new ActionListener();
		
		//分别给按钮起名字
		b1= new JButton("1");
		b2= new JButton("2");
		b3= new JButton("3");
		//b4= new JButton("4");
		b5 = new JButton("问好");
		b6 = new JButton("再见");
		//第一个面板
        JPanel p1=new JPanel();// 
        p1.setLayout(new FlowLayout(FlowLayout.CENTER,20,20));
		p1.add(b1);
		p1.add(b2);
		p1.add(b3);
		add(p1);
		
		//第二个面板
		JPanel p2=new JPanel();
		//p2.add(b4);
		t = new JTextField(12);
		p2.add(t);
		p2.setLayout(new FlowLayout(FlowLayout.CENTER,20,20));
		add(p2);
		
		//第三个面板
        JPanel p3=new JPanel(); 
        p3.setLayout(new FlowLayout(FlowLayout.CENTER,20,20));
        p3.add(b5);
        b5.addActionListener(this);
		p3.add(b6);
		b6.addActionListener(this);
		add(p3); // JFrame默认边界布局
		
		setVisible(true);
		}
	//主程序
	public static void main(String[] args) {
		new Button2();
	}
	public void actionPerformed(ActionEvent e) {
		
	        if (e.getSource()==b5) {
	        	t.setText("你好!***");
	        	//b5.requestFocus();
	        }else if (e.getSource()==b6) {
	        	t.setText("再见!***");
	        	//b6.requestFocus();
	        }
	        
	
	}

}

运行结果:


实验总结:

学习了事件处理,监听器的使用。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/929759.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号