java JLabel问题

application问题!
我用一张图片作为背景图片,然后在上面加两个按钮,怎么做!
问题代码如下:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class SplashWindow extends JFrame
{
JButton ok,no;
JLabel r8=new JLabel(new ImageIcon("Picture.jpg"));
SplashWindow()
{
setLayout(new BorderLayout());
ok=new JButton("ok");
no=new JButton("no");
add(r8,"Center");
toFront();
ok.setLayout(null);
no.setLayout(null);
ok.setSize(30,30);
no.setSize(30,30);
add(ok);
add(no);
setSize(800,600);
setLocation(100,90);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String [] args)
{
SplashWindow splashWindow=new SplashWindow();
}
}
修改代码,并解释!
谢谢!

import java.awt.BorderLayout;

import java.awt.FlowLayout;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

public class T extends JFrame {

 JButton ok, no;

 JLabel r8 = new JLabel(new ImageIcon("picture.jpg"));

 T() {

  setLayout(new BorderLayout());

  ok = new JButton("ok");

  no = new JButton("no");

  add(r8);

  r8.setLayout(new FlowLayout());

  r8.add(ok);

  r8.add(no);

  pack();

  setLocation(100, 90);

  setVisible(true);

  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 }

 public static void main(String[] args) {

  T splashWindow = new T();

 }

}

你原来那种方法是想用 JLabel来做背景图片

可是JLabel和JButton是同级的,怎么能够覆盖

可以用JLabel作为容器,因为它从container继承

所以直接往JLabel中添加组件即可

温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-10-09
布局管理器的问题,正确代码如下
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class SplashWindow extends JFrame
{
JButton ok,no;
JLabel r8=new JLabel(new ImageIcon("Picture.jpg"));
SplashWindow()
{
// setLayout(new BorderLayout(20,20));//不要使用边框布局管理器
this.setLayout(new FlowLayout());//使用流布局管理器,你可以试试这2个布局管理器的区别
ok=new JButton("ok");
no=new JButton("no");
add(r8,"Center");
toFront();

ok.setLayout(null);
no.setLayout(null);
ok.setSize(30,30);
no.setSize(30,30);
this.add(ok, BorderLayout.EAST);
add(no);
setSize(800,600);
setLocation(100,90);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String [] args)
{
SplashWindow splashWindow=new SplashWindow();
}
}
第2个回答  2009-10-09
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class SplashWindow extends JFrame
{
JButton ok,no;
JLabel r8;
public SplashWindow()
{
r8=new JLabel(new ImageIcon("Canyon.png"));
ok=new JButton("ok");
no=new JButton("no");
Container cn=this.getContentPane();
cn.add(r8,"Center");
ok.setSize(30,30);
no.setSize(30,30);
cn.add(ok,BorderLayout.WEST);
cn.add(no,BorderLayout.EAST);
setSize(800,600);
setLocation(100,90);
setVisible(true);
toFront();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String [] args)
{
SplashWindow splashWindow=new SplashWindow();
}
}
问题就在于jframe的应用与frame的区别上添加两个以上组件是不行的
a JFrame contains a JRootPane as its only child. The content pane provided by the root pane should, as a rule, contain all the non-menu components displayed by the JFrame. This is different from the AWT Frame case.
第3个回答  2009-10-09
最近经常见用label显示图片的啊。
其实label不是理想的容器,因为它一般用来容纳文字和图片,作为标签之类的使用。像你只需要作为背景图片的话,估计说的是窗口的背景图片,那么最好是谁的就改谁,不然以后添新东西还需要往label里加。毕竟label不是做这些的,虽然swing里的Jlabel可以作为容器来使用,但其实awt里的Label不能容纳任何东西,从根上就不是为了装按钮或其他东西的。

下面是代码。
我现在身边没有编译环境,只能凭印象写,你可以试试看,应该只会有不小心的拼写错误,我尽量小心。

import java.awt.*;

import javax.swing.*;
import java.awt.event.*;

class SplashWindow extends JFrame
{
JButton ok,no;

//使用Image对象来容纳图片,舍弃掉原有的JLabel。
Image r8=new ImageIcon("Picture.jpg").getImage();

SplashWindow()
{
//这里总觉得不对劲,frame好像不会改变布局,全是默认最大化,不太记得。不过我没有动,大概你有你的需要。
setLayout(new BorderLayout());
ok=new JButton("ok");
no=new JButton("no");

//一般直接把组件放Frame里会出问题,一般都会建一个Panel来装组件,并以内部类的形式重写一下Panel的paintComponent方法,这个方法用来绘制组件本身,但不会影响到组件内部包含的其他组件。
JPanel panel = new JPanel(){

//前缀也可能是public,如果没成功的话,就改成public吧。
protected void paintComponent(Graphics g) {

//这一句把背景绘制到Panel上,就是在00坐标上绘制窗口那么大的图片r8,简单点说就是把图片当panel。
g.drawImage(r8, 0, 0, getWidth(), getHeight(), null);
}};

//这我也没管,没用过,也没见过。
toFront();

//以下两句基本上没有用,虽然swing里每个组件都可以做容器,但其实还是有选择的用容器的一些特性比较好。
ok.setLayout(null);
no.setLayout(null);

ok.setSize(30,30);
no.setSize(30,30);

//把button放在panel里,上面提过,一般不用frame放组件。
panel.add(ok);
panel.add(no);

//把panel添加到frame里。
add(panel);

setSize(800,600);
setLocation(100,90);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String [] args)
{
SplashWindow splashWindow=new SplashWindow();
}
}
相似回答