java为面板添加背景图片

  • 格式:txt
  • 大小:3.73 KB
  • 文档页数:2

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ChangePanelShow extends JFrame{
private JPanel p1 ;
private JPanel p2 ;
private JPanel p3 ;
private JPanel p4 ;
private JPanel p5 ;
private JTextField jtx;
private JButton jbt1 = new JButton("Next");;
private ImageIcon im;
private int count = 0;
private Timer timer;
public ChangePanelShow(){
p1 = new PanelDemo();
p1.setLayout(null);
im = new ImageIcon("C:\\Users\\XFL\\Pictures\\MM\\mm10.jpg");
p1.add(jbt1);
jbt1.setBounds(280, 430,60, 30);
this.add(p1);
timer=new Timer(7000,new TimerListener());
timer.start();
jbt1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
count++;
changepanel();
}

});
}
public void changepanel(){
if(count==0){
jtx = new JTextField("NB501");
p1 = new PanelDemo();
p1.setLayout(null);
im = new ImageIcon("C:\\Users\\XFL\\Pictures\\MM\\mm10.jpg");
p1.add(jbt1);
jbt1.setBounds(getWidth()-70, getHeight()-70,60, 30);
p1.add(jtx);
jtx.setBounds(0, getHeight()-70,60, 30);
this.add(p1);
this.getContentPane().removeAll();
this.getContentPane().add(p1,BorderLayout.CENTER);
this.repaint();
this.validate();
}
else if(count==1){
p2 = new PanelDemo();
jtx = new JTextField("JXUFE");
p2.setLayout(null);
im = new ImageIcon("C:\\Users\\XFL\\Pictures\\MM\\mm15.jpg");
p2.add(jbt1);
jbt1.setBounds(getWidth()-70, getHeight()-70,60, 30);
p2.add(jtx);
jtx.setBounds(0, getHeight()-70,80, 30);
this.add(p2);
this.getContentPane().removeAll();
this.getContentPane().add(p2,BorderLayout.CENTER);
this.repaint();
this.validate();
}
else if(count==2){
p3 = new PanelDemo();
jtx = new JTextField("WELCOME");
p3.setLayout(null);
im = new ImageIcon("C:\\Users\\XFL\\Pictures\\MM\\mm7.jpg");
p3.add(jbt1);
jbt1.setBounds(getWidth()-70, getHeight()-70,60, 30);
p3.add(jtx);
jtx.setBounds(0, getHeight()-70,80, 30);
this.add(p3);
this.getContentPane().removeAll();
this.getContentPane().add(p3,BorderLayout.CENTER);
this.repaint();
this.validate();
}
else if(count==3){
p4 = new PanelDemo();
p4.setLayout(null);
im = new ImageIcon("C:\\Users\\XFL\\Pictures\\MM\\mm17.jpg");
p4.add(jbt1);
jbt1.setBounds(getWidth()-70, getHeight()-70,60, 30);
this.add(p4);
this.getContentPane().removeAll();
this.getContentPane().add(p4,BorderLayout.CENTER);
this.repaint();
this.validate();
}
else if(count==4){
p5 = new PanelDemo();
p5.setLayout(null);
im = new ImageIcon("C:\\Users\\XFL\\Pictures\\MM\\mm9.jpg");
p5.add(jbt1);
jbt1.setBounds(getWidth()-70, getHeight()-70,60, 30);
this.add(p5);
this.getContentPane().removeAll();
this.getContentPane().add(p5,BorderLayout.CENTER);
this.repaint();
this.validate();
count=-1;
}
}
pub

lic static void main(String[] args) {
ChangePanelShow frame = new ChangePanelShow();
frame.setSize(350,500);
frame.setTitle("MM");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
Image image=new ImageIcon("E:\\java答案\\image\\rbs.gif").getImage();
frame.setIconImage(image);
}
class PanelDemo extends JPanel{
public void paintComponent(Graphics g) {
super.paintComponent(g);
im.setImage(im.getImage().getScaledInstance(getWidth(),getHeight(),Image.SCALE_DEFAULT));
g.drawImage(im.getImage(), 0,0,this);
this.setToolTipText("每隔7秒播放一张图片");
}
}
class TimerListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
count++;
changepanel();
}

}
}

下载文档原格式

  / 2
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

相关主题