swing jlabel 加背景图片 做出frame添加背景图片效果

 //原理很简单,大家知道早Jlabel很容易添加背景图片,这个例子就是把一个JLabel放到一个Frame里,把它铺满,再加上图片,然后所有的组件都加到这个JLabel上,就回出现给Frame加上了背景图片的效果。(因为Frame没有添加背景图片的函数,所以没法加,只能靠其他办法)

package model;

import java.awt.*;

import javax.swing.*;
import java.awt.event.*;

public class TestImageWindow extends JFrame {
 JLabel topLabel;
 JLabel userNameLabel, passwordLabel;
 JTextField userNameTextField;
 JPasswordField passwordField;
 JButton exitButton, loginButton;

 public TestImageWindow() {
  super("欢迎登录");
  topLabel = new JLabel();
//  topLabel.setIcon(new ImageIcon("/im/1.jpg"));
  topLabel.setIcon(new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/im/1.jpg"))));
  add(topLabel);
  topLabel.setLayout(null);

  userNameLabel = new JLabel("用户名:");
  userNameLabel.setForeground(Color.WHITE);
  passwordLabel = new JLabel("密 码:");
  userNameTextField = new JTextField(20);
  passwordField = new JPasswordField(20);
  exitButton = new JButton("退 出");
  loginButton = new JButton("登 录");

  ButtonListener bListener = new ButtonListener();
  // 按钮注册事件监听器
  exitButton.addActionListener(bListener);
  loginButton.addActionListener(bListener);
  userNameTextField.addActionListener(bListener);
  passwordField.addActionListener(bListener);

  MyKeyListener keylis = new MyKeyListener();
  userNameTextField.addKeyListener(keylis);
  passwordField.addKeyListener(keylis);

  userNameLabel.setSize(45, 20);
  userNameLabel.setLocation(40, 280);
  topLabel.add(userNameLabel);

  userNameTextField.setSize(50, 20);
  userNameTextField.setLocation(85, 280);
  topLabel.add(userNameTextField);

  passwordLabel.setSize(40, 20);
  passwordLabel.setLocation(140, 280);
  topLabel.add(passwordLabel);

  passwordField.setSize(50, 20);
  passwordField.setLocation(195, 280);
  topLabel.add(passwordField);

  exitButton.setSize(80, 20);
  exitButton.setLocation(260, 280);
  topLabel.add(exitButton);

  loginButton.setSize(80, 20);
  loginButton.setLocation(350, 280);
  topLabel.add(loginButton);

  setResizable(false);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setLocationRelativeTo(null);
  setSize(500, 350);
  setVisible(true);
 }

 private void checkNamePwd() {
  String userName = userNameTextField.getText().trim();
  String pw = passwordField.getText();
  if (userName.length() == 0) {
   JOptionPane.showMessageDialog(null, "用户名不能为空!");
  } else if (pw.length() == 0) {
   JOptionPane.showMessageDialog(null, "密码不能为空!");
  } else {
   JOptionPane.showMessageDialog(null, "恭喜你成功登录!");
  }
 }

 private void exitWindow() {
  System.exit(0);
 }

 class ButtonListener implements ActionListener {
  public void actionPerformed(ActionEvent e) {
   // 获取事件源
   Object source = e.getSource();
   if (source == exitButton) {
    exitWindow();
   } else if (source == loginButton) {
    checkNamePwd();
   }
  }
 }

 class MyKeyListener extends KeyAdapter {
  public void keyPressed(KeyEvent ke) {
   int code = ke.getKeyCode();
   if (code == 10) {
    checkNamePwd();
   } else if (code == 27) {
    exitWindow();
   }
  }
 }
 public static void main(String[] kk){
  TestImageWindow ww=new TestImageWindow();
  ww.show();
 }
}

你可能感兴趣的:(swing,String,object,null,Class,import)