Mouserscsiadapter是什么在java中是什么意思

1995人阅读
该例子共有2个类文件和一个图片文件夹
MainFrame类:定义程序界面SetTrayIcon类:定义程序系统托盘功能
使用规则:SetTrayIcon类可以不用修改,直接使用.只需要在程序构造函数中插入两句即可:第一句:new SetTrayIcon(this);&//调用SetTrayIcon(JFrame)类,设置程序系统托盘功能第二句:this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);&//关键.设置窗口关闭处理,与关闭窗体事件搭配
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainFrame extends JFrame
//以下两标签为窗口界面图片
JLabel l_img2;
public MainFrame()
l_img=new JLabel(new ImageIcon(this.getClass().getClassLoader().getResource("images//netbean1.png")));
l_img2=new JLabel(new ImageIcon(this.getClass().getClassLoader().getResource("images//netbean2.png")));
l_img.setBounds(15,15,l_img.getIcon().getIconWidth(),l_img.getIcon().getIconHeight());
l_img2.setBounds(15,15+l_img.getIcon().getIconHeight()+20,l_img2.getIcon().getIconWidth(),l_img2.getIcon().getIconHeight());
add(l_img);
add(l_img2);
this.setTitle("Java托盘测试");
this.setSize(500,350);
this.setResizable(false);
this.setLayout(null);
center(); //调用窗体于屏幕居中函数
new SetTrayIcon(this); //调用SetTrayIcon(JFrame)类,设置程序系统托盘功能
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); //关键.设置窗口关闭处理,与关闭窗体事件搭配
// 设置窗体于屏幕居中函数
private void center()
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
//获得屏幕的尺寸
Dimension jframeSize=this.getSize(); //获得程序尺寸
setLocation((screenSize.width-jframeSize.width)/2,(screenSize.height-jframeSize.height)/2);
public static void main(String[] args)
new MainFrame();
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class
SetTrayIcon implements ActionListener
MenuItem open,
TrayI //任务栏程序托盘图标
public SetTrayIcon(JFrame jframe)
this.jframe=
initComponents(); //初始化程序托盘组件
//设置编译阶段禁用警告提示
@SuppressWarnings("unchecked")
//初始化程序托盘组件
private void initComponents()
pop=new PopupMenu();
open=new MenuItem("打开");
open.addActionListener(this);
close=new MenuItem("退出");
close.addActionListener(this);
pop.add(open);
pop.add(close);
//检查平台是否受支持系统托盘
if(SystemTray.isSupported())
SystemTray tray=SystemTray.getSystemTray();
ImageIcon imageIcon=new ImageIcon("images/user_edit.png");
//获得托盘显示图标
Image icon=imageIcon.getImage(); //获得Image对象
//Image icon=this.getToolkit().getImage(this.getClass().getClassLoader().getResource("images/user_edit.png"));
trayicon=new TrayIcon(icon,"java系统托盘",pop);
//为托盘添加鼠标事件,双击则打开程序界面
trayicon.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
if(e.getClickCount()==2)
openFrame();
tray.add(trayicon);
}catch(AWTException e)
e.printStackTrace();
//为程序添加窗体事件
jframe.addWindowListener(new WindowListener(){
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub
//隐藏程序于系统托盘函数
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
jframe.setVisible(false);
jframe.dispose();
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
int a=JOptionPane.showConfirmDialog(jframe,"按 [是] 退出,[否] 隐藏?","提示",0);
if(a==0){System.exit(0);}
else if(a==1){jframe.setVisible(false);}
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
//窗口最小化
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
jframe.setVisible(false);
//jframe.dispose();
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
//为右击托盘菜单添加事件
public void actionPerformed(ActionEvent e)
if(e.getSource()==open)
openFrame();
if(e.getSource()==close)
System.exit(-1);
//显示程序界面函数
public void openFrame()
jframe.setVisible(true);
jframe.setAlwaysOnTop(true);
版权声明:本文为博主原创文章,未经博主允许不得转载。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:83554次
排名:千里之外
(1)(2)(8)(2)售后服务电话:010-
千如電機工業股份有限公司於 1979 年成立以來,專注電感器本業之研發、生產與全球行銷,秉持「服務 創新 追求卓越」的經營理念,透過「以人為本」的群體創業運作,堅守「有千如就滿足」的核心價值,掌握電感產品的核心技術,並以「創造發展世界級電感工業」為願景,專注電感器本業,孜孜不倦、崇本務實,一切從根源做起,並奉行「Only the paranoid survive」(唯執著者生存)的座右銘,於今,堪稱立足於電感元件業,規模初具,兩岸三地共計四個廠,行銷方面設有美國子公司及全球行銷通路,產品銷售全球,並於今(2004)年十二月初股票上櫃。長期以來,承客戶之愛護與支持、協力廠商的協助、產業界先進的指導與社會各界的愛護,在此表達由衷的感謝。
当前页:1/3, 每页200条记录
全国销售热线:400-660-6568
售后服务电话:010-
请使用分辨率以达到本站最佳访问效果
版权所有 &北京创新在线网络技术有限公司Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.
I have a bunch of JLabels and i would like to trap mouse click events. at the moment i am having to use:
public void mouseClicked(MouseEvent arg0) {
public void mouseExited(MouseEvent arg0) {
public void mouseEntered(MouseEvent arg0) {
public void mousePressed(MouseEvent arg0) {
public void mouseReleased(MouseEvent arg0) {
System.out.println("Welcome to Java Programming!");
I was wondering if there is a tidier way of doing this instead of having a bunch of events I do not wish trap?
class MyAdapter extends MouseAdapter {
public void mouseClicked(MouseEvent event) {
System.out.println(event.getComponent());
the above works but netBeans says add @override anotation. what does this mean?
EDIT: ok got it. fixed and solved.
2,071144893
Use MouseAdapter()
An abstract adapter class for receiving mouse events. The methods in this class are empty. This class exists as convenience for creating listener objects.
So you need to implement only the method you like such as following example:
import java.awt.event.MouseA
import java.awt.event.MouseE
import javax.swing.JF
import javax.swing.JP
public class MainClass extends JPanel {
public MainClass() {
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
System.out.println(me);
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
10.3k22546
One could use a
class, which implements the
interface, so one does not need to implement all the methods.
However, by overriding the methods of interest, one can get the desired behavior. For example, if one overrides the mouseClicked method, then one can define some behavior for the mouse click event.
For example (untested code):
JLabel label = new JLabel("Hello");
label.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.out.println("Clicked!");
In the code above, the JLabel will print "Clicked!" to the console upon being clicked on.
85k15152196
You can extend
instead, and just override the events you're really interested in.
182k119413704
You can inherit from java.awt.event.MouseAdapter and only override the methods for the event(s) you are interested in.
20.7k53861
some example of mouse event clicked,
in the same way you can use mousePressed or other mouse events
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Work1 extends JFrame{
private JPanel panelN
public Work1(){
super("Work 1");
// create Panel
panelNew = new JPanel();
panelNew.setLayout(null);
panelNew.setBackground(Color.cyan );
add(panelNew);
// create Button
JButton btn = new JButton("click me");
// position and size of a button
btn.setBounds(100, 50, 150, 30);
panelNew.add(btn);
// add event to button
btn.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
// change text of button after click
if (btn.getText() == "abraCadabra"){
btn.setText("click me again") ;
btn.setText("abraCadabra");
public static void main(String[] args){
Work1 go1 = new Work1();
go1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go1.setSize(320,200);
go1.setVisible(true);
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Stack Exchange
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabled}

我要回帖

更多关于 baseadapter 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信