JPanel的背景图片如何设置?
答案:1 悬赏:0 手机版
解决时间 2021-04-04 23:46
- 提问者网友:欲望失宠
- 2021-04-04 15:50
JPanel的背景图片如何设置?
最佳答案
- 五星知识达人网友:等灯
- 2021-04-04 16:46
publicclassTest{privateJFrameframe=newJFrame("背景图片测试");
privateJPanelimagePanel;
privateImageIconbackground;
publicstaticvoidmain(String[]args){newTest();}
publicTest(){background=newImageIcon("1.jpg");//背景图片
JLabellabel=newJLabel(background);//把背景图片显示在一个标签里面
//把标签的大小位置设置为图片刚好填充整个面板
label.setBounds(0,0,background.getIconWidth(),background.getIconHeight());
//把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明
imagePanel=(JPanel)frame.getContentPane();
imagePanel.setOpaque(false);
//内容窗格默认的布局管理器为BorderLayout
imagePanel.setLayout(newFlowLayout());
imagePanel.add(newJButton("测试按钮"));
frame.getLayeredPane().setLayout(null);
//把背景图片添加到分层窗格的最底层作为背景
frame.getLayeredPane().add(label,newInteger(Integer.MIN_VALUE));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
privateJPanelimagePanel;
privateImageIconbackground;
publicstaticvoidmain(String[]args){newTest();}
publicTest(){background=newImageIcon("1.jpg");//背景图片
JLabellabel=newJLabel(background);//把背景图片显示在一个标签里面
//把标签的大小位置设置为图片刚好填充整个面板
label.setBounds(0,0,background.getIconWidth(),background.getIconHeight());
//把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明
imagePanel=(JPanel)frame.getContentPane();
imagePanel.setOpaque(false);
//内容窗格默认的布局管理器为BorderLayout
imagePanel.setLayout(newFlowLayout());
imagePanel.add(newJButton("测试按钮"));
frame.getLayeredPane().setLayout(null);
//把背景图片添加到分层窗格的最底层作为背景
frame.getLayeredPane().add(label,newInteger(Integer.MIN_VALUE));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯