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