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

}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Asio otus

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值