PDA

View Full Version : سوال: تبدیل کلاس به فرم



cnmeysam
شنبه 14 فروردین 1400, 03:33 صبح
سلام کسی میتونه این کلاس رو به فرم تبدیل کنه البته همه قابلیت هاش روش بمونه
یعنی منوها فونت رو بخونه آیکن پشتش درست لود بشه راست چین باشه فوکوس از روی پاپ آپ برداشته میشه پاپ آپ بسته بشه روی منو ها میری رنگی بشه و روشون کلیک میکنی عملیات باز کردن فرم درست انجام بشه و .... یعنی همه چیزایی که توی کلاس کار میکنه توی فرم هم کار کنه منظورم از فرم GUI هستش توی نت بینز





import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.imageio.ImageIO;


public class WindowTest {


//---------------------------------------------------------------
public static void main(String[] args) {
JPopupMenu popup = new JPopupMenu();
WindowTest wt = new WindowTest(popup, 60, 20);
wt.loadPopup(popup);
wt.setTI();
}


//---------------------------------------------------------
public WindowTest(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 WindowTest(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
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
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.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) {
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(ComponentOrientati on.RIGHT_TO_LEFT);
popup.setFont(new java.awt.Font("B Nazanin", 1, 18)); // NOI18N


} // end loadPopup
}

cnmeysam
شنبه 14 فروردین 1400, 07:01 صبح
من این مدلی درستش کردم ولی رنگ زمینه منو ها وقتی موس میره روش لود نمیشه کسی دلیلش رو میدونه؟






import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.imageio.ImageIO;




public class trayform extends javax.swing.JFrame {




private static trayform obj = null;




private trayform() {
initComponents();




JPopupMenu popup = new JPopupMenu();
loadPopup(popup);
setTI();
}




public static trayform getobj() {
if (obj == null) {
obj = new trayform();
}
return obj;
}




public trayform(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 trayform(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
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
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.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) {
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(ComponentOrientati on.RIGHT_TO_LEFT);
popup.setFont(new java.awt.Font("B Nazanin", 1, 18)); // NOI18N




} // end loadPopup




@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {




setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
addWindowStateListener(new java.awt.event.WindowStateListener() {
public void windowStateChanged(java.awt.event.WindowEvent evt) {
formWindowStateChanged(evt);
}
});




javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);




setSize(new java.awt.Dimension(416, 339));
setLocationRelativeTo(null);
}// </editor-fold>




private void formWindowStateChanged(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
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");
}
}




public static void main(String args[]) {




try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClass Name());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(trayform.class. getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(trayform.class. getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(trayform.class. getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(trayform.class. getName()).log(java.util.logging.Level.SEVERE, null, ex);
}




java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new trayform().setVisible(true);
}
});
}




// Variables declaration - do not modify
// End of variables declaration
}

cnmeysam
شنبه 14 فروردین 1400, 11:40 صبح
کسی نیست کمک کنه؟ جای دوری نمیره کمک بدین به این بنده خدا در راه خدا مستحقم:گریه::گریه::گریه::گری ::افسرده::افسرده::ناراحت::نا احت::اشتباه:

cnmeysam
شنبه 14 فروردین 1400, 22:20 عصر
همه سوادشون مثل من ته کشیده یا خساست میکنید برای یاد دادن؟:متفکر:

cnmeysam
یک شنبه 15 فروردین 1400, 04:04 صبح
کسی نبود کمک کنه؟

cnmeysam
یک شنبه 15 فروردین 1400, 09:06 صبح
بالاخره مشکل رو پیدا کردم توی قیمت GUI جاوا یعنی وقتی فرم ایجاد میکنید باید برای رنگ کردن زمینه هر چیزی اون رو اول setOpaque(true) کنید برای مثال توی منو های من باید این کد ها رو اضافه می کردم

Exit.setOpaque(true);
Open.setOpaque(true);
Info.setOpaque(true);


به همین سادگی:متعجب: