这是调用显示logo的方法
import javax.swing.*;
import java.lang.Runnable;
import java.awt.*;
public class Logo extends
JWindow implements Runnable
{
String filename;
//Logo图像文件的文件名
public Logo(String name)
{
filename = name;
}
public void run()
{
ImageIcon ig = new ImageIcon(filename);
JButton btn = new JButton(ig);
//将图片给JButton显示
getContentPane().add(btn);
//将显示图片的btn加到JPanel里
Toolkit kit =
Toolkit.getDefaultToolkit();
Dimension screenSize
= kit.getScreenSize();
//获得屏幕的大小
setLocation(screenSize.width/4,
screenSize.height/4);
//将Logo窗口显示在屏幕宽的1/4,高的1/4处
setSize(ig.getIconWidth(),
ig.getIconHeight());
//将Logo窗口大小设成图像的大小
toFront();
//将Logo窗口显示为最前面的窗口
setVisible(true);
//显示该窗口
}
public void setNotVisible()
{
setVisible(false);
//不显示该窗口
}
}
以下是登陆方法
package use_login;
import java.awt.Dimension;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.*;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
JPanel contentPane;
JLabel lblName = new JLabel();
JLabel lblPassword = new JLabel();
JTextArea txtUserName = new JTextArea();
JTextArea txtPassword = new JTextArea();
JButton btnOk = new JButton();
JButton btnCancel = new JButton();
JOptionPane dlgMessage = new JOptionPane();
public UserLoginFrame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(400, 300));
setTitle("登陆");
lblName.setFont(new java.awt.Font("Dialog", Font.PLAIN, 14));
lblName.setText("用户名:");
lblName.setBounds(new Rectangle(65, 51, 89, 30));
lblPassword.setFont(new java.awt.Font("Dialog", Font.PLAIN, 14));
lblPassword.setText("密码:");
lblPassword.setBounds(new Rectangle(65, 123, 104, 30));
txtUserName.setText("");
txtUserName.setBounds(new Rectangle(170, 51, 150, 30));
txtPassword.setText("");
txtPassword.setBounds(new Rectangle(170, 123, 150, 30));
btnOk.setBounds(new Rectangle(75, 202, 100, 40));
btnOk.setText("确定");
btnOk.addActionListener(new UserLoginFrame_btnOk_actionAdapter(this));
btnCancel.setBounds(new Rectangle(201, 202, 105, 40));
btnCancel.setText("取消");
btnCancel.addActionListener(new UserLoginFrame_btnCancel_actionAdapter(this));
dlgMessage.setBounds(new Rectangle(50, 328, 294, 210));
contentPane.setEnabled(true);
contentPane.setToolTipText("");
contentPane.add(lblName);
contentPane.add(txtUserName);
contentPane.add(btnCancel);
contentPane.add(btnOk);
contentPane.add(dlgMessage);
contentPane.add(lblPassword);
contentPane.add(txtPassword);
}
String user;
String password;
user=txtUserName.getText();
password=txtPassword.getText();
if(user.equals("admin")&& password.equals("pass")){
dlgMessage.showMessageDialog(this,"授权用户","经验证用户",JOptionPane.INFORMATION_MESSAGE);
Logo lg =new Logo("Logo.jpg");
lg.run();
lg.setNotVisible();
}
else{
dlgMessage.showMessageDialog(this,"非法用户名或密码","非法用户",JOptionPane.ERROR_MESSAGE);
txtUserName.setText("");
txtPassword.setText("");
txtUserName.setFocusable(true);
}
System.exit(0);
}
}
class UserLoginFrame_btnCancel_actionAdapter implements ActionListener {
private UserLoginFrame adaptee;
UserLoginFrame_btnCancel_actionAdapter(UserLoginFrame adaptee) {
this.adaptee = adaptee;
}
}
}
class UserLoginFrame_btnOk_actionAdapter implements ActionListener {
private UserLoginFrame adaptee;
UserLoginFrame_btnOk_actionAdapter(UserLoginFrame adaptee) {
this.adaptee = adaptee;
}
adaptee.btnOk_actionPerformed(e);
}
}