为什么我的JFrame无法响应鼠标和窗口的更改? [英] Why won't my JFrame respond to mouse and window changes?

查看:149
本文介绍了为什么我的JFrame无法响应鼠标和窗口的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class wind extends JFrame implements ComponentListener, MouseListener
{
    JButton button;
    JLabel label;
    public wind()
    {
        // initialise instance variables
        setTitle("My First Window!");
        setSize(400, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        JPanel content = new JPanel();
        content.setLayout(new FlowLayout());
        content.addComponentListener(this);
        content.addMouseListener(this);

        label = new JLabel("My First Window");
        content.add(label);
        label.addComponentListener(this);
        button = new JButton("Click If You Wish To Live!");
        button.addMouseListener(this);
        content.add(button)
        setContentPane(content);

    }
    public void componentHidden(ComponentEvent e){
        try{wait(100);}
        catch(InterruptedException error){}
        button.setText("Hidden!");
    }
    public void componentShown(ComponentEvent e){
        try{wait(100);}
        catch(InterruptedException error){}
        button.setText("Shown!");
    }
    public void componentResized(ComponentEvent e){
        try{wait(100);}
        catch(InterruptedException error){}
        button.setText("Resized!");
    }
    public void componentMoved(ComponentEvent e){
        try{wait(100);}
        catch(InterruptedException error){}
        button.setText("Moved!");
    }
    public void mouseExited(MouseEvent e){
        try{wait(100);}
        catch(InterruptedException error){}
        label.setText("Exited!");
    }
    public void mouseEntered(MouseEvent e){
        try{wait(100);}
        catch(InterruptedException error){}
        label.setText("Entered!");
    }
    public void mousePressed(MouseEvent e){
        try{wait(100);}
        catch(InterruptedException error){}
        label.setText("pressed at: "+e.getX()+" "+e.getY());
    }
    public void mouseReleased(MouseEvent e){
        try{wait(100);}
        catch(InterruptedException error){}
        label.setText("Released!");
        label.setLocation(e.getX(), e.getY());
    }
    public void mouseClicked(MouseEvent e){}
}

它不会响应鼠标或窗口的大小调整,隐藏或移动. 此外该按钮未显示.已修复!我刚刚开始学习Java的JFrame和其他图形,因此我不知道我的代码有什么问题,尽管我怀疑这与我制作按钮并将侦听器添加到对象的方式有关.有人可以解释为什么这样做,以及如何解决它.预先谢谢你!

It won't respond to the mouse or window re-sizing, hiding, or moving. Furthermore the button is not being displayed. fixed! I am just starting to learn about Java's JFrame and other graphics so I have no idea what's wrong with my code, although I suspect it has something to do with the way I made the button and added the listeners to the objects. Could someone please explain why it does this, and how to fix it. Thank you in advance!

推荐答案

您的问题是您没有正确使用wait函数.尝试将javax.swing.Timer类(在Swing程序中也称为Swing Timer for delays)用于简单动画重复动作.有关更多信息,请参见有关stackoverflow的以下示例: Java Wait Function

Your problem is that you are using the wait function not correctly. Try to use the class javax.swing.Timer also known as a Swing Timer for delays in Swing programs, for simple animations and for repetitive actions. For more information see this example on stackoverflow: Java Wait Function

ActionListener添加到JButton的一种可能方法:

One possible way to add a ActionListener to a JButton:

// You are adding an ActionListener to the button
//Using the method addActionListener and a anonymous inner class
button.addActionListener(new ActionListener() {//anonymous inner class

    @Override
    public void actionPerformed(ActionEvent arg0)
    {
        button.setText("Text modified by an event called ActionEvent!");
    }
});

这篇关于为什么我的JFrame无法响应鼠标和窗口的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆