用JAVA在窗体中显示背景。但显示不出来,请指教
packagecom.css;importjava.awt.EventQueue;importjavax.swing.JLabel;importjavax.swing.J...
package com.css;
import java.awt.EventQueue;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
public class Login extends JFrame {
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Login frame = new Login();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public Login() {
super();
getContentPane().setLayout(null);
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
ImageIcon backIcon=new ImageIcon(getClass().getResource("background.jpg"));
final JLabel backLabel=new JLabel(backIcon); // 创建背景标签
backLabel.setSize(backIcon.getIconWidth(), backIcon.getIconHeight());
backLabel.setLocation(0, 0);
// 设置标签定位
getContentPane().add(backLabel); // 添加标签到窗体
setResizable(false);
}
} 展开
import java.awt.EventQueue;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
public class Login extends JFrame {
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Login frame = new Login();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public Login() {
super();
getContentPane().setLayout(null);
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
ImageIcon backIcon=new ImageIcon(getClass().getResource("background.jpg"));
final JLabel backLabel=new JLabel(backIcon); // 创建背景标签
backLabel.setSize(backIcon.getIconWidth(), backIcon.getIconHeight());
backLabel.setLocation(0, 0);
// 设置标签定位
getContentPane().add(backLabel); // 添加标签到窗体
setResizable(false);
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询