نمایش نتایج 1 تا 6 از 6

نام تاپیک: تبدیل کلاس به فرم

  1. #1

    Question تبدیل کلاس به فرم

    سلام کسی میتونه این کلاس رو به فرم تبدیل کنه البته همه قابلیت هاش روش بمونه
    یعنی منوها فونت رو بخونه آیکن پشتش درست لود بشه راست چین باشه فوکوس از روی پاپ آپ برداشته میشه پاپ آپ بسته بشه روی منو ها میری رنگی بشه و روشون کلیک میکنی عملیات باز کردن فرم درست انجام بشه و .... یعنی همه چیزایی که توی کلاس کار میکنه توی فرم هم کار کنه منظورم از فرم 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
    }




  2. #2

    نقل قول: تبدیل کلاس به فرم

    من این مدلی درستش کردم ولی رنگ زمینه منو ها وقتی موس میره روش لود نمیشه کسی دلیلش رو میدونه؟






    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:41 صبح

  3. #3

    نقل قول: تبدیل کلاس به فرم

    کسی نیست کمک کنه؟ جای دوری نمیره کمک بدین به این بنده خدا در راه خدا مستحقم

  4. #4

    نقل قول: تبدیل کلاس به فرم

    همه سوادشون مثل من ته کشیده یا خساست میکنید برای یاد دادن؟

  5. #5

    نقل قول: تبدیل کلاس به فرم

    کسی نبود کمک کنه؟
    آخرین ویرایش به وسیله cnmeysam : یک شنبه 15 فروردین 1400 در 04:42 صبح

  6. #6

    نقل قول: تبدیل کلاس به فرم

    بالاخره مشکل رو پیدا کردم توی قیمت GUI جاوا یعنی وقتی فرم ایجاد میکنید باید برای رنگ کردن زمینه هر چیزی اون رو اول setOpaque(true) کنید برای مثال توی منو های من باید این کد ها رو اضافه می کردم

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


    به همین سادگی

برچسب های این تاپیک

قوانین ایجاد تاپیک در تالار

  • شما نمی توانید تاپیک جدید ایجاد کنید
  • شما نمی توانید به تاپیک ها پاسخ دهید
  • شما نمی توانید ضمیمه ارسال کنید
  • شما نمی توانید پاسخ هایتان را ویرایش کنید
  •