简体   繁体   English

将鼠标放在JLabel上时如何调用JFrame,以及从JLabel删除鼠标时如何关闭同一JFrame?

[英]how to call JFrame when I put the mouse on JLabel And how to close the Same JFrame When i remove the mouse from JLabel?

In my program I try to show the JFrame when I put the mouse on JLabel and close the JFrame when I remove the mouse from JLabel . 在我的程序中,当我将鼠标放在JLabel上时,我尝试显示JFrame当我从JLabel删除鼠标时,我尝试关闭JFrame

How can I do it? 我该怎么做?

I tried below way, but I am getting flashing windows continuously(popup and close continuously) 我尝试下面的方法,但是我不断地刷新窗口(弹出并不断关闭)

public class NewJFrame extends javax.swing.JFrame {

  NewJFrame1 frame = new NewJFrame1();
  public NewJFrame() {
    initComponents();

}
  private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    jTextField1 = new javax.swing.JTextField();
    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    //======================================================================
    jLabel1.addMouseListener(new MouseAdapter()
        {

            public void mouseEntered(MouseEvent e)
            {
                frame.setVisible(true);
            }
        });
        jLabel1.addMouseListener(new MouseAdapter()
            {
                public void mouseExited(MouseEvent e)
                {
                    frame.setVisible(false); //Hide window
                }
            });
            jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            jLabel1.setText("Testing ");

   //======================================================================
   pack();
        }
public static void main(String args[]) {
  java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new NewJFrame().setVisible(true);
        }
    });

}
public javax.swing.JLabel jLabel1;
}

In between these lines //============ I have the main code 在这些行之间//============我有主要代码

How to display JFrame when I put mouse on JLabel and how to close the JFrame when I remove the mouse from the JLabel ? 将鼠标放在JLabel上时如何显示JFrame ,以及从JLabel删除鼠标时如何关闭JFrame

When I remove the below code , and when I place the mouse on JLabel I am getting JFrame popup , but I need to close the JFrame popup when I remove the mouse from JLabel . 当我删除下面的代码时,以及将鼠标放在JLabel时,我都会看到JFrame弹出窗口,但是当我从JLabel删除鼠标时,我需要关闭JFrame弹出窗口。

jLabel1.addMouseListener(new MouseAdapter()
    {
        public void mouseExited(MouseEvent e)
        {
            frame.setVisible(false); //Hide window
        }
    });

You need to add the JLabel (as well as the other components) to the JFrame. 您需要将JLabel(以及其他组件)添加到JFrame。 Once you do that, and the JLabel shows in the JFrame, you can use the JLabel's listeners. 完成此操作后,JLabel将显示在JFrame中,您可以使用JLabel的侦听器。


EDITED: 编辑:
A. Here is you code with some minimal changes to show / hide a 2nd JFrame: A.这是您的代码,仅作了一些最小的更改以显示/隐藏第二个JFrame:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class NewJFrame extends javax.swing.JFrame {

    javax.swing.JFrame frame ;

    //this will not compile and not needed
    //NewJFrame1 frame = new NewJFrame1();

    public NewJFrame() {
        initComponents();
    }

    private void initComponents() {

        frame = getAJFrame();

        //set a layout manger
        getContentPane().setLayout(new GridLayout(3, 1));
        setLocationRelativeTo(null);
        jLabel1 = new javax.swing.JLabel();
        //add component
        getContentPane().add(jLabel1);

        JTextField jTextField1 = new javax.swing.JTextField();
        //add component
        getContentPane().add(jTextField1);

        JButton jButton1 = new javax.swing.JButton();
        //add component
        getContentPane().add(jButton1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.addMouseListener(new MouseAdapter()
        {

            @Override
            public void mouseEntered(MouseEvent e)
            {
                frame.setVisible(true);
                frame.pack();
            }
        });

        jLabel1.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mouseExited(MouseEvent e)
            {
                frame.setVisible(false); //Hide window
            }
        });
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Testing ");

        //set a size to the frame
        setPreferredSize(new Dimension(200,100));
        pack();
    }

    /**
     *@return
     */
    private JFrame getAJFrame() {

        JFrame f = new JFrame("A JFrame");
        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        f.getContentPane().setPreferredSize(new Dimension(150,150));
        f.getContentPane().setBackground(Color.BLUE);
        setVisible(false);
        pack();
        return f;
    }

    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                NewJFrame frame = new NewJFrame();
                frame.setVisible(true);
            }
        });
    }
    public javax.swing.JLabel jLabel1;
}

B. If you are trying to show / hide NewJFrame itself : you will not be able to make the JFrame visible again, using mouseEntered. B. 如果您试图显示/隐藏NewJFrame本身 :您将无法使用mouseEntered使JFrame再次可见。 When the JFrame (and JLabel within it) are setVisible(false) it will not generate mouse events. 将JFrame(及其中的JLabel)设置为setVisible(false)时,将不会生成鼠标事件。 The frame becomes invisible when mouse exits the JLabel. 当鼠标退出JLabel时,该框架变为不可见。 You will need to make it visible again using a different technique. 您将需要使用其他技术再次使其可见。

C. See The Use of Multiple JFrames: Good or Bad Practice? C.请参阅使用多个JFrame:良好或不良做法?

  1. Your code is completly wrong, cannot read and understand cleary what do you try. 您的代码完全错误,无法阅读并清楚地了解您要尝试什么。
  2. The problem is you create the new JFrame over your label, so the focus of your mouse changes to the new JFrame and the listener of JLabel say that the new window to dissapear and again and again. 问题是您在标签上创建了新的JFrame,因此鼠标的焦点更改为新的JFrame,并且JLabel的侦听器会说新窗口一次又一次消失。 To fix this, set the new position out the current window. 要解决此问题,请在当前窗口外设置新位置。
  3. Here is the code of the main method: 这是main方法的代码:

     public static void main(String[] args) { EventQueue.invokeLater(() -> { Main ex = new Main(); ex.setVisible(true); }); } 

    Then add components to panel with: 然后使用以下命令将组件添加到面板中:

     JPanel panel = new JPanel(); this.add(panel); JLabel jLabel1 = new JLabel("Label"); JTextField jTextField1 = new JTextField("Field"); JButton jButton1 = new JButton("Button"); panel.add(jLabel1); panel.add(jTextField1); panel.add(jButton1); 

    Then create the new splashing JFrame and set the current one as well. 然后创建新的启动JFrame并设置当前的JFrame。 Since your main class extends JFrame, you can use the keyword this . 由于您的主类扩展了JFrame,因此可以使用关键字this

     JFrame frame = new JFrame(); frame.setSize(300, 200); frame.setVisible(false); this.setTitle("Title"); this.setSize(300, 200); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(EXIT_ON_CLOSE); 

And finally let the listeners do the job from your code. 最后,让听众从您的代码中完成这项工作。

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

相关问题 如何将JLabel从JFrame中的一个JPanel拖动到同一JFrame中另一个JPanel的JTextField中? - How should I drag a JLabel from one JPanel in a JFrame onto a JTextField in another JPanel in the same JFrame? 如何使用 JLabel 获取 JFrame? 尝试时出错 - How to get the JFrame with the JLabel? Error when attempted 如何在另一个类的JFrame中添加JLabel? - How I add a JLabel to a JFrame on another class? 通过mosemotionliistner控制的JLabel,在另一个jlabel(不受鼠标控制)进入并离开JFrame时,返回到原始位置 - JLabel,controlled through mosemotionliistner, returned back to original location when another jlabel(not controlled by mouse) enters and leaves JFrame 如何结合JLabel和JFrame? - How to combine a JLabel and JFrame? 当鼠标位于 JLabel 下方时出现黄色窗口 -> 如何移除? - Yellow Window under JLabel when the mouse is above it -> how to remove? 如何通过单击鼠标来聚焦 JLabel? - How can I focus a JLabel with a mouse click? 何时使用JFrame,JLabel和JPanel - When to use JFrame, JLabel, and JPanel 将4 JLabel放在JFrame的角落 - Put 4 JLabel at corners of a JFrame 我试图将JLabel放在JButton上,但是当我将鼠标指针放在标签上时,它消失了 - I tried to put a JLabel on a JButton but when I put my mouse pointer on the label it disappears
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM