画笔
package GUI;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setBounds(200,200,500,500);
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@Override
public void paint(Graphics g) {
g.setColor(Color.red);
g.fill3DRect(100,100,100,100,true);
}
}
鼠标监听
package GUI;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Iterator;
public class TestMouse {
public static void main(String[] args) {
new MyFrame1().loadFrame();
}
}
class MyFrame1 extends Frame{
ArrayList points;
public void loadFrame(){
setBounds(100,100,500,500);
setVisible(true);
points = new ArrayList<>();
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.addMouseListener(new MouseListener());
}
@Override
public void paint(Graphics g) {
Iterator iterator = points.iterator();
while(iterator.hasNext()){
Point point = (Point) iterator.next();
g.setColor(Color.RED);
g.fillOval(point.x,point.y,10,10);
}
}
public void addPaint(Point point){
points.add(point);
}
private class MouseListener extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
MyFrame1 frame1 =(MyFrame1) e.getSource();
frame1.addPaint(new Point(e.getX(),e.getY()));
frame1.repaint();
}
}
}
窗口监听
package GUI;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
public static void main(String[] args) {
new FrameWindow().FrameWindow();
}
}
class FrameWindow extends Frame{
public void FrameWindow(){
setBackground(Color.blue);
setBounds(100,100,300,300);
setVisible(true);
this.addWindowListener(
new WindowAdapter() {
@Override
public void windowActivated(WindowEvent e) {
FrameWindow source = (FrameWindow) e.getSource();
source.setTitle("激活");
System.out.println("windowClosed");
super.windowActivated(e);
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
System.exit(0);
}
}
);
}
}
键盘监听
package GUI;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class TestKey {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame(){
setBounds(100,100,500,500);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode==KeyEvent.VK_UP){
System.out.println("上键");
}
}
});
}
}
JFrame
package GUI;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class DialogDemon extends JFrame {
public DialogDemon(){
this.setVisible(true);
this.setBounds(100,100,500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(null);
JButton button = new JButton("点击");
button.setBounds(10,10,100,100);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new MyDiYiTanChuang();
}
});
container.add(button);
}
}
public class DialogDemo {
public static void main(String[] args) {
new DialogDemon();
}
}
class MyDiYiTanChuang extends JDialog{
public MyDiYiTanChuang(){
this.setVisible(true);
this.setBounds(200,200,300,300);
Container container = this.getContentPane();
container.setLayout(null);
JLabel label = new JLabel("加油");
label.setBounds(100,100,100,100);
container.add(label);
}
}