黑马程序员技术交流社区
标题:
关于GUI的问题
[打印本页]
作者:
永恒之翼网络
时间:
2012-5-18 22:20
标题:
关于GUI的问题
package gui;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MouseListenerDemo {
private Frame f;
private Button bt;
MouseListenerDemo(){
init();
}
public void init(){
f = new Frame("MyFrame");
f.setBounds(200, 150, 400, 350);
f.setLayout(new FlowLayout());
bt = new Button("good");
f.add(bt);
myevent();
f.setVisible(true);
}
void myevent(){
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
bt.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Action");
}
});
bt.addMouseListener(new MouseAdapter(){
private int count=0;
public void mouseEntered(MouseEvent e){
System.out.println("enter:"+count++);
}
public void mouseClicked(MouseEvent e){
System.out.println("click!");
}
});
}
public static void main(String[] args) {
new MouseListenerDemo();
}
}
就这个程序,我在激活“good”button的情况下,回车没打印“Action”。然后我执行毕老师的源代码执行,情况也是一样。
import java.awt.*;
import java.awt.event.*;
class MouseDemo
{
private Frame f;
private Button but;
MouseDemo()
{
init();
}
public void init()
{
f = new Frame("mouse demo");
f.setBounds(400,100,500,500);
f.setLayout(new FlowLayout());
but = new Button("mouse demo");
f.add(but);
myEvent();
f.setVisible(true);
}
public void myEvent()
{
//窗体关闭事件。
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("action");
}
});
//给按钮添加一个鼠标事件。
but.addMouseListener(new MouseAdapter()
{
private int count = 1;
public void mouseEntered(MouseEvent e)
{
System.out.println("enter :"+count++);
}
public void mouseClicked(MouseEvent e)
{
if(e.getClickCount()==2)
System.out.println("double click :");
}
});
}
}
class MouseListenerDemo
{
public static void main(String[] args)
{
new MouseDemo();
}
}
毕老师的代码。
作者:
梁小波
时间:
2012-5-18 23:34
我原样运行了一下,貌似没问题file:///C:\Users\lxb\AppData\Roaming\Tencent\Users\544144270\QQ\WinTemp\RichOle\6PK(I(CKKPOAP`W`BH{6D}J.jpgfile:///C:\Users\lxb\AppData\Roaming\Tencent\Users\544144270\QQ\WinTemp\RichOle\6PK(I(CKKPOAP`W`BH{6D}J.jpg
6PK(I(CKKPOAP`W`BH{6D}J.jpg
(4.14 KB, 下载次数: 48)
下载附件
2012-5-18 23:33 上传
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2