MouseListener第一次没有工作

时间:2017-06-05 13:51:47

标签: java swing mouselistener

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

        }          
    });

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

4 个答案:

答案 0 :(得分:6)

而不是点击JLabel为什么不创建JButton已经使用ActionListener处理点击并使其看起来像JLabel,如多个答案中所示在this question

  

但它不会隐藏当前的JFrame

嗯,你需要在你的听众上调用JFrame#dispose()方法,但也请看看The Use of Multiple JFrames: Good or Bad Practice?,最好使用Card Layout或者看看教程在How to use Dialogs

答案 1 :(得分:0)

使用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(继承)JFrame类。而不是从JFrame创建一个对象并使用它。阅读此question to learn more about this

答案 2 :(得分:0)

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

答案 3 :(得分:0)

Java标签无法接收ActionListener事件,您应该使用按钮替换标签。你没有点击你点击按钮的标签,标签可能起作用的可能是属性改变听众。

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

中的文件名替换图像文件名
//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);
}}
相关问题