java GUI 应用程序的对鼠标事件不响应!!!
packageJTree;importjavax.swing.*;importorg.w3c.dom.views.AbstractView;importcom.sun.i...
package JTree;
import javax.swing.*;
import org.w3c.dom.views.AbstractView;
import com.sun.istack.internal.Nullable;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/*简单的树形文件夹存储 实现 */
public class TreeDemo1 {
public TreeDemo1(){
JFrame f=new JFrame("TreeDemo1");
Container contentPane=f.getContentPane(); //放在容器
String[] s2={"本机磁盘(C:)","本机磁盘(D:)","本机磁盘(E:)"};
String[] s3={"奇摩站","职棒消息","网络书店"};
Hashtable hashtable1=new Hashtable();
Hashtable hashtable2=new Hashtable();
hashtable1.put("我的电脑",s2);
hashtable1.put("收藏夹",hashtable2);
hashtable2.put("网站列表",s3); //比较特别的地方
Font font = new Font("Dialog", Font.PLAIN, 12);
Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
JButton b=new JButton ("ok");
/**定义widnows界面**/
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
if (UIManager.get(key) instanceof Font) {
UIManager.put(key, font );
}
}
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
// UIManager.setLookAndFeel("com.sun.java.swing.plaf.metal.MetalLookAndFeel");
} catch(Exception el){
System.exit(0);
}
JTree tree=new JTree(hashtable1);
JScrollPane scrollPane=new JScrollPane(); //可有可无的
scrollPane.setViewportView(tree);
contentPane.add(scrollPane);
f.pack();
f.setVisible(true);
/**定义widnows界面**/
f.addWindowListener(new WindowAdapter(){ //常用这个函数
public void windowClosing(WindowEvent e){
System.out.println("exit...");
System.exit(0);
} });
f.addMouseListener(new MouseMonitor1());
}
/*private void addMouseListener(MouseMonitor mouseMonitor) {
// TODO Auto-generated method stub
}*/
class MouseMonitor1 implements MouseListener {
public void mouseEntered(MouseEvent me) //鼠标进入事件
{
System.out.println("树状文件夹被打开11");
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("树状文件夹被打开22");
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("树状文件夹被打开22");
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("树状文件夹被打开22");
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("树状文件夹被打开22");
}
}
public static void main(String[] args){
new TreeDemo1();
}
}
我按退出是可以响应 在控制台显示 " exit.."
但是我按鼠标时他没有响应,不知道为什么? 我也在MouseMonitor1类里定义了键盘敲击事件了啊!!!! 展开
import javax.swing.*;
import org.w3c.dom.views.AbstractView;
import com.sun.istack.internal.Nullable;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/*简单的树形文件夹存储 实现 */
public class TreeDemo1 {
public TreeDemo1(){
JFrame f=new JFrame("TreeDemo1");
Container contentPane=f.getContentPane(); //放在容器
String[] s2={"本机磁盘(C:)","本机磁盘(D:)","本机磁盘(E:)"};
String[] s3={"奇摩站","职棒消息","网络书店"};
Hashtable hashtable1=new Hashtable();
Hashtable hashtable2=new Hashtable();
hashtable1.put("我的电脑",s2);
hashtable1.put("收藏夹",hashtable2);
hashtable2.put("网站列表",s3); //比较特别的地方
Font font = new Font("Dialog", Font.PLAIN, 12);
Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
JButton b=new JButton ("ok");
/**定义widnows界面**/
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
if (UIManager.get(key) instanceof Font) {
UIManager.put(key, font );
}
}
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
// UIManager.setLookAndFeel("com.sun.java.swing.plaf.metal.MetalLookAndFeel");
} catch(Exception el){
System.exit(0);
}
JTree tree=new JTree(hashtable1);
JScrollPane scrollPane=new JScrollPane(); //可有可无的
scrollPane.setViewportView(tree);
contentPane.add(scrollPane);
f.pack();
f.setVisible(true);
/**定义widnows界面**/
f.addWindowListener(new WindowAdapter(){ //常用这个函数
public void windowClosing(WindowEvent e){
System.out.println("exit...");
System.exit(0);
} });
f.addMouseListener(new MouseMonitor1());
}
/*private void addMouseListener(MouseMonitor mouseMonitor) {
// TODO Auto-generated method stub
}*/
class MouseMonitor1 implements MouseListener {
public void mouseEntered(MouseEvent me) //鼠标进入事件
{
System.out.println("树状文件夹被打开11");
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("树状文件夹被打开22");
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("树状文件夹被打开22");
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("树状文件夹被打开22");
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("树状文件夹被打开22");
}
}
public static void main(String[] args){
new TreeDemo1();
}
}
我按退出是可以响应 在控制台显示 " exit.."
但是我按鼠标时他没有响应,不知道为什么? 我也在MouseMonitor1类里定义了键盘敲击事件了啊!!!! 展开
3个回答
展开全部
好像以前遇过这个问题 addMouseListener 不能够直接加在JFrame对象上的
试一下用TreeDemo1继承JFrame
然后用this.addMouseListener(new MouseMonitor1());
-------------
因为正在用的电脑没配置JAVA环境 不能调试 凭记忆说的 如有错请多多包涵
试一下用TreeDemo1继承JFrame
然后用this.addMouseListener(new MouseMonitor1());
-------------
因为正在用的电脑没配置JAVA环境 不能调试 凭记忆说的 如有错请多多包涵
追问
不行啊,this.addMouseListener(new MouseMonitor1());这段执行不了,而且为什么我的关闭窗口事件 他能响应呢?qq聊 1215303889
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询