package test.array;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Login implements ActionListener {
public static void main(String args[]) {
new Login().face();
}
public void face() {
JFrame jf;
JLabel j1, j2;
JTextField jtf;
JPasswordField jd;
JButton jb1, jb2;
//JPanel p1, p2, p3;
jf = new JFrame("登陆窗口");
j1 = new JLabel("用户名");
j2 = new JLabel("密 码");
jtf = new JTextField(15);
jd = new JPasswordField(15);
jb1 = new JButton("确定");
jb2 = new JButton("取消");
// p1 = new JPanel();
// p2 = new JPanel();
// p3 = new JPanel();
JPanel p = new JPanel() {
private static final long serialVersionUID = 1L;
@Override
protected void paintComponent(Graphics g) {
try {
BufferedImage img = ImageIO.read(new File(this.getClass().getResource("1.jpg").getPath()));
g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), null);
} catch (IOException e) {
e.printStackTrace();
}
}
};
p.setLayout(null);
//p.add(p1);
//p.add(p2);
//p.add(p3);
j1.setBounds(50, 10, 60, 20);
p.add(j1);
jtf.setBounds(120, 10, 120, 20);
p.add(jtf);
j2.setBounds(50, 40, 60, 20);
p.add(j2);
jd.setBounds(120, 40, 120, 20);
p.add(jd);
jb1.setBounds(80, 70, 60, 20);
p.add(jb1);
jb2.setBounds(160, 70, 60, 20);
p.add(jb2);
jf.add(p);
jb1.addActionListener(this);
jb2.addActionListener(this);
jf.setBounds(300, 200, 280, 160);
jf.setResizable(false);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand();
if (s.equals("确定")) {
} else {
System.exit(0);
}
}
}
使用SwingX这个库。
JXPanel panel = new JXPanel();
BufferedImage bi = ...;
panel.setBackgroundPainter(new ImagePainter(bi));
frame.setContentPane(panel);