第一题
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Ex8_1 extends JFrame implements WindowListener {
public Ex8_1() {
setLayout(new FlowLayout());
setBounds(0, 0, 200, 200);
setVisible(true);
this.addWindowListener(this);
}
public static void main(String args[]) {
new Ex8_1();
}
public void windowClosing(WindowEvent e) {
System.out.println("The window closing ");
System.exit(0);
}
public void windowOpened(WindowEvent e) {
System.out.println("The window opened");
}
public void windowDeactivated(WindowEvent e) {
System.out.println("The window deactivated");
}
public void windowActivated(WindowEvent e) {
System.out.println("The window activated");
}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
}
第二题
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Ex8_2 extends JFrame implements ActionListener {
static Ex8_2 frm = new Ex8_2();
static JButton btn1 = new JButton("Yellow");
static JButton btn2 = new JButton("Green");
public static void main(String args[]) {
btn1.addActionListener(frm);
btn2.addActionListener(frm);
frm.setTitle("Action Event");
frm.setLayout(new FlowLayout(FlowLayout.CENTER));
frm.add(btn1);
frm.add(btn2);
frm.setSize(200, 150);
frm.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
Object btn = e.getSource();
if (btn == btn1) {
frm.getContentPane().setBackground(Color.YELLOW);
} else if (btn == btn2) {
frm.getContentPane().setBackground(Color.GREEN);
}
}
}
var es = document.getElementById('es');
var ctx = es.getContext('2d');
ctx.clearRect(0, 0, 39, 23);
ctx.drawImage(gfd[0], 0, 0); // 百位:0
ctx.drawImage(gfd[1], 13, 0); // 十位:1
ctx.drawImage(gfd[6], 26, 0); // 个位:6