MouseListener 第一次不起作用

MouseListener doesn't work the first time

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

我的代码是:

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.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();
    }
}

更新

如果不覆盖JFrame中的方法,就没有必要extends(继承)JFrameclass。而不是从 JFrame 创建一个对象并使用它。读这个 question to learn more about this.

与其点击 JLabel,不如创建一个 JButton,它已经使用 ActionListener 处理点击,并使其看起来像 JLabel,如this question.

的多个答案

but it does NOT hide the current JFrame

嗯,你需要打电话给JFrame#dispose() method on your listener, but also please take a look at The Use of Multiple JFrames: Good or Bad Practice?, it's better to use a Card Layout or maybe take a look at the tutorial on How to use Dialogs

Java 标签无法接收ActionListener 事件,您应该将标签替换为按钮。你不点击标签而是点击按钮,对标签有用的可能是 属性 改变监听器。

运行 并分析这段代码,它将清楚地指导您...祝您好运,您选择了世界上最好的语言,我是 java 家伙二

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

Java 标签无法接收ActionListener 事件,您应该将标签替换为按钮。你不点击标签而是点击按钮,对标签有用的可能是 属性 改变监听器。

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

中的图像文件名替换为我的文件名
//new ImageIcon(getClass().getResource("/res/image-file_name"));**

package WhosebugProblemSets;

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.
 * 
   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);
}}