有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java中同一jframe中的jlabel和paintComponent

下面的代码只调用类(类“oefen”),其中包含生成jframe和球的代码(使用painComponent方法),如果按箭头键,它将朝该方向移动。。。它工作得很好,但是当我将目标(jlabel)添加到jframe时,只有目标显示,没有其他显示。。请帮我把目标和球放在同一个框架内

public class theFrame implements KeyListener {

    public  static JFrame j = new JFrame();
    public static ImageIcon tar = new ImageIcon("c://fruit//target.png");
    public static JLabel target = new JLabel(tar);
    public static JPanel p = new JPanel();
    public static void main(String args[]){
        j.setSize(500,600);
        j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        j.setVisible(true);
        oefen o = new oefen();
        j.add(o);
        target.setLayout(null);
        target.setSize(100,100);
        target.setLocation(250,0);
        j.add(target);
    }

}

谢谢

公共类框架扩展了JComponent{

public  static JFrame j = new JFrame();
public static ImageIcon tar = new ImageIcon("c://fruit//target.png");
public static JLabel target = new JLabel(tar);
public static JPanel p = new JPanel();
public static oefen o = new oefen();
public static void main(String args[]){

    j.setSize(500,600);
    j.setLayout(null);
    j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    j.setVisible(true);
    target.setLayout(null);
    target.setSize(0,0);
    target.setLocation(0,0);
    j.add(o);
    j.add(target);
}

}

如果我将JFrame布局设置为null notihng shows。。。上面的代码显示了如何设置JFrameLayout,因为我不知道它是否错误
请帮忙


共 (1) 个答案

  1. # 1 楼答案

    尝试将JFrames布局也设置为null,因为您使用绝对定位