简体   繁体   English

如何使用鼠标和按键监听器在Java中最小化和最大化JFrame?

[英]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. 我想在使用“ mouseClicked”单击框架时最小化框架,并在使用“ keyPressed”按“ n”字符时最大化框架,这是代码正在运行,但是我认为没有做任何事情。

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" “当我使用“ mouseClicked”点击框架时,我想最小化框架

You never add the Test panel to the frame. 您永远不会将“ Test panel添加到框架。 The panel has the mouse listener 面板上有鼠标监听器

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

"and maximize while I pressed 'n' char using "keyPressed" “并在我使用“ keyPressed”按下'n'char时最大化

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 . 如果需要,可以使用Windows-> Tab之类的系统键将您导航回您的应用程序(在Windows中)。

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'. 想象一下,您最小化了您的应用程序,然后开始在需要输入的其他应用程序上工作,然后输入“ n”。 Your other application will automatically open which would be super annoying when trying to post answers an question on SO. 您的其他应用程序将自动打开,这在尝试发布关于SO的问题时会非常烦人。 I'd have to limit my answers to not using the letter n :-) 我必须将答案限制为不使用字母n :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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