0
回答
任务栏图标 公共类 javase
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   
求各位帮帮忙!如何简化这三个文件!例如重复的建窗体方法可以就建一个最好了!
在同一包里面三个.java文件(两个是要来做窗体的,一个就是用来做任务栏图标的)

Text.java
public class Text extends JFrame {
	public static void main(String[] args) {
		new Login_ct("r",200,200,true,1);
		
	}
	public Text(String T_tile,int H_eight,int W_idth,boolean L_xing,float T_ming){
		this.setTitle(T_tile);
		this.setSize(H_eight, W_idth);
		this.setUndecorated(L_xing);
		com.sun.awt.AWTUtilities.setWindowOpacity(this,T_ming);
		
		this.setIconImage(new ImageIcon("image/1/ico.png").getImage());
		this.setLocationRelativeTo(null);
		this.setResizable(true);
		this.setVisible(true);
		if(SystemTray.isSupported()){ 
			new All_ct();//错误的1!在这加入任务栏代码!
	    }
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}


 Login_ct.java
public class Login_ct extends JFrame{
	public static void main(String[] args) {
		new Login_ct("登录",200,200,true,1);
		
	}
	public Login_ct(String T_tile,int H_eight,int W_idth,boolean L_xing,float T_ming){
		this.setTitle(T_tile);
		this.setSize(H_eight, W_idth);
		this.setUndecorated(L_xing);
		com.sun.awt.AWTUtilities.setWindowOpacity(this,T_ming);
		
		
		this.setIconImage(new ImageIcon("image/1/ico.png").getImage());
		this.setLocationRelativeTo(null);
		this.setResizable(true);
		this.setVisible(true);
		if(SystemTray.isSupported()){ 
			new All_ct();//这里是错误的!在这加入ren'wu
	    }
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}


正确的任务栏图标代码:
public void tray(){
		PopupMenu pop = new PopupMenu();	
		final MenuItem showandhid;					//母项目
		MenuItem exit;
		Image miniico;
		TrayIcon trayIcon;
		SystemTray tray;
		
		tray = SystemTray.getSystemTray();
		miniico = Toolkit.getDefaultToolkit().getImage("image/1/ico.png");
		
		showandhid = new MenuItem("隐藏主窗口");
		exit = new MenuItem("退出");
		
        trayIcon = new TrayIcon(miniico, "asdfasd", pop);
        trayIcon.setImageAutoSize(true);//自动大小
			try {
				tray.add(trayIcon);
			} catch (AWTException e1) {
				e1.printStackTrace();
			}
			
		trayIcon.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if(e.getClickCount()==2&&isVisible()==false){
                	showandhid.setLabel("隐藏主窗口");
                	setVisible(true);
                }else if(e.getClickCount()==2&&isVisible()==true){
                	showandhid.setLabel("显示主窗口");
            		setVisible(false);
                }
            }
        });
		
		showandhid.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	if(isVisible()==true){
            		showandhid.setLabel("显示主窗口");
            		setVisible(false);
            	}else{
            		showandhid.setLabel("隐藏主窗口");
                	setVisible(true);
            	}
            }
        });
        
        exit.addActionListener(new ActionListener() { 
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
		pop.add(showandhid);
		pop.add(exit);
	}

举报
雪零星
发帖于6年前 0回/194阅
顶部