導航:首頁 > 編程語言 > javajpanel圖片

javajpanel圖片

發布時間:2023-09-28 12:49:54

java中jpanel的背景圖片問題

寫一個ImageJPanel類繼承自JPanel,在paintComponent方法中用g.drawImage繪制Image背景圖片就擋不著第一張卡片上的四個按鈕了。

完整的程序如下:(注意把背景圖片的路徑換成你的背景圖片路徑)

importjava.awt.CardLayout;
importjava.awt.Color;
importjava.awt.Graphics;
importjava.awt.GridLayout;
importjava.awt.Image;
importjava.awt.Toolkit;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjavax.swing.JSplitPane;
{
=1L;
JPaneljp1=newJPanel();
JPaneltmpjp1=newJPanel();
JPaneljp2=newJPanel();
JPanelcard_jp1=newJPanel();
JPanelcard_jp2=newJPanel();
JPanelcard_jp3=newJPanel();
JPanelcard_jp4=newJPanel();
JButtonjb1=newJButton("按鈕1");
JButtonjb2=newJButton("按鈕2");
JButtonjb3=newJButton("按鈕3");
JButtonjb4=newJButton("按鈕4");
JSplitPanejsp=newJSplitPane(JSplitPane.HORIZONTAL_SPLIT,jp1,jp2);
CardLayoutcl=newCardLayout();
intj=1;
D(){
tmpjp1.setLayout(newGridLayout(4,1,5,5));
tmpjp1.add(jb1);tmpjp1.add(jb2);tmpjp1.add(jb3);tmpjp1.add(jb4);
jp1.add(tmpjp1);
jp2.setLayout(cl);
JPanelp[]=newJPanel[4];
p[0]=newImageJPanel();
jp2.add(p[0],String.valueOf(0));
for(inti=1;i<4;i++){
p[i]=newJPanel();
jp2.add(p[i],String.valueOf(i));
}
JButtonb[]=newJButton[4];
for(inti=1;i<4;i++){
inta=i+1;
b[i]=newJButton("第"+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);
}

publicstaticvoidmain(String[]args){
newD();
}
@Override
publicvoidactionPerformed(ActionEvente){
cl.show(jp2,String.valueOf(j++));
if(j==4)j=0;
}
classImageJPanelextendsJPanel{
privateImageimage;
JButtoncard_jb1=newJButton("按鈕1");
JButtoncard_jb2=newJButton("按鈕2");
JButtoncard_jb3=newJButton("按鈕3");
JButtonjb=newJButton("第1頁,下一頁");
publicImageJPanel(){
super();
setOpaque(true);
add(card_jb1);
add(card_jb2);
add(card_jb3);
jb.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
cl.show(jp2,String.valueOf(j++));
if(j==4)j=0;
}
});
add(jb);
image=Toolkit.getDefaultToolkit().getImage("C:/tmp/F/3.jpg");//這里換成你的背景圖片地址
}
publicvoidpaintComponent(Graphicsg){
super.paintComponent(g);
setBackground(Color.WHITE);
if(image!=null){
intheight=image.getHeight(this);
intwidth=image.getWidth(this);
if(height!=-1&&height>getHeight())
height=getHeight();
if(width!=-1&&width>getWidth())
width=getWidth();
intx=(int)(((double)(getWidth()-width))/2.0);
inty=(int)(((double)(getHeight()-height))/2.0);
g.drawImage(image,x,y,width,height,this);
}
}
}
}

② java怎麼在JFrame中顯示動態圖片

試了一下,從網上找了個GIF

可以用啊

--------------------------------------------------------------------------------------------

importjava.awt.Graphics;

importjavax.swing.ImageIcon;

importjavax.swing.JFrame;

importjavax.swing.JPanel;

{

publicImageApp(){

setDefaultCloseOperation(EXIT_ON_CLOSE);

setLocationRelativeTo(null);

setSize(400,300);

setResizable(false);

getContentPane().setLayout(null);

JPanelpanel=newImagePanel();

panel.setBounds(0,0,400,300);

getContentPane().add(panel);

setVisible(true);

}

publicstaticvoidmain(String[]args){

newImageApp();

}

classImagePanelextendsJPanel{

publicvoidpaint(Graphicsg){

super.paint(g);

//ImageIconicon=newImageIcon("D:\1.jpg");

ImageIconicon=newImageIcon("D:\14405937jqhjsppeninjf9.gif");

g.drawImage(icon.getImage(),0,0,400,300,this);

}

}

}

③ 怎麼在Java里添加背景圖片

可以新建個面板,在面板里放入帶圖片的JLabel,填滿面板即可。
JPanel jp = new JPanel(); //新建面板
jp.setLayout(new FlowLayout()); //設置面板布局
ImageIcon ii=new ImageIcon(getClass().getResource("/Picture/i.jpg"));
JLabel uppicture=new JLabel(ii); //往面板里加入JLabel
this.setVisible(true);

閱讀全文

與javajpanel圖片相關的資料

熱點內容
程序員怎麼跟男朋友說我愛你 瀏覽:309
單片機頻率變化 瀏覽:428
哪個app可以看賭神 瀏覽:466
rstudiopython 瀏覽:127
團隊如何開發伺服器 瀏覽:440
php選擇資料庫的函數 瀏覽:772
dhcp伺服器新增地址 瀏覽:930
程序員跑三個月外賣 瀏覽:941
linux配置tomcat的jdk路徑 瀏覽:363
液體壓縮公式 瀏覽:777
php開發後台管理系統 瀏覽:360
python二分查找遞歸 瀏覽:447
微信如何發視頻不壓縮 瀏覽:902
河北2021美術高考綜合分演算法 瀏覽:606
如何為電腦文件夾加密 瀏覽:835
電腦自啟動應用命令 瀏覽:690
php判斷一個文件是否存在 瀏覽:829
php導出xml文件 瀏覽:904
7個文件夾解壓 瀏覽:383
python實現機器碼 瀏覽:356