目录
swing链接https://blog.csdn.net/weixin_46919419/article/details/112390539
事件监听
当某个事情发生的时候,干什么?
public class TestActionEvent {
public static void main(String[] args) {
//按下按钮,触发一些事件
Frame frame = new Frame();
Button button = new Button("button");
button.addActionListener((e)->{
System.out.println("马建华大帅比");
});//使用Lambda表达式
frame.add(button,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
windowClose(frame);
}
//窗口关闭方法
public static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
点击后
public static void main(String[] args) {
//两个按钮,实现同一个监听
//开始 停止
Frame frame = new Frame("开始停止");
Button button1 = new Button("Start");
Button button2 = new Button("stop");
MyMonitor myMonitor = new MyMonitor();
//获得按钮的信息
button1.addActionListener(myMonitor);
//event.getActionCommand()中可以自定义被触发时返回的命令,如果不定义则会使用按钮上默认的值
button2.setActionCommand("自定义的返回命令");
button2.addActionListener(myMonitor);
frame.add(button1,BorderLayout.NORTH);
frame.add(button2,BorderLayout.SOUTH);
frame.setVisible(true);
frame.setSize(400,400);
TestActionEvent.windowClose(frame);
}
//这里由于两个按钮都要使用,就使用实现类来完成了,不使用Lambda表达式
static class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("按钮被点击了:msg=>"+e.getActionCommand());
}
}
点击Start
点击stop
输入框监听事件
TextField
public class TestText01 {
public static void main(String[] args) {
//main方法只管启动
new MyFrame();
}
}
class MyFrame extends Frame {
public MyFrame(){
TextField textField = new TextField();
add(textField);
//监听这个文本框输入文字
//且按下enter就会触发这个输入框的事件
textField.addActionListener(new MyActionListener());
//设置替换编码,将输入的字符转换为星号,用于隐藏密码
textField.setEchoChar('*');
setVisible(true);
pack();
}
}
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//多态向下转型,监听啥就得到啥
TextField field = (TextField) e.getSource();//获得一些资源
System.out.println(field.getText());//获得输入框的文本内容并打印
//输入密码完成后将文本设置为空
field.setText("");//null
}
}
输入123456
控制台出现
练习设计简易计算器
使用内部类再次优化,内部类最大的好处就是可以畅通滴访问外部类的所有属性和方法
重点:将监听类作为内部类写入到计算器类的里面
class Calculator extends Frame{
//属性
TextField num1,num2,num3;
//方法
public void loadFrame(){
//3个文本框
num1 = new TextField(10);//最多10个字符
num2 = new TextField(10);
num3 = new TextField(10);
//1个按钮
Button button = new Button("=");
//给button添加一个监听事件
button.addActionListener(new MyCalculatorListener());
//1个标签
Label label = new Label("+");
//设置为流式布局
setLayout(new FlowLayout());
//添加组件
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
//监听器类写作内部类
private class MyCalculatorListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
//1、获得加数和被加数
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
//2、将这个值+法运算后,放到第三个框
num3.setText(""+(n1+n2));
//3、清除前两个数
num1.setText("");
num2.setText("");
}
}
}
效果:
画笔
public class TestPaint {
public static void main(String[] args) {
new MyPaint().load();
}
}
class MyPaint extends Frame {
public void load(){
setBounds(200,200,600,500);
setVisible(true);
}
//画笔
@Override
public void paint(Graphics g){
//画笔,需要有颜色,画笔可以画画
g.setColor(Color.RED);
//画一个园
g.drawOval(100,100,100,100);
//画一个实心的园
g.fillOval(200,100,100,100);
g.setColor(Color.green);
g.fillRect(300,100,100,100);
//养成习惯,画笔用完,还原到最初的颜色
g.setColor(Color.black);
}
}
鼠标监听
目的:想要实现鼠标画画(最简单的点击)
public class TestMouseListener {
public static void main(String[] args) {
new MyMouseFrame("画图");
}
}
class MyMouseFrame extends Frame{
//利用集合存储鼠标点击的集合
ArrayList<Point> points;
//画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点
public MyMouseFrame(String title) {
super(title);
setBounds(200,200,400,400);
//鼠标点击的点
points=new ArrayList<>();
//鼠标监听器,针对这个窗口
this.addMouseListener(new MyMouseListener());
setVisible(true);
}
@Override
public void paint(Graphics g) {
g.setColor(Color.RED);
//画画,监听鼠标的事件
for (Point point : points) {
g.fillOval(point.x,point.y,10,10);
}
}
//适配器模式
private class MyMouseListener extends MouseAdapter{
//鼠标按下,弹起,按住不放
@Override
public void mousePressed(MouseEvent e) {
MyMouseFrame myMouseFrame =(MyMouseFrame) e.getSource();
//这里我们点击的时候,就会在界面上产生一个点
myMouseFrame.addPaint(new Point(e.getX(),e.getY()));
//每次点击鼠标都需要重新画一遍
myMouseFrame.repaint();
}
}
//添加一个点到界面上
public void addPaint(Point point){
points.add(point);
}
}
窗口监听
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame() {
super("窗口监听");
setBounds(200,200,400,400);
setBackground(Color.blue);
addWindowListener(new WindowAdapter() {
//其中Closing关闭窗口、Activated激活窗口,最常用
@Override
public void windowOpened(WindowEvent e) {
System.out.println("windowOpened");
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
System.exit(0);
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("windowClosed");
}
/*@Override
public void windowIconified(WindowEvent e) {
System.out.println("");
}
@Override
public void windowDeiconified(WindowEvent e) {
super.windowDeiconified(e);
}*/
@Override
public void windowActivated(WindowEvent e) {
System.out.println("windowActivated");
WindowFrame source = (WindowFrame) e.getSource();
source.setTitle("被激活了");
}
/* @Override
public void windowDeactivated(WindowEvent e) {
super.windowDeactivated(e);
}
@Override
public void windowStateChanged(WindowEvent e) {
super.windowStateChanged(e);
}
@Override
public void windowGainedFocus(WindowEvent e) {
super.windowGainedFocus(e);
}
@Override
public void windowLostFocus(WindowEvent e) {
super.windowLostFocus(e);
}*/
});
setVisible(true);
}
}
启动窗口时
关闭时
键盘监听
public class TestKeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame {
public KeyFrame() {
super("键盘监听");
setBounds(1,2,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
//获取点击了哪个键
//只需要记VK_xxx
int keyCode = e.getKeyCode();
if (keyCode==KeyEvent.VK_UP){
System.out.println("你按下了上键");
}else if (keyCode==KeyEvent.VK_RIGHT) System.out.println("你按了右键");
else if (keyCode==KeyEvent.VK_LEFT) System.out.println("你按了左键");
else if (keyCode==KeyEvent.VK_DOWN) System.out.println("你按了下键");
}
});
}
}
按下上下左右后