简体   繁体   English

线未显示在图像jframe上

[英]line not showing on image jframe

package carspeedometer;

import java.awt.Graphics;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

class a1 {

    a1() {
        JFrame jf = new JFrame("Speedometer");
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel jp = new JPanel();
        JLabel jb = new JLabel(new ImageIcon(
                "C:/Users/Vinayak/Desktop/tester.jpg"));
        jp.add(jb);

        jf.add(jp);
        jf.setVisible(true);
        jf.setSize(700, 700);
    }

    public void paint(Graphics g) {
        g.drawLine(70, 70, 200, 200);
    }

    public static void main(String...s) {
        new a1();
    }
}

Line is not showing on screen.I want to show line on top of the image.please help. 屏幕上未显示线条。我想在图像上方显示线条。请帮助。 Here i am trying to build a speedometer but first a line needs to be displayed 在这里,我试图建立一个速度计,但首先需要显示一条线

You can only draw in Swing if you override a drawing method of a component. 如果您重写组件的绘制方法,则只能在Swing中进行绘制。 Here your paint method overrides nothing because your class extends nothing. 在这里,您的paint方法不会覆盖任何内容,因为您的类不会扩展任何内容。 I suggest 我建议

  • that you create a class that extends JPanel 您创建一个扩展JPanel的类
  • that you override the JPanel's paintComponent(Graphics g) method 您重写了JPanel的paintComponent(Graphics g)方法
  • that you use the @Override annotation to verify the override 您使用@Override批注来验证替代
  • that you place the JPanel into a JFrame and display it and 您将JPanel放入JFrame并显示它,并且
  • that you read the Swing Painting Tutorials . 您阅读了《 Swing Painting教程》 You don't want to guess at this stuff. 您不想猜测这些东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM