Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 定位按钮不起作用_Java_Swing_Jpanel_Jlabel_Imageicon - Fatal编程技术网

Java 定位按钮不起作用

Java 定位按钮不起作用,java,swing,jpanel,jlabel,imageicon,Java,Swing,Jpanel,Jlabel,Imageicon,我有一个装饰过的JFrame。我已将背景图像添加到帧中,并添加了JButton。 添加按钮时,背景图像不可见。我该怎么办? 这是我的代码片段 public Demo3() { setTitle("STARTUP"); setSize(800,500); setDefaultCloseOperation(EXIT_ON_CLOSE); setUndecorated(true); setLocationRelativeTo(null); JPane

我有一个装饰过的
JFrame
。我已将背景图像添加到帧中,并添加了
JButton
。 添加按钮时,背景图像不可见。我该怎么办? 这是我的代码片段

public Demo3()

{
    setTitle("STARTUP");
    setSize(800,500);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setUndecorated(true);
    setLocationRelativeTo(null);

    JPanel jp=new JPanel();
    JButton jb=new JButton();
    JLabel jl=new JLabel(new ImageIcon("c://image.jpg"));
    jp.setLayout(null);
    jp.add(jl);
    jp.add(jb);

    add(jp);
    jb.setBounds(400,250,50,50);
    setVisible(true);
}

不要使用空布局

请参阅,以获得几个选项。根据您的具体要求,您可以:

  • 使用JLabel作为背景,然后设置标签的布局管理器
  • 在配电盘上进行自定义绘制,然后向配电盘添加零部件

  • 您需要设置JLabel null的布局。然后只有setBounds方法可以工作

    lj,setLaout(空)


    jb.立根(400250,50,50)

    顺便说一句-你不明白我的建议,张贴SSCCE,或者你只是忽视它?-1,这是一个可怕的解决方案。您不应该使用空布局。Swing设计用于布局管理器。一个月前你得到了一个更好的解决方案。