JFrame设置背景

上传人:飞*** 文档编号:4679726 上传时间:2017-08-22 格式:DOC 页数:7 大小:37KB
返回 下载 相关 举报
JFrame设置背景_第1页
第1页 / 共7页
JFrame设置背景_第2页
第2页 / 共7页
JFrame设置背景_第3页
第3页 / 共7页
JFrame设置背景_第4页
第4页 / 共7页
JFrame设置背景_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《JFrame设置背景》由会员分享,可在线阅读,更多相关《JFrame设置背景(7页珍藏版)》请在金锄头文库上搜索。

1、设置背景:import java.awt.Container; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class Test extends JFrame public Test() setBak(); /调用背景方法 Container c = getContentPane(); /获取 JFrame 面板 JPanel jp = new JPanel(); /创建个 JPanel jp.setOp

2、aque(false); /把 JPanel 设置为透明 这样就不会遮住后面的背景 这样你就能在 JPanel 随意加组件了 c.add(jp); setSize(100, 200); setVisible(true); public void setBak() (JPanel)this.getContentPane().setOpaque(false); ImageIcon img = new ImageIcon(d:/Java/01.jpg); JLabel background = new JLabel(img);this.getLayeredPane().add(background,

3、 new Integer(Integer.MIN_VALUE); background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight(); public static void main(String args) Test s = new Test(); s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 摘自:http:/ 作者:神之泪花法二 :你在建立 swing 窗体的时候,java 会自动生成一个类(Application) 这时候你需要把添加的图片写在 JFrame 类里,由

4、于你要设置一个窗体,窗体肯定有属性(例如大小 .),把窗体的属性写在程序的入口 Application 里. 这就就能解决了. public class Text extends JFrame ImageIcon img; public Landing() img = new ImageIcon(图片名加后缀格式); JPanel panel = new JPanel() public void paintComponent(Graphics g) g.drawImage(img.getImage(), 0, 0, null); super.paintComponent(g); ; panel

5、.setOpaque(false); setContentPane(panel); try jbInit(); catch (Exception ex) ex.printStackTrace(); 把窗体的设置写在 Application 类里, 这里是入口 Landing frame = new Landing(); frame.setBounds(,); frame.setVisible(true);import java.awt.FlowLayout;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.

6、swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;public class JFrameBackground private JFrame frame = new JFrame(背景图片测试);private JPanel imagePanel;private ImageIcon background;public static void main(String args) new JFrameBackground(); public JFrameBackground() background = new Imag

7、eIcon(003.jpg);/ 背景图片JLabel label = new JLabel(background);/ 把背景图片显示在一个标签里面/ 把标签的大小位置设置为图片刚好填充整个面板label.setBounds(0, 0, background.getIconWidth(),background.getIconHeight();/ 把内容窗格转化为 JPanel,否则不能用方法 setOpaque()来使内容窗格透明imagePanel = (JPanel) frame.getContentPane();imagePanel.setOpaque(false);/ 内容窗格默认的

8、布局管理器为 BorderLayoutimagePanel.setLayout(new FlowLayout();imagePanel.add(new JButton(测试按钮);frame.getLayeredPane().setLayout(null);/ 把背景图片添加到分层窗格的最底层作为背景frame.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(background.getIco

9、nWidth(), background.getIconHeight();frame.setResizable(false);frame.setVisible(true);public class Bj private JFrame frame = new JFrame(背景图片测试 );private JPanel imagePanel;private ImageIcon background;public static void main(String args) new Bj();public Bj() background = new ImageIcon(img/IMG_2014020

10、3_135531.jpg);/ 背景图片JLabel label = new JLabel(background);/ 把背景图片显示在一个标签里面/ 把标签的大小位置设置为图片刚好填充整个面板label.setBounds(0, 0, background.getIconWidth(),background.getIconHeight();/ 把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明imagePanel = (JPanel) frame.getContentPane();imagePanel.setOpaque(false); / 内容窗格默认的布局

11、管理器为BorderLayoutimagePanel.setLayout(new FlowLayout();imagePanel.add(new JButton(测试按钮 );frame.getLayeredPane().setLayout(null);/ 把背景图片添加到分层窗格的最底层作为背景frame.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(background.getIconWidth(), background.getIconHeight();frame.setResizable(false);frame.setVisible(true);

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 研究报告 > 综合/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号