当前位置: 技术问答>java相关
请各位大虾给个关于JAVA事件的小代码!——————200分言谢——————(在线等)
来源: 互联网 发布时间:2015-09-02
本文导语: 完整例子要求如下: 当鼠标移动到按钮A上的时候,标签A上显示“鼠标上移” 当鼠标按下按钮A的时候,标签A上显示“鼠标按下” 当鼠标右击按钮A的时候,标签A上显示“鼠标右击” 小弟初学JAVA对事件中的“监听...
完整例子要求如下:
当鼠标移动到按钮A上的时候,标签A上显示“鼠标上移”
当鼠标按下按钮A的时候,标签A上显示“鼠标按下”
当鼠标右击按钮A的时候,标签A上显示“鼠标右击”
小弟初学JAVA对事件中的“监听者”、“事件源”、“注册”、“接口”...等概念弄得头大如斗,请各位给个小例子,谢谢!200分言谢决不食言!
当鼠标移动到按钮A上的时候,标签A上显示“鼠标上移”
当鼠标按下按钮A的时候,标签A上显示“鼠标按下”
当鼠标右击按钮A的时候,标签A上显示“鼠标右击”
小弟初学JAVA对事件中的“监听者”、“事件源”、“注册”、“接口”...等概念弄得头大如斗,请各位给个小例子,谢谢!200分言谢决不食言!
|
你要的是下边的意思吗?
package helloworld;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Frame2 extends JFrame
{
private JPanel jPanel1 = new JPanel();
private JButton jButton1 = new JButton();
public Frame2()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
Frame2 frame2 = new Frame2();
frame2.setSize(400,300);
frame2.show();
}
private void jbInit() throws Exception
{
jButton1.setText("jButton1");
jButton1.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mouseEntered(MouseEvent e)
{
jButton1_mouseEntered(e);
}
public void mousePressed(MouseEvent e)
{
jButton1_mousePressed(e);
}
public void mouseClicked(MouseEvent e)
{
jButton1_mouseClicked(e);
}
});
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jButton1, null);
}
void jButton1_mouseEntered(MouseEvent e)
{
jButton1.setText("mouse move up");
}
void jButton1_mousePressed(MouseEvent e)
{
jButton1.setText("mouse press down");
}
void jButton1_mouseClicked(MouseEvent e)
{
if(e.getModifiers() ==InputEvent.BUTTON3_MASK)
jButton1.setText("right mouse button down");
}
}
package helloworld;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Frame2 extends JFrame
{
private JPanel jPanel1 = new JPanel();
private JButton jButton1 = new JButton();
public Frame2()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
Frame2 frame2 = new Frame2();
frame2.setSize(400,300);
frame2.show();
}
private void jbInit() throws Exception
{
jButton1.setText("jButton1");
jButton1.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mouseEntered(MouseEvent e)
{
jButton1_mouseEntered(e);
}
public void mousePressed(MouseEvent e)
{
jButton1_mousePressed(e);
}
public void mouseClicked(MouseEvent e)
{
jButton1_mouseClicked(e);
}
});
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jButton1, null);
}
void jButton1_mouseEntered(MouseEvent e)
{
jButton1.setText("mouse move up");
}
void jButton1_mousePressed(MouseEvent e)
{
jButton1.setText("mouse press down");
}
void jButton1_mouseClicked(MouseEvent e)
{
if(e.getModifiers() ==InputEvent.BUTTON3_MASK)
jButton1.setText("right mouse button down");
}
}
|
import java.awt.*;
import java.awt.event.*;
public class ActionTest extends WindowAdapter implements ActionListener
{
Frame mainFrame;
Label label;
Button button;
public ActionTest()
{
mainFrame=new Frame("事件测试");
label=new Label("效果显示");
button=new Button("A");
mainFrame.setLayout(null);
label.setBounds(20,30,200,23);
button.setBounds(20,63,70,23);
mainFrame.add(label);
mainFrame.add(button);
button.addMouseListener(new MouseListener()
{
public void mousePressed(MouseEvent me)
{
label.setText("鼠标按下");
}
public void mouseReleased(MouseEvent me)
{
}
public void mouseClicked(MouseEvent me)
{
label.setText("鼠标单击");
}
public void mouseExited(MouseEvent me)
{
label.setText("鼠标离开");
}
public void mouseEntered(MouseEvent me)
{
label.setText("鼠标进入");
}
}
);
mainFrame.addWindowListener(this);
mainFrame.setSize(250,100);
mainFrame.setBackground(new Color(212,208,200));
mainFrame.setVisible(true);
mainFrame.setResizable(false);
}
public static void main(String[]args)
{
ActionTest actiontest=new ActionTest();
}
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
public void actionPerformed(ActionEvent ae)
{
}
}
import java.awt.event.*;
public class ActionTest extends WindowAdapter implements ActionListener
{
Frame mainFrame;
Label label;
Button button;
public ActionTest()
{
mainFrame=new Frame("事件测试");
label=new Label("效果显示");
button=new Button("A");
mainFrame.setLayout(null);
label.setBounds(20,30,200,23);
button.setBounds(20,63,70,23);
mainFrame.add(label);
mainFrame.add(button);
button.addMouseListener(new MouseListener()
{
public void mousePressed(MouseEvent me)
{
label.setText("鼠标按下");
}
public void mouseReleased(MouseEvent me)
{
}
public void mouseClicked(MouseEvent me)
{
label.setText("鼠标单击");
}
public void mouseExited(MouseEvent me)
{
label.setText("鼠标离开");
}
public void mouseEntered(MouseEvent me)
{
label.setText("鼠标进入");
}
}
);
mainFrame.addWindowListener(this);
mainFrame.setSize(250,100);
mainFrame.setBackground(new Color(212,208,200));
mainFrame.setVisible(true);
mainFrame.setResizable(false);
}
public static void main(String[]args)
{
ActionTest actiontest=new ActionTest();
}
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
public void actionPerformed(ActionEvent ae)
{
}
}