package Task1;
//首先要調用需要的包
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class myFrame extends Frame //窗體類,繼承Frame
{
CardLayout cl=new CardLayout(); //定義卡片布局
public void initial() //創建一個窗體
{
this.setBounds(500, 300, 300, 200); //窗體的大小和坐標
this.setTitle("圖片自動瀏覽"); //窗體主題
this.setLayout(cl); //窗體採用的布局方式,上面已經定義了卡片布局,所以這里直接調用即可
this.setVisible(true); //顯示窗體
JButton[] jbtn=new JButton[4]; //創建添加圖片的數組,有幾張圖,數據成員就有幾個
for(int i=0;i<4;i++) //我這里是用循環來添加圖片,所以上面要創建數組,當然分開一個一個的添加圖片也是可以的
{
jbtn[i]=new JButton();
jbtn[i].setIcon(new ImageIcon("c://"+i+".jpg")); //這是所要添加圖片的地址
this.add(jbtn[i],i+""); //顯示
}
while(true) //關鍵的時候到了,如何讓圖片滾動循環,我這里是用了while循環來寫的
{
cl.next(this); //圖片的切換,下一張
try{Thread.sleep(3000);} //切換的間隔時間為3秒,也就是3000毫秒
catch(Exception e){}
}
}
public myFrame() //這是固定方法,窗體程序必須寫,少了它整個程序完蛋,所以不多做解釋
{
initial();
}
}
public class Test { //Test類,我這里是專門寫主函數的
public static void main(String[] args) //主函數main
{
myFrame mf=new myFrame(); //實例化調用窗體類myFrame
}
}
ok,完成。程序復制過去以後可能會報錯,並不是我寫錯了,我的有些包名或者類名還有大括弧可能與你的不符合,你需要把細節方面的東西再做調試一下,然後在運行。
望採納😘謝謝!
② 關於CSS怎麼設置滾動背景圖
我建議你用css3來寫,就是寫一個小動畫
具體怎麼動,你改background-position的值就行了。
<style>
@keyframesanimatedBird{
0%{background-position:20px20px;}
50%{background-position:30px30px;}
100%{background-position:300px-90px;}
}
.animate-area{
background:url(....);
animation:animatedBird4slinearinfinite;
}
</style>
<divclass="animate-area"></div>
③ JAVA中怎麼實現背景圖片滾動
importjavax.swing.ImageIcon;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
{
publicMainJFrame(){
//設置標題
super("JFram設置背景圖片(Cannel_2020)");
//設置大小
setSize(500,400);
//設置位置
setLocation(200,50);
//背景圖片的路徑。(相對路徑或者絕對路徑。本例圖片放於"java項目名"的文件下)
Stringpath="background.jpg";
//背景圖片
ImageIconbackground=newImageIcon(path);
//把背景圖片顯示在一個標簽裡面
JLabellabel=newJLabel(background);
//把標簽的大小位置設置為圖片剛好填充整個面板
label.setBounds(0,0,this.getWidth(),this.getHeight());
//把內容窗格轉化為JPanel,否則不能用方法setOpaque()來使內容窗格透明
JPanelimagePanel=(JPanel)this.getContentPane();
imagePanel.setOpaque(false);
//把背景圖片添加到分層窗格的最底層作為背景
this.getLayeredPane().add(label,newInteger(Integer.MIN_VALUE));
//設置可見
setVisible(true);
//點關閉按鈕時退出
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
publicstaticvoidmain(String[]args){
newMainJFrame();
}
}
運行如下
④ html怎麼設置滾動圖片
方案一:直接使用HTML的滾動標簽 marquee ,把圖片放入滾滾標簽內部,代碼如下:
<marquee>
<img src='1.jpg'>
<img src='2.jpg'>
<img src='3.jpg'>
<img src='4.jpg'>
</marquee>
方案二:使用第三方插件,比如swiper.js,
插件
⑤ js中在canvas中加入一張背景怎麼讓背景滾動
<html>
<head>
<metacharset="utf-8"/>
<title>LScroll5.html</title>
<scripttype="text/javascript">
varLScroll={
img:null,
sc:null,
scx:null,
at:0,
flag:true,
loadImg:function(srcs,callback){
varmg=newImage();
mg.src=srcs;
mg.onload=function(){
callback(this);
};//callbackfunction
returnLScroll.img=mg;
},
init:function(srcs){
if(!.body)
document.createElement('body');
if(!LScroll.sc){
varsc=document.createElement('canvas');
LScroll.scx=sc.getContext('2d');
varcallback=function(imgs){//afteronloadimagecanbedraw
sc.width=imgs.width/4;
sc.height=imgs.height;//notstyle.
sc.style.backgroundColor='black';
sc.style.border='solid1pxwhite';
document.body.style.backgroundColor='black';
document.body.style.textAlign='center';
document.body.appendChild(LScroll.sc=sc);
LScroll.draw(LScroll.sc,LScroll.scx);
};
LScroll.loadImg(srcs,callback);
}
},
draw:function(sc,scx){
scx.clearRect(0,0,sc.width,sc.height);
scx.save();
scx.beginPath();
switch(LScroll.flag){
casetrue:
if(-LScroll.at==LScroll.img.width-sc.width)
LScroll.flag=!LScroll.flag;
LScroll.at--;
break;
casefalse:
if(LScroll.at==0)
LScroll.flag=!LScroll.flag;
LScroll.at++;
}
scx.drawImage(LScroll.img,LScroll.at,0);
scx.restore();
setTimeout(function(){
LScroll.draw(sc,scx);
},10);
}
};
window.onload=function(){
LScroll.init('./Park.jpg');
};
</script>
</head>
<body>
</body>
</html>
⑥ 編程貓怎麼讓背景橫屏滾動
只要點擊背景,給背景添加重復移動的指令即可實現。