package GUI;
import java.awt.Button;
import java.awt.Desktop;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
public class KeybroadandmouseDemo {
Frame f = null;
Button b = null;
Desktop dt = null ;
File file = null;
public void init()
{
f = new Frame("my Frame");
f.setBounds(300, 300, 600, 500);
f.setLayout(new FlowLayout());
b = new Button();
f.add(b);
f.setVisible(true);
file = new File("C:\\Users\\yy\\Desktop\\练习四.txt");
}
public void myEvent() {
// TODO Auto-generated method stub
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
b.addMouseListener(new MouseAdapter(){
private int count = 0 ;
public void mouseEntered(MouseEvent e)
{
System.out.println("鼠标进入button范围!"+count++);
}
public void mouseClicked(MouseEvent e)
{
System.out.println("我点!开启浏览器!");
try {
dt.open(file);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
}
public static void main(String args[]){
KeybroadandmouseDemo make = new KeybroadandmouseDemo();
make.init();
make.myEvent();
}
问题:为什么会报错,应该是我想打开一个文件,但是报错。能告诉我原因和解决方法么?急等 |
|