简体   繁体   中英

how to minimize and maximize JFrame in java using mouse and key Listener?

I want to minimize the frame while I clicked on the frame using "mouseClicked" and maximize while I pressed 'n' char using "keyPressed" this is code is running but I think not doing any thing.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JPanel implements MouseListener,KeyListener {
   static JFrame frame = new JFrame("java lover");

    public Test() {

           super();
           this.addMouseListener(this);
           this.addKeyListener(this);
    }

//************************************************************************************
  public void mouseClicked(MouseEvent e){
     frame.setState(Frame.ICONIFIED);  // to minimize frame

    }
 //************************************************************************************
    public void mouseEntered(MouseEvent e){
    }
    public void mouseExited(MouseEvent e){
    }
    public void mousePressed(MouseEvent e){
    }
    public void mouseReleased(MouseEvent e){
    }
 //*************key*********************************
  public void keyTyped(KeyEvent e) {
     }
  //*************key*********************************************************************
     public void keyPressed(KeyEvent e) {
         if(e.getKeyChar()=='n'){
              frame.setState ( Frame.NORMAL );// for maximize or normal frame
             }
    }
 //*************key**********************************************************************
     public void keyReleased(KeyEvent e) {
    }
//**************************************************

   public static void main(String[] args) {
          Test panel=new Test();
               frame.setSize(600, 600);
               frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   }
}

thanks for helping.

"I want to minimize the frame while I clicked on the frame using "mouseClicked"

You never add the Test panel to the frame. The panel has the mouse listener

Test panel=new Test();
frame.add(panel);

"and maximize while I pressed 'n' char using "keyPressed"

I don't think that is possible. Once the frame is minimized, the application is no longer focused, and key event will transfer to the focused application or to the system. System keys like Windows->Tab can navigate you back to your application (in Windows) if you need to .

But maybe the better question is "why would you want to do that" ? Imagine you minimize your application, then start working on another application that requires typing, and you type 'n'. Your other application will automatically open which would be super annoying when trying to post answers an question on SO. I'd have to limit my answers to not using the letter n :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM