jDialog已添加两个JPanel,如何给jDialog添加背景图片啊,并且JPanel是透明的,求大神!
代码如下JDialogjDialog=newJDialog();JPaneljp1=newJPanel();JPaneljp2=newJPanel();JLabeljl=...
代码如下
JDialog jDialog = new JDialog();
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JLabel jl = new JLabel("正在测量......");
jp1.add(jl);
JButton jb = new JButton("取消");
jp2.add(jb);
jp1.setOpaque(false);
jp2.setOpaque(false);
jDialog.setUndecorated(true);
jDialog.add("Center",jp1);
jDialog.add("South",jp2);
jDialog.setSize(400, 200);
jDialog.setLocationRelativeTo(null);
jDialog.setVisible(true); 展开
JDialog jDialog = new JDialog();
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JLabel jl = new JLabel("正在测量......");
jp1.add(jl);
JButton jb = new JButton("取消");
jp2.add(jb);
jp1.setOpaque(false);
jp2.setOpaque(false);
jDialog.setUndecorated(true);
jDialog.add("Center",jp1);
jDialog.add("South",jp2);
jDialog.setSize(400, 200);
jDialog.setLocationRelativeTo(null);
jDialog.setVisible(true); 展开
1个回答
展开全部
按照你的要求给jDialog添加背景图片的Java程序如下
注意 背景图片要放在Java当前项目的根目录下
比如你的当前项目名是project1, 在D:\project\project1\ 下就是你的当前项目的根目录(其中D:\project\只是打个比方,以你的实际目录为准)
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
public class D {
D(){
JDialog jDialog = new JDialog();
jDialog.setSize(400, 200);
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
Container container = jDialog.getContentPane();
JPanel panelBg=new JPanel();
JPanel panelContent=new JPanel();
JLayeredPane lp=new JLayeredPane();
Dimension dimension = new Dimension(jDialog.getWidth(), jDialog.getHeight());
lp.setSize(dimension);
panelContent.setLayout(new BorderLayout());
Image imageBg = Toolkit.getDefaultToolkit().getImage("autumn1.jpg");
ImageIcon imageIcon = new ImageIcon(imageBg.getScaledInstance
(dimension.width, dimension.height, Image.SCALE_FAST));
JLabel jl1 = new JLabel(imageIcon);
Point origin = new Point(0, 0);
Rectangle rectangle = new Rectangle(origin, dimension);
panelBg.setBounds(rectangle);
panelBg.setLayout(new BorderLayout());
panelBg.add(jl1,BorderLayout.CENTER);
JLabel jl = new JLabel("正在测量......");
jp1.add(jl);
JButton jb = new JButton("取消");
jp2.add(jb);
jp1.setOpaque(false);
jp2.setOpaque(false);
panelContent.setBounds(rectangle);
panelContent.add(jp1,BorderLayout.CENTER);
panelContent.add(jp2,BorderLayout.SOUTH);
panelContent.setOpaque(false);
lp.add(panelBg,new Integer(0));
lp.add(panelContent, new Integer(1));
container.add(lp, BorderLayout.CENTER);
jDialog.setUndecorated(true);
jDialog.setLocationRelativeTo(null);
jDialog.setVisible(true);
}
public static void main(String[] args) {
new D();
}
}
运行结果
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询