展开迷你的钢铁侠
写一个dmt类继承自JPanel,在paintComponent方法中用g.drawImage绘制32313133353236313431303231363533e4b893e5b19e31333337393539Image背景图片就挡不着第一张卡片上的四个按钮了。
完整的程序如下:(注意把背景图片的路径换成你的背景图片路径)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;
JButton jb1=new JButton("按钮1");
JButton jb2=new JButton("按钮2");
JButton jb3=new JButton("按钮3");
JButton jb4=new JButton("按钮4");
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);
p[0]=new dmt();
jp2.add(p[0],String.valueOf(0));
for(int i = 1; i
p[i] = new JPanel();
jp2.add(p[i],String.valueOf(i));
}
JButton b疯狂的纸鹤 = new JButton[4];
for(int i = 1; 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 dmt 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 dmt() {
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);
}
}
}
}