不太明白搂主的意思,你是说在点击按钮后开始监听鼠标的动作吗?如果是WEB页面的话,可以写成<input id="b01" type="button" value="Click Me" onclick="document.body.onmouseover = function() {document.write('MOUSE MOVE!!!!!!!!!!')}">,如果不是WEB页面的话,可以用addEventListener来添加
要用这几个函数实现: public void mouseExited(MouseEvent en){} public void mouseEntered(MouseEvent en){} public void mouseReleased(MouseEvent en){} public void mousePressed(MouseEvent en){} public void mouseClicked(MouseEvent en) 获得en.getButton()的值,在相应的事件里写入相应的代码,具体的可以参考JDK API。
点击之后,再让他运行监听方法内的实质内容
嗯 好的 十分感谢 我才学java 还不清楚方向,怎么收你发的实例呢
/**
* @param args
*/
JTextArea text;
JButton button;
JButton button2;
JButton button3;
RandomAccessFile inout = null;
Example4() {
super("顺序的循环输出");
Container con = this.getContentPane();
text = new JTextArea(20, 30);
text.setBackground(Color.cyan);
button = new JButton("排序后的数组显示在文本框中");
button2 = new JButton("清空数据");
button3 = new JButton("退出");
button.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
con.setLayout(new BorderLayout());
con.setSize(100, 200);
JScrollPane jsp = new JScrollPane(text);
con.add(jsp, BorderLayout.NORTH);
JPanel p=new JPanel();
p.add(button);
p.add(button2);
p.add(button3);
con.add(p,BorderLayout.SOUTH);
this.setVisible(true);
this.pack();
} public static void main(String[] args) {
Example4 example = new Example4(); }
public void actionPerformed(ActionEvent arg0) {
if (arg0.getSource() == button) {
long data[] = new long[] { 151, 20, 187, 102, 30, 100, -54, 58, 780 };
try {
inout = new RandomAccessFile("test.txt", "rw");
for (int i = 0; i < data.length; i++) {
inout.writeLong(data[i]);
System.out.println("整数文件写入成功!");
}
text.setText(null);
for (int j = data.length - 1; j >= 0; j--) {
inout.seek(j*8);
text.append(String.valueOf(inout.readLong()) + " ");
} } catch (FileNotFoundException e1) {
System.out.println("文件没有发现异常" + e1);
} catch (IOException e2) {
System.out.println("文件读写错误!" + e2);
} }
if (arg0.getSource() == button2) {
text.setText(null);
}
if(arg0.getSource()==button3){
try {
inout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
button.removeActionListener(this);
System.exit(1);
}
}
public void mouseExited(MouseEvent en){}
public void mouseEntered(MouseEvent en){}
public void mouseReleased(MouseEvent en){}
public void mousePressed(MouseEvent en){}
public void mouseClicked(MouseEvent en)
获得en.getButton()的值,在相应的事件里写入相应的代码,具体的可以参考JDK API。
@Override
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
popMenu.show(e.getComponent(), e.getX(),e.getY());
}
}
});