资源预览内容
第1页 / 共7页
第2页 / 共7页
第3页 / 共7页
第4页 / 共7页
第5页 / 共7页
第6页 / 共7页
第7页 / 共7页
亲,该文档总共7页全部预览完了,如果喜欢就下载吧!
资源描述
设置背景: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.setOpaque(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, 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:/wenda.tianya.cn/wenda/thread?tid=3c95f9c22a1f969d&clk=wttpcts 作者:神之泪花法二 :你在建立 swing 窗体的时候,java 会自动生成一个类(Application) 这时候你需要把添加的图片写在 JFrame 类里,由于你要设置一个窗体,窗体肯定有属性(例如大小 .),把窗体的属性写在程序的入口 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.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.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 ImageIcon(003.jpg);/ 背景图片JLabel label = new JLabel(background);/ 把背景图片显示在一个标签里面/ 把标签的大小位置设置为图片刚好填充整个面板label.setBounds(0, 0, background.getIconWidth(),background.getIconHeight();/ 把内容窗格转化为 JPanel,否则不能用方法 setOpaque()来使内容窗格透明imagePanel = (JPanel) frame.getContentPane();imagePanel.setOpaque(false);/ 内容窗格默认的布局管理器为 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);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_20140203_135531.jpg);/ 背景图片JLabel label = new JLabel(background);/ 把背景图片显示在一个标签里面/ 把标签的大小位置设置为图片刚好填充整个面板label.setBounds(0, 0, background.getIconWidth(),background.getIconHeight();/ 把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明imagePanel = (JPanel) frame.getContentPane();imagePanel.setOpaque(false); / 内容窗格默认的布局管理器为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);
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号