鼠标侦听器第一次不起作用



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

我的代码是:

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

有谁知道如何修复它以便从第一次单击开始工作并隐藏当前框架?

与其点击JLabel不如创建一个已经处理ActionListener点击的JButton,并使其看起来像一个JLabel,如这个问题的多个答案所示。

但它不会隐藏当前的 JFrame

好吧,你需要在侦听器上调用JFrame#dispose()方法,但也请查看使用多个 JFrames:好或坏的做法?,最好使用卡片布局或查看如何使用对话框的教程

Java 标签无法接收 ActionListener 事件,您应该用按钮替换标签。您不单击单击按钮的标签,可能对标签有用的可能是属性更改侦听器。

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

//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 事件,您应该用按钮替换标签。您不单击单击按钮的标签,可能对标签有用的可能是属性更改侦听器。

运行并分析此代码,它将清楚地指导您...祝你好运,你选择了世界上最好的语言,我是一个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);
}}

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

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

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创建一个对象并使用它。 阅读此问题以了解更多信息。

最新更新