addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent evt){
} });
里面共五个方法,用那个就加那个,不用就不加。除了写出的那个还有mouseClicked,mouseRelease,mouseEntered,mouseExited
{
public void mousePressed(MouseEvent evt){
} });
里面共五个方法,用那个就加那个,不用就不加。除了写出的那个还有mouseClicked,mouseRelease,mouseEntered,mouseExited
button = new JButton("hehe");
button.addActionListener(this);
button.removeMouseListener(this);这会有问题的么?应该不会阿。
method a(){
addmouselistener();
}
method b(){
removemouselistener(this);
}
}
这样也不对呀!
我都不知道为什么?
method a(){
addmouselistener();
}
method b(){
removemouselistener(this);
}
}
你这个HEHE里的addmouselistener()是怎么写的??
import java.awt.*;
import java.awt.event.*;
import java.applet.*;public class IsTriangle extends Applet implements MouseMotionListener
{
Label l1=new Label("第一边长度:"),
l2=new Label("第二边长度:"),
l3=new Label("第三边长度:"),
l4=new Label(" 形状: ");
TextField tf1=new TextField("请输入第一边的长度",20),
tf2=new TextField("请输入第二边的长度",20),
tf3=new TextField("请输入第三边的长度",20),
tf4=new TextField("结果",20);
Button bok=new Button("确定"); int line1=0,line2=0,line3=0;
public void init()
{
setLayout(new FlowLayout());
add(l1);
add(tf1);
add(l2);
add(tf2);
add(l3);
add(tf3);
add(l4);
add(tf4);
add(bok);
tf1.addMouseMotionListener(this);
tf2.addMouseMotionListener(this);
tf3.addMouseMotionListener(this);
tf1.addKeyListener(new keyListener());
tf2.addKeyListener(new keyListener());
tf3.addKeyListener(new keyListener());
tf4.setEditable(false);
bok.addActionListener(new bokListener());
} class keyListener extends KeyAdapter
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyChar()!='0'&&e.getKeyChar()!='1'&&
e.getKeyChar()!='2'&&e.getKeyChar()!='3'&&
e.getKeyChar()!='4'&&e.getKeyChar()!='5'&&
e.getKeyChar()!='6'&&e.getKeyChar()!='7'&&
e.getKeyChar()!='8'&&e.getKeyChar()!='9')
javax.swing.JOptionPane.showMessageDialog(null,"对不起"+e.getKeyChar()+"是非法字符\n"+"您只能输入一个正整数!");
}
} public void mouseMoved(MouseEvent e)
{
((TextField)e.getSource()).requestFocus();
((TextField)e.getSource()).selectAll();
} public void mouseDragged(MouseEvent e)
{
} class bokListener extends Frame implements ActionListener
{
int a=0,b=0,c=0;
String Msg="";
public void actionPerformed(ActionEvent e)
{
line1=Integer.parseInt(tf1.getText());
line2=Integer.parseInt(tf2.getText());
line3=Integer.parseInt(tf3.getText());
Msg=compare(line1,line2,line3);
tf4.setText(Msg);
}
public String compare(int x,int y,int z)
{
a=Math.max(x,Math.max(y,z));
c=Math.min(x,Math.min(y,z));
b=x+y+z-a-c;
if((b+c>a)&&(a-c<b))
if(a==c) return "等边三角形"; //等边三角形
else {
if(b*b+c*c-a*a>0)
{
if(a==b) return "等腰锐角三角形"; //等腰锐角三角形
if(b==c) return "等腰锐角三角形"; //等腰锐角三角形
else return "一般锐角三角形"; //一般锐角三角形
}
if(b*b+c*c-a*a==0)
if(c==b) return "等腰直角三角形"; //等腰直角三角形
else return "一般直角三角形"; //一般直角三角形
if(b*b+c*c-a*a<0)
if(c==b) return "等腰钝角三角形"; //等腰钝角三角形
else return "一般钝角三角形"; //一般钝角三角形
}
return "抱歉,不能构成三角形";
}
}
}
IsTriangle.html文件:
<APPLET CODE="IsTriangle.class" width=300 height=400>
</APPLET>
import java.awt.event.*;
import java.applet.*;
public class IsTriangle extends Applet implements MouseMotionListener
{
private MyListener myMouseListener1;
private MyListener myMouseListener2;
private MyListener myMouseListener3; Label l1=new Label("第一边长度:"),
l2=new Label("第二边长度:"),
l3=new Label("第三边长度:"),
l4=new Label(" 形状: "); TextField tf1=new TextField("请输入第一边的长度",20),
tf2=new TextField("请输入第二边的长度",20),
tf3=new TextField("请输入第三边的长度",20),
tf4=new TextField("结果",20);
Button bok=new Button("确定");
Button removeButton=new Button("取消"); int line1=0,line2=0,line3=0; public void init()
{
setLayout(new FlowLayout());
add(l1);
add(tf1);
add(l2);
add(tf2);
add(l3);
add(tf3);
add(l4);
add(tf4);
add(bok);
add(removeButton);
myMouseListener1=new MyListener(tf1);
myMouseListener2=new MyListener(tf2);
myMouseListener3=new MyListener(tf3);
tf1.addMouseMotionListener(myMouseListener1);
tf1.addMouseListener(myMouseListener1);
tf2.addMouseMotionListener(myMouseListener2);
tf3.addMouseMotionListener(myMouseListener3);
tf1.addKeyListener(new keyListener());
tf2.addKeyListener(new keyListener());
tf3.addKeyListener(new keyListener());
tf4.setEditable(false);
bok.addActionListener(new bokListener()); }
public void start(){
removeButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tf1.removeMouseMotionListener(myMouseListener1); //去除mouseclicked事件监听
tf1.removeMouseListener(myMouseListener1);
tf2.removeMouseMotionListener(myMouseListener2);
tf3.removeMouseMotionListener(myMouseListener3);
}
});
}
class keyListener extends KeyAdapter
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyChar()!='0'&&e.getKeyChar()!='1'&&
e.getKeyChar()!='2'&&e.getKeyChar()!='3'&&
e.getKeyChar()!='4'&&e.getKeyChar()!='5'&&
e.getKeyChar()!='6'&&e.getKeyChar()!='7'&&
e.getKeyChar()!='8'&&e.getKeyChar()!='9')
javax.swing.JOptionPane.showMessageDialog(null,"对不起"+e.getKeyChar()+"是非法字符\n"+"您只能输入一个正整数!");
}
} public void mouseMoved(MouseEvent e)
{
((TextField)e.getSource()).requestFocus();
((TextField)e.getSource()).selectAll();
} public void mouseDragged(MouseEvent e)
{
} class bokListener extends Frame implements ActionListener
{
int a=0,b=0,c=0;
String Msg=""; public void actionPerformed(ActionEvent e)
{
tf1.addMouseMotionListener(myMouseListener1);
tf1.addMouseListener(myMouseListener1); //增加mouseclicked事件监听
tf2.addMouseMotionListener(myMouseListener2);
tf3.addMouseMotionListener(myMouseListener3);
line1=Integer.parseInt(tf1.getText());
line2=Integer.parseInt(tf2.getText());
line3=Integer.parseInt(tf3.getText());
Msg=compare(line1,line2,line3);
tf4.setText(Msg);
} public String compare(int x,int y,int z)
{
a=Math.max(x,Math.max(y,z));
c=Math.min(x,Math.min(y,z));
b=x+y+z-a-c;
if((b+c>a)&&(a-c<b))
if(a==c) return "等边三角形"; //等边三角形
else { if(b*b+c*c-a*a>0)
{
if(a==b) return "等腰锐角三角形"; //等腰锐角三角形
if(b==c) return "等腰锐角三角形"; //等腰锐角三角形
else return "一般锐角三角形"; //一般锐角三角形
}
if(b*b+c*c-a*a==0)
if(c==b) return "等腰直角三角形"; //等腰直角三角形
else return "一般直角三角形"; //一般直角三角形
if(b*b+c*c-a*a<0)
if(c==b) return "等腰钝角三角形"; //等腰钝角三角形
else return "一般钝角三角形"; //一般钝角三角形
}
return "抱歉,不能构成三角形";
}
}
} class MyListener implements MouseMotionListener,MouseListener {
private Object owner;
public MyListener(Object owner) {
this.owner=owner;
} public void mouseDragged(MouseEvent e) { }
public void mouseMoved(MouseEvent e){
((TextField)owner).requestFocus();
((TextField)owner).selectAll();
}
public void mouseClicked(MouseEvent e) {
javax.swing.JOptionPane.showMessageDialog(null,"Mouse Clicked");
}
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
}