本文目錄一覽:
java背景圖片不顯示
/**加入背景圖片的話**/
ImageIcon im = new ImageIcon(“./image/denglujiemian.jpg”);
JLabel l1 = new JLabel(im);
/**這裡使用的是空布局,就是你的窗口的大小*/
l1.setBounds(0,0,300,200);
deskpane.add(l1);
註:最好將你的背景加在其他圖層最後,即放在setVisible前。
補充:注釋:ImageIcon im = new ImageIcon(“./image/denglujiemian.jpg”);
這裡的image是需要你自己創的一個包,裡面放你需要的圖片,記住書寫格式 “./image/**.jpg”
效果截圖:
希望能解決您的問題。
JAVA界面加了背景圖片但是顯示不出來,代碼在下面
你的背景圖片的Java程序,我幫你改完了,你看看吧(只是改了JM();構造函數中的內容,去掉了setOpaque函數,其它的地方沒動)
import javax.swing.*;
public class JM extends JFrame {
JLabel yh = new JLabel(“用戶名”);
JLabel mm = new JLabel(“密碼”);
JTextField yh1 = new JTextField(15);
JPasswordField mm1 =new JPasswordField(15);
JRadioButton jmm = new JRadioButton(“記住密碼”);
JButton dl =new JButton(“登陸”);
JButton qx = new JButton(“取消”);
JButton zc = new JButton(“註冊”);
public JM (){
this.setTitle(“登陸界面”);
this.setSize(300, 270);
JPanel c = (JPanel)this.getContentPane();
c.setLayout(null);
//Container c = this.getContentPane();
yh.setBounds(20, 0, 100, 100);
yh1.setBounds(70,37, 200, 27);
mm.setBounds(20, 30, 100, 100);
mm1.setBounds(70, 67, 200,27);
jmm.setBounds(70, 95, 80, 30);
dl.setBounds(70, 135, 60, 35);
qx.setBounds(135, 135, 60, 35);
zc.setBounds(200, 135, 60, 35);
c.add(yh);
c.add(yh1);
c.add(mm);
c.add(mm1);
c.add(jmm);
c.add(dl);
c.add(qx);
c.add(zc);
JLabel bg = new JLabel(new ImageIcon(“006Cx3u5ly1fbz7xwdx1sj30i40dydg4.jpg”));
bg.setBounds(0, 0, 300, 270);
c.add(bg);
c.setOpaque(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
//private void setOpaque(boolean b) {
// TODO Auto-generated method stub
//}
public static void main(String[] args) {
// TODO Auto-generated method stub
new JM();
}
}
運行結果
java 登錄界面的背景怎麼顯示不出來
java登陸界面的背景顯示不出來,很有可能是因為你的背景圖片資源沒有,或者路徑不對,你可以嘗試用絕對路徑試試看.
java為什麼添加圖片後界面沒顯示?
JPanel jPanelToolBar = new JPanel(new BorderLayout()){
public void paintComponent(Graphics g) {
super.paintComponent(g);
Image image = paintPicture(“1_03”);
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();
g.drawImage(image, 0, 0, getSize().width, getSize().height, this);
}
}
};
private Image paintPicture(String picName){
String path = pathimage+picName+“.png”;//這是圖片路徑
Image image = null;
try {
image = ImageIO.read(new File(path));
} catch (IOException e) {
log.error(e);
}
return image;
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/156893.html