
برای استفاده از سیستم ترای در جاوا با پشتیبانی از فونت فارسی راست چین و آیکن پشت منو ها میتوانید از روش زیر استفاده کنید اول یه پروژه ایجاد کنید بعد یه form جدید بسازید و دستورات زیر را در بدنه public NewJFrame() فراخوانی کنید توابع زیر در فرم، یک آیکن کنار ساعت ویندوز اضافه میکنه که با کلیک روش 3 منو نمایش و درباره ما و خروج رو مشاهده می کنید.
|
1 2 3 |
JPopupMenu popup = new JPopupMenu(); loadPopup(popup); setTI(); |
اول کتاب خانه های مورد نیاز رو فراخوانی کنید:
|
1 2 3 4 |
import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.imageio.ImageIO; |
سپس دستورات زیر رو توی بدنه سورس فرم اضافه کنید (توجه کنید که مسیر و نام عکس های خودتون رو جایگزین کنید تا برنامه کار کنه):
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
//برای ترای آیکن و منوهای ساپورت فارسی public NewJFrame(JPopupMenu popup, int x, int y) { // Window win = new Window(new Frame()); //null); JDialog win = new JDialog(new Frame()); //null); win.setUndecorated(true); JMenuBar mnBar = new JMenuBar(); //Works final JMenu men = new JMenu("Test"); //<< Shows Test and a Right caret JMenuItem mi1 = new JMenuItem("one"); // men.add(mi1); mnBar.add(men); win.setLayout(new BorderLayout()); // win.add(mnBar, BorderLayout.NORTH); win.setBounds(x, y, 100, 100); win.setOpacity(0.05f); // win.setBackground( win.setVisible(true); win.setFocusable(true); // loadPopup(popup); win.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { System.out.println("mC at " + e.getPoint()); // WORKS // Place and show popup popup.setLocation(e.getPoint()); popup.setVisible(true); if (e.getPoint().y > 90) { System.exit(0); } } }); mnBar.addFocusListener(new FocusAdapter() { @Override public void focusLost(FocusEvent evt) { System.out.println("focusLost"); //<<<< never popup.setVisible(false); } @Override public void focusGained(FocusEvent evt) { System.out.println("focusGained"); // ditto } }); win.addWindowFocusListener(new WindowFocusListener() { @Override public void windowLostFocus(WindowEvent evt) { System.out.println("WfocusLost"); //<<<< THIS ONE popup.setVisible(false); } @Override public void windowGainedFocus(WindowEvent evt) { System.out.println("WfocusGained"); // ditto } }); win.requestFocus(); // nothing men.requestFocusInWindow(); // nothing System.out.println("isFocusable()=" + win.isFocusable()); // isFocusable()=true } //---------------------------------------------- TrayIcon ti; public void setTI() { try { ti = new TrayIcon(ImageIO.read(getClass().getResource("/images/kabe.jpg")), "Have a nice day"); ti.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { System.out.println("mC for ti at " + e.getPoint()); // What x,y for the popup???? int x = e.getPoint().x; int y = e.getPoint().y; // Create our own local popup to respond to TrayIcon click JPopupMenu popup = new JPopupMenu(); new NewJFrame(popup, x - 20, y - 20); loadPopup(popup); popup.setLocation(x - 12, y - 95); popup.setVisible(true); } }); SystemTray.getSystemTray().add(ti); //<<<<<<< } catch (Exception ex) { ex.printStackTrace(); } } //-------------------------------------------------------------------------- public void loadPopup(JPopupMenu popup) { JMenuItem Exit = new JMenuItem("خروج"); Exit.setFont(new java.awt.Font("B Nazanin", 1, 18)); // NOI18N Exit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/exit.png"))); //exit.png"))); // NOI18N Exit.setOpaque(true); JMenuItem Open = new JMenuItem("نمایش"); Open.setFont(new java.awt.Font("B Nazanin", 1, 18)); // NOI18N Open.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/kabe.jpg"))); //kabe.jpg"))); // NOI18N Open.setOpaque(true); JMenuItem Info = new JMenuItem("درباره ما"); Info.setFont(new java.awt.Font("B Nazanin", 1, 18)); // NOI18N Info.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/info.png"))); //info.png"))); // NOI18N Info.setOpaque(true); Info.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseEntered(java.awt.event.MouseEvent evt) { Info.setBackground(Color.GREEN); } public void mouseExited(java.awt.event.MouseEvent evt) { Info.setBackground(UIManager.getColor("control")); } }); Exit.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseEntered(java.awt.event.MouseEvent evt) { Exit.setBackground(Color.RED); } public void mouseExited(java.awt.event.MouseEvent evt) { Exit.setBackground(UIManager.getColor("control")); } }); Open.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseEntered(java.awt.event.MouseEvent evt) { Open.setBackground(Color.GREEN); } public void mouseExited(java.awt.event.MouseEvent evt) { Open.setBackground(UIManager.getColor("control")); } }); Exit.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); Open.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // JFrame frame = new JFrame(); // frame. setVisible(true); popup.setVisible(false); // frame. setExtendedState(JFrame.NORMAL); } }); Info.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Info info = new Info(); info.setVisible(true); ti.displayMessage("Open form", "Frame is opened", TrayIcon.MessageType.NONE); } }); popup.add(Open); popup.addSeparator(); popup.add(Info); popup.addSeparator(); popup.add(Exit); // popup.setComponentOrientation(ComponentOrientation .RIGHT_TO_LEFT); popup.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); popup.setFont(new java.awt.Font("B Nazanin", 1, 18)); // NOI18N } // end loadPopup //برای ترای آیکن و منوهای ساپورت فارسی |
و در آخر برای مخفی کردن فرم وقتی مینیمایز میشه در قسمت WindowStateChange فرم کد های زیر رو مینویسیم
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// برای مخفی شدن فرم بعد از مینی مایز کردن if (evt.getNewState() == ICONIFIED) { //System.out.println("unable to add to tray"); setVisible(false); } if (evt.getNewState() == 7) { //System.out.println("unable to add to system tray"); setVisible(false); } if (evt.getNewState() == MAXIMIZED_BOTH) { setVisible(true); //System.out.println("Tray icon removed"); } if (evt.getNewState() == NORMAL) { setVisible(true); //System.out.println("Tray icon removed"); } // برای مخفی شدن فرم بعد از مینی مایز کردن |
و برای جلو گیری از چند بار باز شدن فرمتون اول از همه (فرض بر اینه که فرمی که ساختین اسمش رو تغییر ندادین ) این قسمت از سروس برنامه رو :
|
1 2 3 |
public NewJFrame() { initComponents(); } |
به این شکل تغییر میدین:
|
1 2 3 |
private NewJFrame() { initComponents(); } |
به عبارتی public رو به private تغییر میدید و بعد از این قسمت کد های زیر رو اضافه میکنید
|
1 2 3 4 5 6 7 8 9 10 |
// برای یک بارباز شدن فرم private static NewJFrame() obj = null; public static NewJFrame getobj() { if (obj == null) { obj = new NewJFrame(); } return obj; } // برای یک بارباز شدن فرم |




