[英]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:良好或不良做法?
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.