Java 窗口生成器错误

标签 java eclipse swing windowbuilder

刚刚听说 Window Builder,所以我正在编写一段代码,该应用程序只是获取您的姓名并将其返回到标签中。

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;


public class app {

    private JFrame frame;
    private JTextField textField;
    private JLabel lblNewLabel;
    private static String text;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    app window = new app();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public app() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        lblNewLabel = new JLabel(text);
        textField = new JTextField();
        textField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent arg0) {
                text = textField.getText();
            }
        });
        frame.getContentPane().add(textField, BorderLayout.NORTH);
        textField.setColumns(10);

        JButton btnNewButton = new JButton("Check My Name");
        btnNewButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                lblNewLabel.setText("Your name is " + text + "!");
            }
        });

        frame.getContentPane().add(btnNewButton, BorderLayout.CENTER);


        frame.getContentPane().add(lblNewLabel, BorderLayout.SOUTH);


    }

}

不明白为什么它有时有效而不是一直有效。我是窗口生成器的新手,所以我希望得到详细的答案。

为什么不工作?你能指出我正确的方向吗?

最佳答案

Don't understand why it works sometimes and not all the time.

以后要明确。什么有时有效,什么无效?告诉我们您执行的操作步骤会产生错误。

public void mouseClicked(MouseEvent arg0) {
    lblNewLabel.setText("Your name is " + text + "!");

我猜问题出在上面的代码上。当在同一鼠标点处生成 mousePressed 和 mouseReleased 事件时,会生成 mouseClicked 事件。如果您在两个事件之间移动鼠标,即使移动 1 个像素,也不会生成 mouseClicked 事件。

不要使用 MouseListener。

相反,按钮被设计为与 ActionListener 一起使用。实现 actionPerformed(...) 方法并将 ActionListener 添加到您的按钮。监听器中的代码将是相同的。

阅读 Swing 教程中关于 How to Write an ActionListener 的部分了解更多信息。

如果这不能解决您的问题,则需要使用有关如何创建问题的更多信息来更新您的问题。

关于Java 窗口生成器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29573336/

相关文章:

c# - 从 C# 程序调用 Eclipse 插件中的 Java 方法

java - 重启Eclipse中调试当前函数

JAVA JTextField 不显示

java - 测量锁的争用时间

java - Autodesk Forge Viewer 在本地服务器上存储文件吗?

java - 使用嵌入数字规范化字符串

java - 错误 : Access restriction: The type 'DataSource' is not API (restriction on required library . .\rt.jar)

java - 如何动态更改 JXTreeTable 中特定单元格的颜色

java - Java Swing 应用程序的一个很好的例子是什么?

Java ExecutorService 何时调用 shutdown()