Java-如何使文本显示在您单击的位置?
我创建了一个Jframe,该Jframe根据您单击的位置显示X和Y值。 Jframe为300x100px。
现在,当我单击Jframe内部时,我确实会得到正确的x和y点,但文本在Jframe上以随机斑点出现。我希望文字出现在用户点击何处而不是在随机点中出现的,因为它经常出现在框架之外。.我在代码中做错了什么?
************************************************************/
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.event.MouseInputListener;
public class Proj06Runner {
public static void main(String[] args) {
System.out.println("NAME"); //replace with name
//creating jframe
JFrame main = new JFrame("NAME"); //replace with name
//set size of frame
main.setSize(300, 100);
//creating lable
JLabel j1 = new JLabel("");
j1.setForeground(Color.RED);
//make frmae visible
main.setVisible(true);
//addding mouselistner to frame
main.addMouseListener(new MouseInputListener() {
@Override
public void mouseClicked(MouseEvent e) {
String output = "x=" + e.getX() + ",y=" + e.getY();
//add label to text which is shown
j1.setText(output);
Dimension size=j1.getPreferredSize();
j1.setBounds(e.getX(),e.getY(),size.width,size.height);
main.add(j1);
}
//MouseInputListner is interface so that we have to implement all methods
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
});
//make progrma terminate after clicking close button
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
I have created a Jframe that displays the x and y value based on where you click. The JFrame is 300x100px.
Right now when I click inside the JFrame i do get the correct x and y points but the text appears in random spots on the JFrame. I want the text to appear wherever the user clicks instead of in random spots because often times it shows up outside of the frame..what am i doing wrong in my code?
************************************************************/
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.event.MouseInputListener;
public class Proj06Runner {
public static void main(String[] args) {
System.out.println("NAME"); //replace with name
//creating jframe
JFrame main = new JFrame("NAME"); //replace with name
//set size of frame
main.setSize(300, 100);
//creating lable
JLabel j1 = new JLabel("");
j1.setForeground(Color.RED);
//make frmae visible
main.setVisible(true);
//addding mouselistner to frame
main.addMouseListener(new MouseInputListener() {
@Override
public void mouseClicked(MouseEvent e) {
String output = "x=" + e.getX() + ",y=" + e.getY();
//add label to text which is shown
j1.setText(output);
Dimension size=j1.getPreferredSize();
j1.setBounds(e.getX(),e.getY(),size.width,size.height);
main.add(j1);
}
//MouseInputListner is interface so that we have to implement all methods
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
});
//make progrma terminate after clicking close button
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为布局。布局经理用于自动确定容器中组件的布局。如果要将组件放在特定的坐标位置,则根本不应使用布局管理器。
将Jframe的布局设置为null。
It is because of the layout. Layout managers are used to automatically determine the layout of components in a container. If you want to put components at specific coordinate locations, then you should not use a layout manager at all.
Either set layout of JFrame or JPanel as a null.
您可以...
使用基于自定义绘画的解决方案,例如...
请参阅 aw和摇摆和执行自定义绘画
有关更多详细信息,
您可以...
使用自定义布局管理器...
这将与其他组件效果很好,并且您可以将组件包装在
jscrollpane
中,并免费获取可解开的窗口。当您认为不需要布局经理时,99.9%的时间,您错了,其他0.1%的时间,您需要一个自定义布局管理器
You could...
Use a custom painting based solution, for example...
See Painting in AWT and Swing and Performing Custom Painting
for more details
You could...
Use a custom layout manager...
This will play nicely with other components and you could wrap the component in a
JScrollPane
and get a resizable window for free.99.9% of the time when you think you don't need a layout manager, you're wrong, the other 0.1% of the time, you need a custom layout manager