import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.imageio.ImageIO;
public class LoginExample {
public static void main(final String... args) {
EventQueue.invokeLater(new Runnable(){
@Override public void run(){
try {
final XPanel container = new XPanel(ImageIO.read(LoginExample.class.getResource("/images/background.jpg")));
final JPanel panel = new JPanel();
final GroupLayout layout = new GroupLayout(panel);
container.setLayout(new BorderLayout());
panel.setLayout(layout);
panel.setOpaque(false);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
final JLabel name = new JLabel("
用户名");
final JLabel pass = new JLabel("密码");
final JTextField name_field = new JTextField(12);
final JPasswordField pass_field = new JPasswordField(12);
name_field.setOpaque(false);
pass_field.setOpaque(false);
GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING).
addComponent(name).addComponent(pass));
hGroup.addGroup(layout.createParallelGroup().
addComponent(name_field).addComponent(pass_field));
layout.setHorizontalGroup(hGroup);
GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
vGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).
addComponent(name).addComponent(name_field));
vGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).
addComponent(pass).addComponent(pass_field));
layout.setVerticalGroup(vGroup);
final JFrame frame = new JFrame("Login");
container.add(new JLabel("**欢迎您登录**", JLabel.CENTER), BorderLayout.PAGE_START);
container.add(panel);
frame.setContentPane(container);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
} catch (Exception e) {
}
}
});
}
public static class XPanel extends JPanel {
private BufferedImage background;
public XPanel(BufferedImage background) {
this.background = background;
}
@Override public void paintComponent(Graphics g) {
BufferedImage bufimg = new BufferedImage(getWidth(),getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = bufimg.createGraphics();
// restore the foreground color in case the superclass needs it
g2.setColor(g.getColor());
super.paintComponent(g2);
// do an alpha composite
Graphics2D gx = (Graphics2D) g;
gx.drawImage(background,0,0,null);
gx.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.8f));
gx.drawImage(bufimg,0,0,null);
}
}
}