JAVA为何鼠标事件没响应
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassJButtonExample...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JButtonExample implements ActionListener, MouseListener {
JFrame f;
JButton b;
JTextField t,t1;
int tag = 0;
public static void main(String[] args) {
JButtonExample tb = new JButtonExample();
tb.go();
}
public void go() {
JFrame f = new JFrame("button");
Container content = f.getContentPane();
JButton b = new JButton("sample");
b.addActionListener(this);
content.add(b, "South");
t = new JTextField();
t1 = new JTextField();
content.add(t, "Center");
content.add(t1, "North");
f.addMouseListener(this);
// String s="press the button";
// t.setText(s);
f.setSize(300, 300);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String s1 = "press the button";
String s2 = "press ";
if (tag == 0) {
t.setText(s1);
tag = 1;
} else {
t.setText(s2);
tag = 0;
}
}
public void mouseClicked(MouseEvent e) {
String s3 = "button";
//t.setText(s3);
System.out.println("enter");
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
String s2 = "press ";
t1.setText(s2);
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
} 展开
import java.awt.event.*;
import javax.swing.*;
public class JButtonExample implements ActionListener, MouseListener {
JFrame f;
JButton b;
JTextField t,t1;
int tag = 0;
public static void main(String[] args) {
JButtonExample tb = new JButtonExample();
tb.go();
}
public void go() {
JFrame f = new JFrame("button");
Container content = f.getContentPane();
JButton b = new JButton("sample");
b.addActionListener(this);
content.add(b, "South");
t = new JTextField();
t1 = new JTextField();
content.add(t, "Center");
content.add(t1, "North");
f.addMouseListener(this);
// String s="press the button";
// t.setText(s);
f.setSize(300, 300);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String s1 = "press the button";
String s2 = "press ";
if (tag == 0) {
t.setText(s1);
tag = 1;
} else {
t.setText(s2);
tag = 0;
}
}
public void mouseClicked(MouseEvent e) {
String s3 = "button";
//t.setText(s3);
System.out.println("enter");
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
String s2 = "press ";
t1.setText(s2);
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
} 展开
2个回答
展开全部
修改
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JButtonExample implements ActionListener, MouseListener {
JFrame f;
JButton b;
JTextField t, t1;
int tag = 0;
public static void main(String[] args) {
JButtonExample tb = new JButtonExample();
tb.go();
}
public void go() {
JFrame f = new JFrame("button");
Container content = f.getContentPane();
JButton b = new JButton("sample");
b.addActionListener(this);
content.add(b, "South");
t = new JTextField();
t1 = new JTextField();
content.add(t, "Center");
content.add(t1, "North");
b.addMouseListener(this);
// String s="press the button";
// t.setText(s);
f.setSize(300, 300);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String s1 = "press the button";
String s2 = "press ";
if (tag == 0) {
t.setText(s1);
tag = 1;
} else {
t.setText(s2);
tag = 0;
}
}
public void mouseClicked(MouseEvent e) {
String s3 = "button";
t1.setText(s3);
System.out.println("enter");
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
String s2 = "press ";
t1.setText(s2);
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JButtonExample implements ActionListener, MouseListener {
JFrame f;
JButton b;
JTextField t, t1;
int tag = 0;
public static void main(String[] args) {
JButtonExample tb = new JButtonExample();
tb.go();
}
public void go() {
JFrame f = new JFrame("button");
Container content = f.getContentPane();
JButton b = new JButton("sample");
b.addActionListener(this);
content.add(b, "South");
t = new JTextField();
t1 = new JTextField();
content.add(t, "Center");
content.add(t1, "North");
b.addMouseListener(this);
// String s="press the button";
// t.setText(s);
f.setSize(300, 300);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String s1 = "press the button";
String s2 = "press ";
if (tag == 0) {
t.setText(s1);
tag = 1;
} else {
t.setText(s2);
tag = 0;
}
}
public void mouseClicked(MouseEvent e) {
String s3 = "button";
t1.setText(s3);
System.out.println("enter");
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
String s2 = "press ";
t1.setText(s2);
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
追问
你的改的哪里哦,我怎么看不出来有什么不同呢
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询