一、实验要求:
1. 创建一个小程序,该小程序在IE窗口中的显示大小为400*400
2. 小程序中有两个文本框。
3. 当鼠标移入小程序所在的区域时,在两个文本中分别显示当前鼠标的X、Y坐标。
4. 当鼠标移出小程序所在的区域时,把两个文本框中的数值清零。
import java.awt.*;
import java.awt.event.*;class MyFrame extends Frame {
public MyFrame() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
} public static void main(String args[]) {
MyFrame mainFrame = new MyFrame();
mainFrame.setSize(400, 400);
mainFrame.setTitle("Title");
mainFrame.setLayout(new FlowLayout());
mainFrame.add(new TextField());
mainFrame.add(new TextField());
mainFrame.setVisible(true);
}
}
1. 创建一个小程序,该小程序在IE窗口中的显示大小为400*400
2. 小程序中有两个文本框。
3. 当鼠标移入小程序所在的区域时,在两个文本中分别显示当前鼠标的X、Y坐标。
4. 当鼠标移出小程序所在的区域时,把两个文本框中的数值清零。
import java.awt.*;
import java.awt.event.*;class MyFrame extends Frame {
public MyFrame() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
} public static void main(String args[]) {
MyFrame mainFrame = new MyFrame();
mainFrame.setSize(400, 400);
mainFrame.setTitle("Title");
mainFrame.setLayout(new FlowLayout());
mainFrame.add(new TextField());
mainFrame.add(new TextField());
mainFrame.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
class myJF extends JFrame
{
JTextField jtfX;
JTextField jtfY;
public myJF(String title)
{
super("title");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c=this.getContentPane();
c.setLayout(new FlowLayout(FlowLayout.CENTER));
jtfX=new JTextField(7);
jtfY=new JTextField(7);
c.add(jtfX);
c.add(jtfY);
myMMA mma=new myMMA();
this.addMouseMotionListener(mma);
myMA ma=new myMA();
this.addMouseListener(ma);
}
class myMMA extends MouseMotionAdapter
{
public void mouseMoved(MouseEvent e)
{
jtfX.setText(String.valueOf(e.getX()));
jtfY.setText(String.valueOf(e.getY()));
}
}
class myMA extends MouseAdapter
{
public void mouseExited(MouseEvent e)
{
jtfX.setText("");
jtfY.setText("");
}
}
}
public class start
{
public static void main(String[] args)
{
myJF.setDefaultLookAndFeelDecorated(true);
myJF tmp=new myJF("测试");
tmp.setSize(500,400);
tmp.setBackground(Color.darkGray);
tmp.setVisible(true);
}
}