swing-JPanel-背景图片

JPanel没有直接设置背景图片的方法。如果你想做成有背景的jpanel,需要自己继承JComponent,重载paintComponent,画出设置的壁纸。很简单代码:

问题

很显然这白的颜色太难看了,要去掉就需要这样设置一下:

/setOpaque()/如果为 true,则该组件绘制其边界内的所有像素。

jslider.setOpaque(false);

package com.swing.panel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

public class Demo extends JFrame {
	
	private boolean isShowing;
	private JLabel jlabel;
	private JPanelWithPhoto jpanel;
	private JButton jbutton1;
	private JButton jbutton2;
	private JSlider jslider;
	private JLabel jlabel2;
	private JLabel jlabel3;
	
	public Demo(){
		init();
	}
	
	public void init(){
		isShowing = false ;
		jlabel = new JLabel();
		jlabel.setForeground(Color.red);
		//jlabel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Tweaking Tooltips", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), java.awt.Color.red));
			
		jlabel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "good", TitledBorder.DEFAULT_POSITION	, TitledBorder.DEFAULT_POSITION	, new Font("utg-8", 0, 23), Color.green	));
		jlabel.setText("<html>\n<body>\n<center>\n<h2>\nPACIFIC OCEAN\n</h2>\n<p>\n<strong>\n<em>\nIt has been snowing for thousands of years!\n</em>\n</strong>\n</p>\n</center>\n</body>\n</html>");
	    
		jbutton1 = new JButton();
	    jbutton1.setText("begin....");
	    jbutton1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(isShowing == true){
					jbutton1.setText("end!!!");
				}else{
					jbutton1.setText("begin...");
				}
				
			}
		});
	    jbutton2 = new JButton();
	    jbutton2.setText("stop");
	    jbutton2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);
				}
		});
	    
		jslider = new JSlider();
		jslider.setForeground(Color.white);
		jslider.setMaximum(10);
		jslider.setMinimum(1);
		jslider.setValue(5);
		//jslider.setVisible(true);
		//   //如果为 true,则该组件绘制其边界内的所有像素。
		jslider.setOpaque(false);
		
		jlabel2 = new JLabel("less");
		jlabel3 = new JLabel("more");
		
		
		jpanel = new JPanelWithPhoto(new ImageIcon(this.getClass().getResource("pacific.jpg")));
		//jpanel.setIcon(new ImageIcon(this.getClass().getResource("pacific.jpg")));
		javax.swing.GroupLayout wallpaperPanel1Layout = new javax.swing.GroupLayout(jpanel);
		jpanel.setLayout(wallpaperPanel1Layout);
        wallpaperPanel1Layout.setHorizontalGroup(
            wallpaperPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(wallpaperPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(wallpaperPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlabel, javax.swing.GroupLayout.DEFAULT_SIZE, 437, Short.MAX_VALUE)
                    .addGroup(wallpaperPanel1Layout.createSequentialGroup()
                        .addComponent(jlabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jslider, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jlabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 65, Short.MAX_VALUE)
                        .addComponent(jbutton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbutton2)))
                .addContainerGap())
        );
        wallpaperPanel1Layout.setVerticalGroup(
            wallpaperPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, wallpaperPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jlabel, javax.swing.GroupLayout.DEFAULT_SIZE, 349, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(wallpaperPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(wallpaperPanel1Layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addGroup(wallpaperPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jbutton2)
                            .addComponent(jbutton1)))
                    .addGroup(wallpaperPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(wallpaperPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jslider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jlabel2)
                            .addComponent(jlabel3))))
                .addContainerGap())
        );

		 javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
	        getContentPane().setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addComponent(jpanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
	        );
	        layout.setVerticalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addComponent(jpanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
	        );
	        pack();
		
		
	}
	
	public static void main(String args[]){
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		EventQueue.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				new Demo().setVisible(true);
			}
		});
		
	}

} 
package com.swing.panel;

import java.awt.Graphics;

import javax.swing.Icon;
import javax.swing.JPanel;

public class JPanelWithPhoto extends JPanel {
	private Icon icon ;
	
	

	public Icon getIcon() {
		return icon;
	}



	public void setIcon(Icon icon) {
		this.icon = icon;
	}

	public JPanelWithPhoto(Icon icon){
		this.icon = icon ;
	}

	@Override
	protected void paintComponent(Graphics g) {
		 if(icon!=null)
	            icon.paintIcon(this, g, 0, 0);
		
	}
	

}

package com.swing.panel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

public class Demo extends JFrame {
	
	private boolean isShowing;
	private JLabel jlabel;
	private JPanelWithPhoto jpanel;
	private JButton jbutton1;
	private JButton jbutton2;
	private JSlider jslider;
	private JLabel jlabel2;
	private JLabel jlabel3;
	
	public Demo(){
		init();
	}
	
	public void init(){
		isShowing = false ;
		jlabel = new JLabel();
		jlabel.setForeground(Color.red);
		//jlabel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Tweaking Tooltips", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), java.awt.Color.red));
			
		jlabel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "good", TitledBorder.DEFAULT_POSITION	, TitledBorder.DEFAULT_POSITION	, new Font("utg-8", 0, 23), Color.green	));
		jlabel.setText("<html>\n<body>\n<center>\n<h2>\nPACIFIC OCEAN\n</h2>\n<p>\n<strong>\n<em>\nIt has been snowing for thousands of years!\n</em>\n</strong>\n</p>\n</center>\n</body>\n</html>");
	    
		jbutton1 = new JButton();
	    jbutton1.setText("begin....");
	    jbutton1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(isShowing == true){
					jbutton1.setText("end!!!");
				}else{
					jbutton1.setText("begin...");
				}
				
			}
		});
	    jbutton2 = new JButton();
	    jbutton2.setText("stop");
	    jbutton2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);
				}
		});
	    
		jslider = new JSlider();
		jslider.setForeground(Color.white);
		jslider.setMaximum(10);
		jslider.setMinimum(1);
		jslider.setValue(5);
		//jslider.setVisible(true);
		//   //如果为 true,则该组件绘制其边界内的所有像素。
		jslider.setOpaque(false);
		
		jlabel2 = new JLabel("less");
		jlabel3 = new JLabel("more");
		
		
		jpanel = new JPanelWithPhoto(new ImageIcon(this.getClass().getResource("pacific.jpg")));
		//jpanel.setIcon(new ImageIcon(this.getClass().getResource("pacific.jpg")));
		javax.swing.GroupLayout wallpaperPanel1Layout = new javax.swing.GroupLayout(jpanel);
		jpanel.setLayout(wallpaperPanel1Layout);
        wallpaperPanel1Layout.setHorizontalGroup(
            wallpaperPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(wallpaperPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(wallpaperPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlabel, javax.swing.GroupLayout.DEFAULT_SIZE, 437, Short.MAX_VALUE)
                    .addGroup(wallpaperPanel1Layout.createSequentialGroup()
                        .addComponent(jlabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jslider, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jlabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 65, Short.MAX_VALUE)
                        .addComponent(jbutton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbutton2)))
                .addContainerGap())
        );
        wallpaperPanel1Layout.setVerticalGroup(
            wallpaperPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, wallpaperPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jlabel, javax.swing.GroupLayout.DEFAULT_SIZE, 349, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(wallpaperPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(wallpaperPanel1Layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addGroup(wallpaperPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jbutton2)
                            .addComponent(jbutton1)))
                    .addGroup(wallpaperPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(wallpaperPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jslider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jlabel2)
                            .addComponent(jlabel3))))
                .addContainerGap())
        );

		 javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
	        getContentPane().setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addComponent(jpanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
	        );
	        layout.setVerticalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addComponent(jpanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
	        );
	        pack();
		
		
	}
	
	public static void main(String args[]){
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		EventQueue.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				new Demo().setVisible(true);
			}
		});
		
	}

} 
package com.swing.panel;

import java.awt.Graphics;

import javax.swing.Icon;
import javax.swing.JPanel;

public class JPanelWithPhoto extends JPanel {
	private Icon icon ;
	
	

	public Icon getIcon() {
		return icon;
	}



	public void setIcon(Icon icon) {
		this.icon = icon;
	}

	public JPanelWithPhoto(Icon icon){
		this.icon = icon ;
	}

	@Override
	protected void paintComponent(Graphics g) {
		 if(icon!=null)
	            icon.paintIcon(this, g, 0, 0);
		
	}
	

}

转载于:https://my.oschina.net/winHerson/blog/40965

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值