问题描述
这是我的代码:
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. 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无法响应鼠标和窗口的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!