[英]JLabel doesnt show on JPanel
I already looked up this problem and found several threads.我已经查找了这个问题并找到了几个线程。 Tried all the solutions and none of them helped.
尝试了所有的解决方案,但没有一个有帮助。 I am trying to show a cross where my mouse is placed, the x and the y coordinate of my current mouse position is supposed to be shown in the top left and top right corner.
我试图在我的鼠标所在的位置显示一个十字,我当前鼠标位置的 x 和 y 坐标应该显示在左上角和右上角。 In order to achieve this, I used two JLabels.
为了实现这一点,我使用了两个 JLabel。 Maybe I am overlooking something?
也许我忽略了什么?
I played around with the standard Text I set in the Labels, positioning, different Layouts for my frame and panel - nothing helps.我在标签中设置了标准文本,定位,框架和面板的不同布局 - 没有任何帮助。 The following code should be good enough to get an understanding, I dont think it would be helpful if I left out something.
下面的代码应该足以让人理解,如果我遗漏了一些东西,我认为这不会有帮助。
Fensterkreuz(){
jl1 = new JLabel("0");
jl2 = new JLabel("0");
jl1.setSize(new Dimension(100,100));
jl2.setSize(new Dimension(100,100));
jl1.setFont(new Font ("Arial", Font.PLAIN, 15));
jl2.setFont(new Font ("Arial", Font.PLAIN, 15));
cP = new Point();
this.add(jl1);
this.add(jl2);
addMouseMotionListener(this);
}
public void mouseDragged (MouseEvent e){
}
public void mouseMoved (MouseEvent e) {
cP = e.getPoint();
repaint();
}
public void paint (Graphics g){
g.drawLine((cP.x),(cP.y-15), (cP.x),(cP.y+15));
g.drawLine((cP.x-15),(cP.y), (cP.x+15),(cP.y));
jl1.setText(String.valueOf(cP.x));
jl2.setText(String.valueOf(cP.y));
}
public static void main (String [] args) {
JFrame f = new JFrame();
JComponent test = new Fensterkreuz();
test.setOpaque(false);
f.setVisible(true);
f.setSize(1500,1000);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(test);
}
You are overriding the paint()
method.您正在覆盖
paint()
方法。 So, you need add super.paint(g);
所以,你需要添加
super.paint(g);
as the first line in your overridden paint()
method.作为您重写的
paint()
方法中的第一行。
To show the 2 labels properly, you need to add this.setLayout(new FlowLayout(FlowLayout.LEFT));
要正确显示 2 个标签,您需要添加
this.setLayout(new FlowLayout(FlowLayout.LEFT));
line.线。
I'm adding the full code with above changes here, so that you can run it and see the results yourself.我在这里添加了具有上述更改的完整代码,以便您可以运行它并自己查看结果。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Fensterkreuz extends JComponent implements MouseMotionListener {
private JLabel jl1;
private JLabel jl2;
private Point cP;
Fensterkreuz(){
jl1 = new JLabel("0");
jl2 = new JLabel("0");
jl1.setSize(new Dimension(100,100));
jl2.setSize(new Dimension(100,100));
jl1.setFont(new Font ("Arial", Font.PLAIN, 15));
jl2.setFont(new Font ("Arial", Font.PLAIN, 15));
cP = new Point();
//this.setLayout(new FlowLayout(FlowLayout.LEFT));
//this.add(jl1);
//this.add(jl2);
this.setLayout(new GridBagLayout());
this.add(jl1, new GridBagConstraints(0, 0, 1, 1, 0.0, 1.0, GridBagConstraints.NORTHWEST,
GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
this.add(jl2, new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHEAST,
GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
addMouseMotionListener(this);
}
public void mouseDragged (MouseEvent e){
}
public void mouseMoved (MouseEvent e) {
cP = e.getPoint();
repaint();
}
public void paint (Graphics g){
super.paint(g);
g.drawLine((cP.x),(cP.y-15), (cP.x),(cP.y+15));
g.drawLine((cP.x-15),(cP.y), (cP.x+15),(cP.y));
jl1.setText(String.valueOf(cP.x));
jl2.setText(String.valueOf(cP.y));
}
public static void main (String [] args) {
JFrame f = new JFrame();
JComponent test = new Fensterkreuz();
test.setOpaque(false);
f.setVisible(true);
f.setSize(1500,1000);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(test);
}
}
Put repaint() at the bottom of your main method.将 repaint() 放在 main 方法的底部。 Repaint calls the Paint method you have but I think you also have to add your own overriding repaint method to stop “flickering”.
Repaint 调用您拥有的 Paint 方法,但我认为您还必须添加自己的覆盖重绘方法以停止“闪烁”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.