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

Java练习题8

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

Java练习题8

Java swing
在窗口中有两个按钮,“确定”和“取消”。
单击“确定”按钮,窗口标题栏显示“你单击了确定按钮”。“确定”按钮变成“OK”。
单击“取消”按钮,窗口标题栏显示“你单击了取消按钮”。“取消”按钮变成“Cancel”。
要求采用四种方法:本类、外部类、内部类、匿名类。

方法1:本类

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Interaction1 implements ActionListener{	//本类
	Jframe fr;
	JButton buttonOK,buttonCancel ;
	
	Interaction1(){
	fr=new Jframe("事件响应");
	fr.setLayout(new FlowLayout());
	buttonOK=new JButton("确定");
	buttonCancel=new JButton("取消");
	fr.add(buttonCancel);
	fr.add(buttonOK);
	fr.setSize(300, 150);
	fr.setVisible(true);
	fr.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
	fr.setLocationRelativeTo(null);
	buttonOK.addActionListener(this);
	buttonCancel.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==buttonOK){
			fr.setTitle("你单击了确定按钮");
			buttonOK.setText("OK");
		}
		else if(e.getSource()==buttonCancel) {
			fr.setTitle("你单击了取消按钮");
			buttonCancel.setText("Cancel");
		}
			
		
	}
	public static void main(String[] args) {
		new Interaction1();
	}
}


方法二:匿名类

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.Jframe;

public class Interaction2 {//匿名类
	Jframe fr;
	JButton buttonOK,buttonCancel ;
	
	Interaction2(){
	fr=new Jframe("事件响应");
	fr.setLayout(new FlowLayout());
	buttonOK=new JButton("确定");
	buttonCancel=new JButton("取消");
	fr.add(buttonCancel);
	fr.add(buttonOK);
	fr.setSize(300, 150);
	fr.setVisible(true);
	fr.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
	fr.setLocationRelativeTo(null);
	buttonOK.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			fr.setTitle("你单击了确定按钮");
			buttonOK.setText("OK");
		}
	});
	buttonCancel.addActionListener(new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			fr.setTitle("你单击了取消按钮");
			buttonCancel.setText("Cancel");
		}
	});
	
	}
	public static void main(String[] args) {
		new Interaction2();
	}

}


方法三:外部类

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.Jframe;

public class Interaction5 {//外部类
	Jframe fr;
	JButton buttonOK,buttonCancel ;
	buttonActionListener OKListener,CancelListener;
	
	Interaction5(){
	fr=new Jframe("事件响应");
	fr.setLayout(new FlowLayout());
	buttonOK=new JButton("确定");
	buttonCancel=new JButton("取消");
	fr.add(buttonCancel);
	fr.add(buttonOK);
	fr.setSize(300, 150);
	fr.setVisible(true);
	fr.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
	fr.setLocationRelativeTo(null);
	buttonOK.addActionListener(new buttonListener(this));
	buttonCancel.addActionListener(new buttonListener(this));
	}
	
	public static void main(String[] args) {
		new Interaction3();
	}
}


class buttonListener implements ActionListener{
	private Interaction5 In5;
	public buttonListener(Interaction5 In5) {
		this.In5=In5;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==In5.buttonOK){
			In5.fr.setTitle("你单击了确定按钮");
			In5.buttonOK.setText("OK");
		}
		else if(e.getSource()==In5.buttonCancel) {
			In5.fr.setTitle("你单击了取消按钮");
			In5.buttonCancel.setText("Cancel");
		}	
		
	}
	
}
	


方法四:内部类

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.Jframe;

public class Interaction4 {//内部类
	Jframe fr;
	JButton buttonOK,buttonCancel ;
	buttonActionListener OKListener,CancelListener;
	
	Interaction4(){
	fr=new Jframe("事件响应");
	fr.setLayout(new FlowLayout());
	buttonOK=new JButton("确定");
	buttonCancel=new JButton("取消");
	fr.add(buttonCancel);
	fr.add(buttonOK);
	fr.setSize(300, 150);
	fr.setVisible(true);
	fr.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
	fr.setLocationRelativeTo(null);
	ButtonAction ba=new ButtonAction();
	buttonOK.addActionListener(ba);
	buttonCancel.addActionListener(ba);
	}
	class ButtonAction implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			if(e.getSource()==buttonOK){
				fr.setTitle("你单击了确定按钮");
				buttonOK.setText("OK");
			}
			else if(e.getSource()==buttonCancel) {
				fr.setTitle("你单击了取消按钮");
				buttonCancel.setText("Cancel");
			}	
		}
		
	}
	public static void main(String[] args) {
		new Interaction4();
	}

}

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

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

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