LOADING

正在加载喵~

week9

2025/4/19 未来与梦 未标记

[collapse title”第一题”] java import java.awt.event.; import javax.swing.; import java.awt.; public class Ex8_1 extends JFrame implements WindowListener { public Ex8_1() { setLayout(n…

[collapse title=”第一题”]

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) {}
}

[/collapse]

[collapse title=”第二题”]

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

[/collapse]