Java MouseListener第一次不工作了

Java MouseListener第一次不工作了,java,swing,mouselistener,Java,Swing,Mouselistener,我是Java新手,我正在创建一个简单的GUI。我在JFrame中有一个Java标签,当我单击它时,程序应该显示另一个帧并隐藏当前帧。我还把它打印出来,以检查像按钮一样的标签是否有效。第一次它根本不起作用。它在从第二次单击开始的下一次尝试中起作用,但不会隐藏当前帧 我的代码是: private void jLabel4MouseClicked(java.awt.event.MouseEvent evt) { Mai

我是Java新手,我正在创建一个简单的GUI。我在JFrame中有一个Java标签,当我单击它时,程序应该显示另一个帧并隐藏当前帧。我还把它打印出来,以检查像按钮一样的标签是否有效。第一次它根本不起作用。它在从第二次单击开始的下一次尝试中起作用,但不会隐藏当前帧

我的代码是:

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);

        }          
    });
是否有人知道如何修复它,以便从第一次单击开始工作并隐藏当前帧?

使用n2.dispose代替n2.setVisiblefalse

这是一个简单的示例,但正如其他人所说,在同一个应用程序中使用多个JFrame并不好。而不是尝试一个具有适当布局的JFrame和JPanel

更新

如果不重写JFrame中的方法,则无需扩展继承JFrame类。而不是从JFrame创建一个对象并使用它。阅读此文。

使用n2.dispose代替n2.setVisiblefalse

这是一个简单的示例,但正如其他人所说,在同一个应用程序中使用多个JFrame并不好。而不是尝试一个具有适当布局的JFrame和JPanel

更新

如果不重写JFrame中的方法,则无需扩展继承JFrame类。而不是从JFrame创建一个对象并使用它。阅读此文。

与其单击JLabel,不如创建一个JButton,该按钮已使用ActionListener处理单击,并使其看起来像JLabel,如上的多个答案所示

但它不会隐藏当前JFrame

好的,您需要在您的侦听器上调用方法,但也请看一看,最好使用或可能看一看关于

的教程,而不是单击JLabel,为什么不创建一个JButton,它已经使用ActionListener处理单击,并使其看起来像一个JLabel,如上的多个答案所示

但它不会隐藏当前JFrame


好的,你需要在你的监听器上调用方法,但是也请看一下,最好使用一个或者看一下关于Java标签不能接收ActionListener事件的教程,你应该用一个按钮替换标签。如果不单击标签,则单击按钮,可能对标签起作用的是属性更改侦听器

运行并分析此代码,它将清楚地指导您。。。祝你好运,你选择了世界上最好的语言,我是一个java的家伙


Java标签无法接收ActionListener事件,应使用按钮替换标签。如果不单击标签,则单击按钮,可能对标签起作用的是属性更改侦听器

运行并分析此代码,它将清楚地指导您。。。祝你好运,你选择了世界上最好的语言,我是一个java的家伙


Java标签无法接收ActionListener事件,应使用按钮替换标签。如果不单击标签,则单击按钮,可能对标签起作用的是属性更改侦听器

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

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

package StackOverflowProblemSets;

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://stackoverflow.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);
}}

Java标签无法接收ActionListener事件,应使用按钮替换标签。如果不单击标签,则单击按钮,可能对标签起作用的是属性更改侦听器

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

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

package StackOverflowProblemSets;

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://stackoverflow.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);
}}

上面不可编译的代码片段中显示的逻辑被彻底弄乱了。mousePressed方法中的所有代码语句都将移动到“父”侦听器的同一个方法中。一般提示:1为了更快地获得更好的帮助,请发布一个或。2参见3使用如中所示的a。4.4 NewJFrame2对属性和类使用描述性名称,而不是IDE中类似机器人的建议。mousePressed方法中的所有代码语句都将移动到“父”侦听器的同一个方法中。一般提示:1为了更快地获得更好的帮助,请发布一个或。2参见3使用如中所示的a。4.4 NewJFrame2对属性和类使用描述性名称,而不是IDE中类似机器人的建议。我使用标签是因为我需要图像。按钮也可以有图像:for或示例:So,我的答案仍然有效:我正在使用标签,因为我需要一个图像。按钮也可以有一个图像:例如:那么,我的答案仍然有效:D有什么区别?@samuelowino:调用dispose释放关联的资源我手动添加项目,并使用
从私人空间开始,我似乎有个问题。第一次不工作,第二次打印一次,第三次打印两次,依此类推。试试我的例子。@Danny这是因为每次单击按钮时,都会执行print It works语句,这就是它的ActionListener块中发生的情况,每次单击UI组件时,都会执行其actionListsner调用中的代码块有什么区别?@samuelowino:Calling dispose释放关联的资源我手动添加项目,我使用的代码从private void labelMousePressed开始,我似乎有问题。第一次不工作,第二次打印一次,第三次打印两次,依此类推。试试我的例子。@Danny这是因为每次单击按钮时,都会执行print It works语句,这就是它的ActionListener块中发生的情况,在每次单击UI组件时,都会执行actionListsner调用中的代码块。我很早就看到,您选择了JLabel,因为您需要一个映像,您也可以通过JButton实现这一点。查看我的下一篇编辑文章You are welcome@Danny中的代码,这只是一个建议,请尝试介绍如何操作Swing、核心Java 2和Java,以及更深入理解Swing UI编程的完整参考书。他们为我工作。我很早就知道你选择JLabel是因为你需要一个图像,你也可以通过JButton实现这一点。查看我的下一篇编辑文章You are welcome@Danny中的代码,这只是一个建议,请尝试介绍如何操作Swing、核心Java 2和Java,以及更深入理解Swing UI编程的完整参考书。他们为我工作。
//new ImageIcon(getClass().getResource("/res/image-file_name"));**

package StackOverflowProblemSets;

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://stackoverflow.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);
}}