MouseListener第一次不工作

问题描述:

我是Java新手,我正在创建一个简单的GUI。我在JFrame的Java中有一个标签,当我点击它时,程序应该显示另一个框架并隐藏当前的框架。我也打印它以检查标签(其行为像一个按钮)是否工作。第一次它不是工作。它在第二次点击开始的下一次尝试中起作用,但它不会隐藏当前帧,而是而不是MouseListener第一次不工作

我的代码是:

private void jLabel4MouseClicked(java.awt.event.MouseEvent evt) {          

    MainFrame mf = new MainFrame(); 
    jLabel4.addMouseListener(new MouseAdapter(){ 

     @Override 
     public void mousePressed(MouseEvent e){ 
      System.out.println("It works."); 
      mf.setVisible(true); 

      NewJFrame2 n2 = new NewJFrame2(); 
      n2.setVisible(false); 

     }   
    }); 

有谁知道如何解决它,以便从第一次点击的工作和隐藏当前帧?

+2

上面不可编译的代码片段中显示的逻辑被深深地搞砸了。 “mousePressed”方法中的所有代码语句都应移至“父”侦听器的相同方法。一般提示:1)为了更快地提供更好的帮助,请发布[MCVE]或[简短,独立,正确的示例](http://www.sscce.org/)。 2)参见[使用多个JFrames,好/坏实践?](http://*.com/q/9554636/418556)3)使用['CardLayout'](http://download.oracle.com /javase/8/docs/api/java/awt/CardLayout.html),如[本答案](http://*.com/a/5786005/418556)所示。 4).. –

+3

.. 4)'NewJFrame2'为属性和类使用**描述性的名称,而不是类似机器人的IDE建议。 –

的Java标签不能够接受的ActionListener事件,你应该用一个按钮替换标签。你不要点击你点击按钮的标签,对标签有什么作用可能是一个属性更改监听器。

在这个答案中的按钮有图像,只要记住创建一个文件夹的unser src名称它res然后添加图像您按钮显示。您可以用我的文件名替换图像文件名

//new ImageIcon(getClass().getResource("/res/image-file_name"));** 

package *ProblemSets; 

import sun.applet.Main; 

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 

/** 
* Created by HACKER on 05/06/2017. 
* https://*.com/questions/44370545/mouselistener-doesnt-work- 
    the-first-time-and-there-are-other-errors 
*/ 

class MainFrame extends JFrame { 

    JButton button2 = new JButton("Go to Frame 2", new 
ImageIcon(getClass().getResource("/res/ic_action_maps_blue.png"))); 


public MainFrame() { 
    setSize(500, 500); 
    getContentPane().setBackground(Color.RED); 
    setLayout(new FlowLayout()); 

    add(button2); 

    button2.addMouseListener(new MouseAdapter() { 
     /** 
     * {@inheritDoc} 
     * 
     * @param e 
     */ 
     @Override 
     public void mouseClicked(MouseEvent e) { 
      setVisible(false); 
      new Sample2().setVisible(true); 
     } 
    });}} 



public class Sample2 extends JFrame { 

JButton button4; 

public Sample2() { 

    setSize(500, 600); 
    setLayout(new FlowLayout()); 
    getContentPane().setBackground(Color.YELLOW); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    MainFrame mf = new MainFrame(); 
    button4 = new JButton("Button 4", new 
ImageIcon(getClass().getResource("/res/ic_action_alpha_icon_D.png"))); 
    add(button4); 

    button4.addMouseListener(new MouseAdapter() { 
     /** 
     * {@inheritDoc} 
     * 
     * @param e 
     */ 
     @Override 
     public void mouseClicked(MouseEvent e) { 
      System.out.println("It works."); 
      mf.setVisible(true); 
      setVisible(false); 
     } 
    }); 
} 

public static void main(String[] args) { 
    Sample2 sample2 = new Sample2(); 
    sample2.setVisible(true); 
}} 

使用n2.dispose()代替n2.setVisible(false);

这是给你一个简单的例子,但作为有人说多JFrame在相同的应用程序并不好。而不是尝试一个JFrameJPanel与适当的布局。

import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 

import javax.swing.JFrame; 
import javax.swing.JLabel; 

public class Main { 
    JFrame MainFrame; 
    JFrame ChildFrame; 
    JLabel label; 

    public Main(){ 
     MainFrame = new JFrame("Example"); 
     MainFrame.setSize(300, 300); 

     label = new JLabel("Click me"); 
     labelMousePressed(); 
     MainFrame.add(label); 
     MainFrame.setVisible(true); 
    } 
    private void labelMousePressed() {          
     label.addMouseListener(new MouseAdapter(){ 

      public void mousePressed(MouseEvent e){ 
       System.out.println("It works."); 

       MainFrame.dispose(); 

       ChildFrame = new JFrame("Child"); 
       ChildFrame.setSize(300, 300); 
       ChildFrame.setVisible(true); 
      }   
     }); 
    } 
    public static void main(String[] args) { 
     Main m = new Main(); 
    } 
} 

UPDATE

如果DONOT覆盖JFrame的方法,它不需要extends(继承)JFrame类。而不是从JFrame创建一个对象并使用它。阅读这question to learn more about this

+0

有什么区别? –

+0

@samuelowino:调用'dispose()'释放相关资源 – Blasanka

+1

我手动添加项目,并使用从“private void labelMousePressed()”开始的代码,我似乎遇到了问题。第一次不工作,第二次打印一次,第三次打印两次,等等。 – Danny

而是点击一个JLabel为什么不创建一个JButton其中已经处理了点击与ActionListener,使它看起来像一个JLabel如图所示的多个答案上this question的。

,但它并不能掩盖当前的JFrame

那么,你需要调用你的听众JFrame#dispose()方法,也请看一看The Use of Multiple JFrames: Good or Bad Practice?,最好使用Card Layout或可能采取看看教程How to use Dialogs

+0

我正在使用标签因为我需要有一个图像。 – Danny

+2

一个按钮也可以有一个图像:对于[示例](https://*.com/questions/4801386/how-do-i-add-an-image-to-a-jbutton)或[教程]( https://docs.oracle.com/javase/tutorial/uiswing/components/button.html)示例:)所以,我的答案仍然有效:D – Frakcool

+0

应该被接受回答 – CraigR8806

Java标签不能接收ActionListener事件,你应该用一个按钮替换标签。你不要点击你点击按钮的标签,对标签有什么作用可能是一个属性更改监听器。

运行和分析这些代码就会清楚地指导你...你选择了世界上最好的语言好运,是一个java的家伙2个

class MainFrame extends JFrame { 

JButton button2 = new JButton("Go to Frame 2"); 


public MainFrame() { 
    setSize(500, 500); 
    getContentPane().setBackground(Color.RED); 
    setLayout(new FlowLayout()); 

    add(button2); 

    button2.addMouseListener(new MouseAdapter() { 
     /** 
     * {@inheritDoc} 
     * 
     * @param e 
     */ 
     @Override 
     public void mouseClicked(MouseEvent e) { 
      setVisible(false); 
      new Sample2().setVisible(true); 
     } 
    }); 
}} 


public class Sample2 extends JFrame { 

JButton button4; 

public Sample2() { 

    setSize(500, 600); 
    setLayout(new FlowLayout()); 
    getContentPane().setBackground(Color.YELLOW); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    MainFrame mf = new MainFrame(); 
    button4 = new JButton("Button 4"); 
    add(button4); 

    button4.addMouseListener(new MouseAdapter() { 
     /** 
     * {@inheritDoc} 
     * 
     * @param e 
     */ 
     @Override 
     public void mouseClicked(MouseEvent e) { 
      System.out.println("It works."); 
      mf.setVisible(true); 
      setVisible(false); 
     } 
    }); 
} 
public static void main(String[] args) { 
    Sample2 sample2 = new Sample2(); 
    sample2.setVisible(true); 
}} 
+0

我很早就看到你选择了JLabel,因为你需要一个图像,你也可以用JButton实现这个。看看我的下一个编辑代码 –

+0

这有助于我为了让它工作。大! – Danny

+0

不用客气@Danny,只是一个建议,包括Manning Up Swing In Action,Core Java 2和Java完整参考书籍,以便更深入地了解摆动UI编程。他们为我工作。 –