Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中更新绑定的JLabel文本_Java_Swing_User Interface_Intellij Idea_Jlabel - Fatal编程技术网

在Java中更新绑定的JLabel文本

在Java中更新绑定的JLabel文本,java,swing,user-interface,intellij-idea,jlabel,Java,Swing,User Interface,Intellij Idea,Jlabel,我正在使用IntellijIDEA的表单编辑器,并将一个JLabel绑定到我的鼠标输入类。我的目标是在表单上获得鼠标的x和y坐标,并将它们打印到标签上。一切都在膨胀,我的代码通常可以正常工作,我可以将JLabel文本设置为x和y的值,但只有在表单加载时设置一次。我一辈子都搞不清楚如何在表单加载后定期更新这些值。我不熟悉Java和IntelliJ,所以我很可能错过了一些简单的东西。这是我的密码 package com.company; import javax.swing.*; import j

我正在使用IntellijIDEA的表单编辑器,并将一个JLabel绑定到我的鼠标输入类。我的目标是在表单上获得鼠标的x和y坐标,并将它们打印到标签上。一切都在膨胀,我的代码通常可以正常工作,我可以将JLabel文本设置为x和y的值,但只有在表单加载时设置一次。我一辈子都搞不清楚如何在表单加载后定期更新这些值。我不熟悉Java和IntelliJ,所以我很可能错过了一些简单的东西。这是我的密码

package com.company;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import static javax.swing.JFrame.*;

public class mouse_input {
    private JPanel mouse_pad;
    private JLabel label;

    private static int x;
    private static int y;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                displayJframe();
            }
        });

    }
    static void displayJframe(){
        // Create blank content frame
        JFrame frame = new JFrame("Mouse Input");
        frame.setContentPane(new mouse_input().mouse_pad);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.pack();
        frame.validate();

        // Add mouse motion listener
        frame.addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseDragged(MouseEvent e) {
                // It doesn't like me deleting this
            }

            @Override
            public void mouseMoved(MouseEvent e) {
                x = e.getX();
                y = e.getY();
                frame.revalidate();
            }
        });

        // Set cursor type
        frame.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));

        // Display frame
        frame.setVisible(true);
    }

    private void createUIComponents() {
        label = new JLabel();
        label.setText("X" + x + "Y" + y);
    }
}

现在,我已经做了一些研究,探索并发现我可能能够从头开始创建一个JLabel,但我真的很想尝试一下,看看是否可以专门使用IntelliJ的gui编辑器,我还没有看到任何类似的东西。再说一次,我可能只是个大白痴。如果有任何反馈,我将不胜感激。

从新的应用程序开始

private static void createAndShowGUI() {
    //Make sure we have nice window decorations.
    JFrame.setDefaultLookAndFeelDecorated(true);

    //Create and set up the window.
    JFrame frame = new JFrame("HelloWorldSwing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Add the ubiquitous "Hello World" label.
    JLabel label = new JLabel("Hello World");
    frame.getContentPane().add(label);

    //Display the window.
    frame.pack();
    frame.setVisible(true);

 // Add mouse motion listener
    frame.addMouseMotionListener(new MouseMotionListener() {
        @Override
        public void mouseDragged(MouseEvent e) {
            // It doesn't like me deleting this
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            int x = e.getX();
            int y = e.getY();
            label.setText("X" + x + "Y" + y);
            frame.revalidate();
        }
    });

}

public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}
归功于
从新的应用程序开始

private static void createAndShowGUI() {
    //Make sure we have nice window decorations.
    JFrame.setDefaultLookAndFeelDecorated(true);

    //Create and set up the window.
    JFrame frame = new JFrame("HelloWorldSwing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Add the ubiquitous "Hello World" label.
    JLabel label = new JLabel("Hello World");
    frame.getContentPane().add(label);

    //Display the window.
    frame.pack();
    frame.setVisible(true);

 // Add mouse motion listener
    frame.addMouseMotionListener(new MouseMotionListener() {
        @Override
        public void mouseDragged(MouseEvent e) {
            // It doesn't like me deleting this
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            int x = e.getX();
            int y = e.getY();
            label.setText("X" + x + "Y" + y);
            frame.revalidate();
        }
    });

}

public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}
归功于

当我运行您发布的代码时,我得到了一个
NullPointerException
。这是堆栈跟踪的一部分。(请注意,我的环境是[64位]Windows 10上的JDK 13.0.1。)

您发布的这行代码导致了错误

frame.setContentPane(新的鼠标输入();
这是因为
鼠标垫
从未初始化过。
这很容易解决

private JPanel mouse_pad=new JPanel();
当我运行更改后的代码时,将显示以下窗口

这是因为一个
JPanel
和所有Swing容器一样,没有定义的大小,因为它具有包含的[GUI]组件的大小。事实上,方法
pack()。因此,对于您发布的代码,为了有一个适当大小的窗口,我们需要显式地设置一个大小。因此,我们需要的不是
pack()
(例如)

frame.setSize(400300);
现在,当我运行更新的代码时,我得到

标签在哪里?根据您发布的代码,它永远不会添加到
框架
。您需要将两个组件添加到
框架
,即
鼠标垫
标签
。如果将
鼠标垫
设置为“内容窗格”,则无处也无法添加
标签
。这就是布局管理器的重要性所在。不幸的是,如果您是Swing新手,并且依赖GUI构建器,那么如何利用布局管理器来组织希望显示的所有组件并不明显。在我看来,学习基础知识很重要。GUI生成器适用于熟悉Swing并知道如何利用该生成器来节省时间和精力的程序员。它不是学习挥杆或缩短学习曲线的工具。因此,作为一个学习了基础知识的人,我知道内容窗格的默认布局管理器是
BorderLayout
,因此在您发布的代码中,我现在将调用方法
setContentPane()
替换为以下内容

frame.add(鼠标垫,BorderLayout.CENTER);
createUIComponents();//为了初始化成员“标签”
添加(标签、边框布局、页面开始);
最后,您需要向接收鼠标运动事件的组件添加
MouseMotionListener
。这不是
JFrame
,而是
JPanel
,即在这种情况下是
鼠标垫
。此外,GUI生成器可能不会让您知道使用空方法实现
MouseMotionListener
的类
MouseMotionAdapter
,因此如果您编写扩展
MouseMotionAdapter
的类,则只需实现相关方法,而不是所有方法,这解释了您在发布的代码中的以下注释中注意到的内容

//它不喜欢我删除这个

这是固定代码。这不是最好的实现,但它回答了您的问题,基本上是:

我的代码不起作用。我应该如何改变它以使其工作

导入java.awt.BorderLayout;
导入java.awt.Cursor;
导入java.awt.event.MouseEvent;
导入java.awt.event.MouseMotionAdapter;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
公共类鼠标输入{
private JPanel mouse_pad=new JPanel();
私人标签;
私有静态int x;
私人静态智力;
公共静态void main(字符串[]args){
MouseInput实例=新建MouseInput();
SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
createUIComponents();
displayJframe();
}
});
}
public void displayJframe(){
//创建空白内容框
JFrame=新的JFrame(“鼠标输入”);
frame.add(鼠标垫,BorderLayout.CENTER);
添加(标签、边框布局、页面开始);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
框架。设置尺寸(400300);
frame.setVisible(true);
//添加鼠标运动侦听器
鼠标垫。addMouseMotionListener(新的MouseMotionAdapter(){
@凌驾
public void mouseMoved(MouseEvent e){
x=e.getX();
y=e.getY();
label.setText(“X=“+X+”,Y=“+Y”);
}
});
//设置光标类型
frame.setCursor(新光标(Cursor.CROSSHAIR_Cursor));
//显示框
frame.setVisible(true);
}
私有void createUIComponents(){
label=新的JLabel();
label.setText(“X=“+X+”,Y=“+Y”);
}
}
对于学习Swing,我推荐
但是,如果您刚刚开始使用Java进行GUI编程,您是否考虑过JavaFX