英文:
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示例。
我刚刚使用MouseMotionListener
的mouseMoved
方法中的坐标更新了标题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.
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) {
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论