将鼠标坐标移动到JFrame的标题上是否有方法?

huangapple go评论183阅读模式
英文:

Is there a way to move mouse coordinates to the title of the JFrame?

问题

得到 `method` `paint()` 以在 `JFrame` 中绘制鼠标坐标 `x=20, y=20`。有没有办法将渲染的鼠标坐标移到 `JFrame``title`尝试使用 `jFrame.setTitle()`,但它需要一个 `String` 作为参数

jFrame.setTitle("Coordinates x:" + xCord + " y:" + yCord) 无效

以下是我的代码

private static void paint() {
    JComponent jComponent = new JComponent() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Font font = new Font("Comic Sans MS", Font.BOLD, 20);
            g.setFont(font);
            g.drawString("Coordinates x:" + xCord + " y:" + yCord, 20, 20);
        }
    };

    jFrame.add(jComponent);
    jFrame.addMouseMotionListener(new MouseAdapter() {
        @Override
        public void mouseMoved(MouseEvent e) {
            super.mouseMoved(e);
            xCord = e.getX();
            yCord = e.getY();
            jComponent.repaint();
        }
    });
}
英文:

Got method paint() to draw mouse coordinates in JFrame in coordinates x=20, y=20. Is there a way to move rendering mouse coordinates to the title of the JFrame? Try to use jFrame.setTitle() but it want String as parametr.

 jFrame.setTitle(g.drawString("Coordinates x:" + xCord + " y" + yCord, 20, 20)) not work.

Here is my code:

 private static void paint() {
        JComponent jComponent = new JComponent() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Font font = new Font("Comic Sans MS", Font.BOLD, 20);
                g.setFont(font);
                g.drawString("Coordinates x:" + xCord + " y" + yCord, 20, 20);

            }
        };

        jFrame.add(jComponent);
        jFrame.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                super.mouseMoved(e);
                xCord = e.getX();
                yCord = e.getY();
                jComponent.repaint();
            }
        });
    }

答案1

得分: 0

以下是你要翻译的内容:

这是一个更新JFrame标题的简单GUI示例。

将鼠标坐标移动到JFrame的标题上是否有方法?

我刚刚使用MouseMotionListenermouseMoved方法中的坐标更新了标题String

以下是可运行的示例代码:

import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class MouseCoordinatesTitle implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new MouseCoordinatesTitle());
    }
    
    private JFrame frame;
    
    private String title;
    
    public MouseCoordinatesTitle() {
        this.title = "Coordinates: ";
    }

    @Override
    public void run() {
        frame = new JFrame(title);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        frame.add(createMainPanel());
        
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
    
    private JPanel createMainPanel() {
        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(600, 400));
        panel.addMouseMotionListener(new PositionListener());
        return panel;
    }
    
    public void updateJFrameTitle(Point point) {
        StringBuilder builder = new StringBuilder(title);
        builder.append("X: ");
        builder.append(point.x);
        builder.append(", Y: ");
        builder.append(point.y);
        frame.setTitle(builder.toString());
    }
    
    public class PositionListener implements MouseMotionListener {

        @Override
        public void mouseMoved(MouseEvent event) {
            Point point = event.getPoint();
            updateJFrameTitle(point);
        }

        @Override
        public void mouseDragged(MouseEvent event) {
        
        }

    }

}
英文:

Here's a simple example of a GUI that updates the title of the JFrame.

将鼠标坐标移动到JFrame的标题上是否有方法?

I just updated the title String with the coordinates from the mouseMoved method of the MouseMOtionListener.

Here's the runnable, contained, example.

import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class MouseCoordinatesTitle implements Runnable {

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new MouseCoordinatesTitle());
	}
	
	private JFrame frame;
	
	private String title;
	
	public MouseCoordinatesTitle() {
		this.title = "Coordinates: ";
	}

	@Override
	public void run() {
		frame = new JFrame(title);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		frame.add(createMainPanel());
		
		frame.pack();
		frame.setLocationByPlatform(true);
		frame.setVisible(true);
	}
	
	private JPanel createMainPanel() {
		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(600, 400));
		panel.addMouseMotionListener(new PositionListener());
		return panel;
	}
	
	public void updateJFrameTitle(Point point) {
		StringBuilder builder = new StringBuilder(title);
		builder.append("X: ");
		builder.append(point.x);
		builder.append(", Y: ");
		builder.append(point.y);
		frame.setTitle(builder.toString());
	}
	
	public class PositionListener implements MouseMotionListener {

		@Override
		public void mouseMoved(MouseEvent event) {
			Point point = event.getPoint();
			updateJFrameTitle(point);
		}

		@Override
		public void mouseDragged(MouseEvent event) {
		
		}

	}

}

huangapple
  • 本文由 发表于 2020年10月6日 01:14:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64213281.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定