module-info:
module MainTest {
requires java.desktop;
}
package xyz.zlqhe.main;
import java.awt.*;
import java.awt.event.*;
public class Main {
public static void main(String[] args) {
Frame f = new Frame("我的第一个图形化页面");
f.setSize(400,600);
f.setLocation(300,50);
f.setIconImage(Toolkit.getDefaultToolkit().createImage("C:\\Users\\Ashine\\Pictures\\Saved Pictures\\CSDN头像.jpg"));
Button b1 = new Button("ShutDown");
Button b2 = new Button("Space");
Button b3 = new Button("KeyAndMouse");
f.add(b2);
f.add(b1);
f.add(b3);
f.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
f.setLayout(new FlowLayout());
b1.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
super.mouseReleased(e);
System.exit(0);
}
});
b2.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
if(e.getKeyCode() == KeyEvent.VK_ENTER){
System.exit(0);
}
//System.out.println(e.getKeyCode());
}
});
//ActionListener可以同时监听鼠标和键盘,但是键盘监听的只能是空格
b3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
f.setVisible(true);
}
}