一个代码显示效果不是所想要的,求助
图形界面上有一个按钮,当鼠标移到按钮上时按钮掩藏,当鼠标离开按钮时按钮显示。下面是我的代码,不过不是我想要的效果,大侠们帮忙改正一下,最好是先说下思路,说一下我的思路为什么错了。程序代码:
import java.awt.*;
import java.awt.event.*;
public class shu145_8 extends Frame implements WindowListener{
Button b;
Panel p;
public shu145_8(String title){
super(title);
b=new Button("按钮");
p=new Panel();
p.add(b,"Center");
add(p);
addWindowListener(this);
b.addMouseListener(new MouseHandler());
}
public static void main(String args[]){
shu145_8 me=new shu145_8("鼠标");
me.setSize(200,200);
me.setVisible(true);
}
public void windowClosing(WindowEvent e){
System.exit(0);
}
public void windowClosed(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowOpened(WindowEvent e){}
class MouseHandler implements MouseListener{
public void mouseClicked(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseExited(MouseEvent e){
b.setVisible(true);
}
public void mouseEntered(MouseEvent e){
b.setVisible(false);
}
}
}
如果我在开始就按钮掩藏import java.awt.event.*;
public class shu145_8 extends Frame implements WindowListener{
Button b;
Panel p;
public shu145_8(String title){
super(title);
b=new Button("按钮");
p=new Panel();
p.add(b,"Center");
add(p);
addWindowListener(this);
b.addMouseListener(new MouseHandler());
}
public static void main(String args[]){
shu145_8 me=new shu145_8("鼠标");
me.setSize(200,200);
me.setVisible(true);
}
public void windowClosing(WindowEvent e){
System.exit(0);
}
public void windowClosed(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowOpened(WindowEvent e){}
class MouseHandler implements MouseListener{
public void mouseClicked(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseExited(MouseEvent e){
b.setVisible(true);
}
public void mouseEntered(MouseEvent e){
b.setVisible(false);
}
}
}
b.setVisible(false);
然后再运行,效果是鼠标到按钮上就消失了,不过鼠标离开按钮时,按钮也不显示出来了,
----------------解决方案--------------------------------------------------------
程序代码:
import java.awt.Button;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class shu145_8 extends Frame {
Button b;
Panel p;
public shu145_8(String title) {
super(title);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
b = new Button("按钮");
p = new Panel();
p.add(b, "Center");
p.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
int y=b.getLocation().y;
int x=b.getLocation().x;
int ex = e.getPoint().x;
int ey = e.getPoint().y;
int bx = b.getPreferredSize().width;
int by = b.getPreferredSize().height;
if(ex>=x&&ey>=y&&ex<=x+bx&&ey<=y+by)
b.setVisible(false);
else
b.setVisible(true);
}});
add(p);
b.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent arg0) {
b.setVisible(false);
}
});
}
public static void main(String args[]) {
shu145_8 me = new shu145_8("鼠标");
me.setSize(200, 200);
me.setVisible(true);
}
}
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class shu145_8 extends Frame {
Button b;
Panel p;
public shu145_8(String title) {
super(title);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
b = new Button("按钮");
p = new Panel();
p.add(b, "Center");
p.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
int y=b.getLocation().y;
int x=b.getLocation().x;
int ex = e.getPoint().x;
int ey = e.getPoint().y;
int bx = b.getPreferredSize().width;
int by = b.getPreferredSize().height;
if(ex>=x&&ey>=y&&ex<=x+bx&&ey<=y+by)
b.setVisible(false);
else
b.setVisible(true);
}});
add(p);
b.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent arg0) {
b.setVisible(false);
}
});
}
public static void main(String args[]) {
shu145_8 me = new shu145_8("鼠标");
me.setSize(200, 200);
me.setVisible(true);
}
}
我没去查看源代码,不知道setVisible(false)是怎么实现的。不过感觉setVisible(false)后,按钮隐藏起来,其鼠标事件的操作范围也就消失了,再稍微移动一下,就触发了exit了,而exit后又触发了setVisible(false)。所以楼主的代码就会错误了。
----------------解决方案--------------------------------------------------------
setvisible(false)就触发mouse exit啦。这样是不行的。
----------------解决方案--------------------------------------------------------
我有听说是awt不支持双缓冲,所以有闪屏的情况。
谢谢你们的解答,
----------------解决方案--------------------------------------------------------
就算你改成swing也是一样的,不知道这个题目怎么跟闪屏相关了
----------------解决方案--------------------------------------------------------