GUI-监听
按下按钮,触发事件
public class Listen01 {
public static void main(String[] args) {
Frame frame=new Frame("Listen");
frame.setVisible(true);
//按下按钮,触发事件
Button bt1=new Button("bt1");
//addActionListener()需要一个ActionListener,所以我们要创建一个类实现接口ActionListener
bt1.addActionListener(new MyListener());
frame.add(bt1,BorderLayout.CENTER);
}
}
class MyListener implements ActionListener{
多个按钮可以共用一个事件监听,然后对这些按钮进行判断
public class Listener {
public static void main(String[] args) {
Frame frame=new Frame("listener");
frame.setBounds(100,100,300,300);
frame.setVisible(true);
Button bt1=new Button("start");
Button bt2=new Button("stop");
bt1.setActionCommand("start");
bt2.setActionCommand("stop");
bt1.addActionListener(new MyListener2());
bt2.addActionListener(new MyListener2());
frame.add(bt1,BorderLayout.EAST);
frame.add(bt2,BorderLayout.WEST);
}
}
class MyListener2 implements ActionListener{
e.getActionCommand():获取按钮信息
e.setActionCommand():设置按钮信息
//默认值是给按钮设置的名称
抽离关闭窗口方法
private static void close(Frame frame){
frame.addWindowListener(new WindowAdapter() {
输入框监听
后台获取输入的内容
public class TextListener {
public static void main(String[] args) {
//启动
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
pack();
setVisible(true);
//创建一个文本框
TextField textField=new TextField();
add(textField);
//监听文本框
textField.addActionListener(new MyAction());
//设置编码
//输入时,输入内容不显示;控制台可以获取
textField.setEchoChar('*');
}
}
class MyAction implements ActionListener{
简易计算两数相加
组合大于继承,优先使用组合
此时A可以使用B的内容
class A{
public B b;
}
public class Calculate {
public static void main(String[] args) {
new MyFrame1("Calculate");
}
}
//计算器类
class MyFrame1 extends Frame{
public MyFrame1(String name){
super(name);
addWindowListener(new WindowAdapter() {
优化
传入参数变成传入Calculate类对象,直接对类操作
public class Calculate2 {
public static void main(String[] args) {
new Calculate1().load();
}
}
//计算器类
class Calculate1 extends Frame {
//属性
//3个文本框
TextField t1=new TextField(10);//最大可以填10个字符
TextField t2=new TextField(10);
TextField t3=new TextField(20);
//1个标签
Label l1=new Label("+");
//1个按钮
Button bt=new Button("=");
//方法
public void load(){
setBounds(100,100,500,200);
setVisible(true);
setLayout(new FlowLayout());
addWindowListener(new WindowAdapter() {
使用内部类
使用内部类,可以直接获取对象的属性,简化代码
private class Act implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
//点击触发计算
int a;
a=(Integer.parseInt(t1.getText())+(Integer.parseInt(t2.getText())));
//结果显示在第三个变量上
t3.setText(String.valueOf(a));
//清除前两个框
t1.setText("");
t2.setText("");
}
}
内部类最大优势就是可以直接访问外部属性
鼠标监听
实现鼠标画画
思路:1.添加鼠标监听
2.获取鼠标按压的点的坐标位置
3.将这个点存在集合里
4.从集合里取出该点,在窗口上画出来
public class Mouse {
public static void main(String[] args) {
new Mouse1("paint");
}
}
class Mouse1 extends Frame{
ArrayList point;
public Mouse1(String name){
super(name);
setBounds(200,200,600,400);
setVisible(true);
this.addMouseListener(new Mouse());
//将获取到的所有点存在集合里
point=new ArrayList<>();
}
//画出这个点
@Override
public void paint(Graphics g) {
//迭代器获取点
Iterator iterator=point.iterator();
while(iterator.hasNext()){
Point point=(Point) iterator.next();
g.setColor(Color.red);
g.fillOval(point.x,point.y,10,10);
}
}
//获取按压的这个点,继承适配器
private class Mouse extends MouseAdapter {
public void mousePressed(MouseEvent e) {
point.add(new Point(e.getX(),e.getY()));
//让他每次点击都重新画一遍,否则页面一直处于初始化状态没有变化
repaint();
}
}
}
窗口监听
public class Window {
public static void main(String[] args) {
new TestWindowFrame();
}
}
class TestWindowFrame extends Frame{
public TestWindowFrame(){
setBounds(100,100,400,300);
setBackground(Color.GRAY);
setVisible(true);
//使用匿名内部类
addWindowListener(new WindowListener() {
@Override
//打开窗口首先执行
public void windowOpened(WindowEvent e) {
System.out.println("窗口打开");
}
@Override
//点击关闭按钮执行
public void windowClosing(WindowEvent e) {
System.out.println("窗口正在关闭");
}
@Override
//窗口已经关闭
public void windowClosed(WindowEvent e) {
System.out.println("窗口已经关闭");
}
@Override
//隐藏后再次开启窗口会执行
public void windowActivated(WindowEvent e) {
System.out.println("状态激活");
}
}
}
键盘监听
public class Keyboard {
public static void main(String[] args) {
new Key();
}
}
class Key extends Frame{
public Key(){
setBounds(100,100,400,400);
setVisible(true);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int keycode = e.getKeyCode();//获取键盘的编码值
//使用VK_判断按下的按钮是哪一个键
if (keycode == KeyEvent.VK_ENTER) {
System.out.println("键盘被按下Enter键");
}
System.out.println(keycode);
}
});
}
}

浙公网安备 33010602011771号