awt

时间:2021-05-24 11:28:52
public class MouseTest extends Frame{

    private static final long serialVersionUID = 5437685336595276319L;
List<Point> list = new ArrayList<Point>(); public void addPoint(Point p){ list.add(p);
} /**
* 重写画笔
*/
@Override
public void paint(Graphics g) {
Color c = g.getColor();
for (Point point : list) {
g.setColor(Color.white);
g.fillOval(point.x, point.y, 10, 10);
}
g.setColor(c); } public static void main(String[] args) {
final MouseTest f = new MouseTest();
f.setBackground(Color.black);
f.setLayout(null);
f.setBounds(600, 100, 300, 300);
f.setVisible(true);
//窗口添加鼠标点击事件
f.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
MouseTest f = (MouseTest) e.getSource();
f.addPoint(new Point(e.getX(), e.getY()));
//frame重画
f.repaint();
System.out.println("鼠标点击");
}
}); f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
MouseTest f2 = (MouseTest) e.getSource();
f2.setVisible(false);
//f.setVisible(false);
//0正常退出,-1非正常退出
System.exit(0); }
}); } }
public class FrameTest extends Frame{

    @Override
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.green);
g.drawLine(100, 100, 200, 200);
g.setColor(c); //super.paint(g);
} public void createFrame(String title,int x,int y,int w,int h){
this.setTitle(title);
this.setLayout(null); //this.setLayout(new FlowLayout(FlowLayout.CENTER,20,20)); //new BorderLayout();
//BorderLayout.CENTER;
//new GridLayout(3, 2);3行两列 this.setBounds(x, y, w, h);
this.setBackground(Color.blue);
Panel panel = new Panel();
panel.setBounds(w/4, h/4, w/2, h/2);
panel.setBackground(Color.yellow);
this.add(panel); //this.add(panel, BorderLayout.NORTH); this.setVisible(true); } public static void main(String[] args) {
FrameTest frameTest = new FrameTest();
frameTest.createFrame("测试", 600, 400, 600, 600); /*Frame frame = new FrameTest();
//frame.setLayout(null);
frame.setBounds(200, 200, 300, 300);
Button b = new Button("denglu"); b.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
System.out.println("点击登陆"); }
}); frame.add(b); TextField field = new TextField();
field.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
TextField tf = (TextField) e.getSource();
System.out.println(tf.getText());
tf.setText("");
}
});
field.setEchoChar('*');
frame.add(field); //frame.pack();
frame.setVisible(true);*/ } }

键盘

import java.awt.Frame;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; public class KeyTest extends Frame{ public static void main(String[] args) { KeyTest k = new KeyTest(); k.setLayout(null);
k.setBounds(600, 100, 300, 300);
k.setVisible(true);
//键盘按下事件
k.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
//System.out.println("键盘按下-->"+e.getKeyCode());
//w s a d 上下左右
switch (e.getKeyCode()) {
case 87:
System.out.println("上");
break;
case 83:
System.out.println("下");
break;
case 65:
System.out.println("左");
break;
case 68:
System.out.println("右");
break;
default:
break;
} }
}); k.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
KeyTest k2 = (KeyTest) e.getSource();
k2.setVisible(false);
System.exit(0);
}
});
} }