java中jpanel的背景图片问题 100
JFrame中有个JSPlitPane分割面板,分割面板的左边为树新结构,我这里用按钮代替,游标为一个卡片布局的JPanel,卡片布局的JPanel中有四张做好的卡片Pa...
JFrame中有个JSPlitPane分割面板,分割面板的左边为树新结构,我这里用按钮代替,游标为一个卡片布局的JPanel,卡片布局的JPanel中有四张做好的卡片Panel,现在显示的是第一张,怎么给这个第一张卡片JPanel设置背景图片,有不挡着第一张卡片JPanel中的四个按钮,相当于按钮在背景上面?最高分悬赏,谢谢
上面问题有错别字,改为:
JFrame中有个JSPlitPane分割面板,分割面板的左边为树新结构,我这里用按钮代替,右边为一个卡片布局的JPanel,卡片布局的JPanel中有四张做好的卡片Panel,现在显示的是第一张,怎么给这个第一张卡片JPanel设置背景图片,有不挡着第一张卡片JPanel中的四个按钮,相当于按钮在背景上面?最高分悬赏,谢谢 展开
上面问题有错别字,改为:
JFrame中有个JSPlitPane分割面板,分割面板的左边为树新结构,我这里用按钮代替,右边为一个卡片布局的JPanel,卡片布局的JPanel中有四张做好的卡片Panel,现在显示的是第一张,怎么给这个第一张卡片JPanel设置背景图片,有不挡着第一张卡片JPanel中的四个按钮,相当于按钮在背景上面?最高分悬赏,谢谢 展开
1个回答
展开全部
写一个ImageJPanel类继承自JPanel,在paintComponent方法中用g.drawImage绘制Image背景图片就挡不着第一张卡片上的四个按钮了。
完整的程序如下:(注意把背景图片的路径换成你的背景图片路径)
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
public class D extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
JPanel jp1=new JPanel();
JPanel tmpjp1=new JPanel();
JPanel jp2=new JPanel();
JPanel card_jp1=new JPanel();
JPanel card_jp2=new JPanel();
JPanel card_jp3=new JPanel();
JPanel card_jp4=new JPanel();
JButton jb1=new JButton("按钮1");
JButton jb2=new JButton("按钮2");
JButton jb3=new JButton("按钮3");
JButton jb4=new JButton("按钮4");
JSplitPane jsp=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jp1,jp2);
CardLayout cl=new CardLayout();
int j=1;
D(){
tmpjp1.setLayout(new GridLayout(4,1,5,5));
tmpjp1.add(jb1);tmpjp1.add(jb2);tmpjp1.add(jb3);tmpjp1.add(jb4);
jp1.add(tmpjp1);
jp2.setLayout(cl);
JPanel p[] = new JPanel[4];
p[0]=new ImageJPanel();
jp2.add(p[0],String.valueOf(0));
for(int i = 1; i < 4; i++){
p[i] = new JPanel();
jp2.add(p[i],String.valueOf(i));
}
JButton b[] = new JButton[4];
for(int i = 1; i < 4; i++){
int a = i + 1;
b[i] = new JButton("第" + a + "页,下一页");
b[i].addActionListener(this);
p[i].add(b[i]);
}
jsp.setResizeWeight(0.3);
add(jsp);
setSize(800,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new D();
}
@Override
public void actionPerformed(ActionEvent e) {
cl.show(jp2,String.valueOf(j++));
if(j==4) j=0;
}
class ImageJPanel extends JPanel {
private Image image;
JButton card_jb1=new JButton("按钮1");
JButton card_jb2=new JButton("按钮2");
JButton card_jb3=new JButton("按钮3");
JButton jb = new JButton("第1页,下一页");
public ImageJPanel() {
super();
setOpaque(true);
add(card_jb1);
add(card_jb2);
add(card_jb3);
jb.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
cl.show(jp2,String.valueOf(j++));
if(j==4) j=0;
}
});
add(jb);
image = Toolkit.getDefaultToolkit().getImage( "C:/tmp/F/3.jpg");//这里换成你的背景图片地址
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.WHITE);
if (image != null) {
int height = image.getHeight(this);
int width = image.getWidth(this);
if (height != -1 && height > getHeight())
height = getHeight();
if (width != -1 && width > getWidth())
width = getWidth();
int x = (int) (((double) (getWidth() - width)) / 2.0);
int y = (int) (((double) (getHeight() - height)) / 2.0);
g.drawImage(image, x, y, width, height, this);
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |