如何为面板添加背景图片,求大神指导,代码如下:(自己添加的时候总是把组件遮挡了或者添加不上去)
万分感谢!!
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class Main extends JFrame implements ActionListener {
/**
* @param args
*/
JLabel l1 = new JLabel("武汉纺织大学学生信息查询");
JLabel l2 = new JLabel("用户登陆");
JLabel l3 = new JLabel("用户名 :");
JLabel l4 = new JLabel("密 码 :");
JTextField t1 = new JTextField();
JPasswordField t2 = new JPasswordField();
JButton button1 = new JButton("登陆");
JButton button2 = new JButton("重置");
static JPanel p1 = new JPanel(new FlowLayout(FlowLayout.CENTER, 2, 2));
Font f1 = new Font("宋体", Font.BOLD, 20);
Font f2 = new Font("宋体", Font.BOLD, 30);
Font f3 = new Font("宋体", Font.BOLD, 15);
public static String currentSID = "";
public Main() {
GUI();
}
public void GUI() { // 面板设计
p1.add(l1);
p1.add(l2);
p1.add(l3);
p1.add(l4);// 文字
p1.add(t1);
p1.add(t2);// 文本框
p1.add(button1);
p1.add(button2);// 按钮
add(p1);
p1.setBackground(Color.black);
p1.setLayout(null);
l1.setBounds(100, 50, 400, 30);
l1.setFont(f2);
l1.setForeground(Color.cyan);
l2.setBounds(250, 150, 130, 40);
l2.setFont(f1);
l2.setForeground(Color.cyan);
l3.setBounds(170, 230, 100, 40);
l3.setFont(f1);
l3.setForeground(Color.cyan);
l4.setBounds(170, 280, 100, 40);
l4.setFont(f1);
l4.setForeground(Color.cyan);
t1.setBounds(280, 240, 130, 20);
t1.setFont(f3);
t2.setBounds(280, 290, 130, 20);
t2.setFont(f3);
button1.setBounds(200, 350, 80, 30);
button1.setFont(f1);
button2.setBounds(300, 350, 80, 30);
button2.setFont(f1);
button1.addActionListener(this);
button2.addActionListener(this);
}
public void actionPerformed(ActionEvent e) { // 登陆消息响应
// TODO Auto-generated method stub
Object source = e.getSource();
if (source == button1) {
String user = t1.getText();
String password = t2.getText();
if((user.equals("admin")==true)&&(password.equals("123")==true))
{
setVisible(false);
new panel();
}
else if(login(user,password))
{
setVisible(false);
new vo();
}
else if((user.equals("")==true&&(password.equals("")==true)))
{
JOptionPane.showMessageDialog(null, "请输入用户名和密码!", "提示", 2);
}
else{
JOptionPane.showMessageDialog(null, "输入的用户名或密码错误!请重新输入!", "提示", 2);
t1.setText(null);
t2.setText(null);
}
}
if (source == button2) {
t1.setText(null);
t2.setText(null);
}
}
public boolean login(String name,String password){ // 调用xml文档中的参数登陆
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder domParser = factory.newDocumentBuilder();
Document document = domParser.parse(new File("class.xml"));
try {
NodeList imags = document.getElementsByTagName("student");
int j = imags.getLength();
for (int k = 0; k < j; k++) {
Element elink = (Element) imags.item(k);
String number = elink.getElementsByTagName("number").item(0)
.getTextContent();
String classes = elink.getElementsByTagName("classes")
.item(0).getTextContent();
if(name.equals(number) && password.equals(classes)){
currentSID = number;
return true;
}
}
} catch (Exception e) {
System.out.println(e);
System.out.println("错误");
}
} catch (Exception e) {
System.out.println(e);
System.out.println("错误");
}
return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Main f = new Main();
f.setTitle("武汉纺织大学");
f.add(p1);
f.setSize(600, 600);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
try {
// 设置界面的外观,为系统外观
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(f);
} catch (Exception e) {
e.printStackTrace();
}
}
}