当前位置: 代码迷 >> Java相关 >> 请教一下,如何实现启动画面的延迟
  详细解决方案

请教一下,如何实现启动画面的延迟

热度:134   发布时间:2007-07-23 23:05:44.0
请教一下,如何实现启动画面的延迟
大家好,我最近在做毕业设计,正在做登陆方法,想实现登陆画面的效果,但是调用方法后,画面一下子就消失了,想请教一下,如何实现画面延迟的时间?敬请不吝赐教,谢谢了。
这是调用显示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.BorderLayout;
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;
public class UserLoginFrame extends JFrame {
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);
}
public void btnOk_actionPerformed(ActionEvent e) {
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);
}
}
public void btnCancel_actionPerformed(ActionEvent e) {
System.exit(0);
}
}

class UserLoginFrame_btnCancel_actionAdapter implements ActionListener {
private UserLoginFrame adaptee;
UserLoginFrame_btnCancel_actionAdapter(UserLoginFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnCancel_actionPerformed(e);
}
}

class UserLoginFrame_btnOk_actionAdapter implements ActionListener {
private UserLoginFrame adaptee;
UserLoginFrame_btnOk_actionAdapter(UserLoginFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnOk_actionPerformed(e);
}
}

搜索更多相关的解决方案: 画面  import  public  延迟  java  

----------------解决方案--------------------------------------------------------
我看见调用logo方法的时候
Logo lg =new Logo("Logo.jpg");
lg.run();
lg有wait();方法
请教一下,是不是这个方法是延迟用的,但是用了,老是报错

----------------解决方案--------------------------------------------------------
你可以在run方法中使用sleep啊


----------------解决方案--------------------------------------------------------
顺便问下,怎么这个时候做毕业设计呢?
----------------解决方案--------------------------------------------------------

谢谢楼上的大侠,基本上解决了,我是在
package use_login;

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);
//显示该窗口
try
{
Thread.currentThread().sleep(5000);
}
catch(Exception e)
{}

}
public void setNotVisible()
{
setVisible(false);
//不显示该窗口
}
}
中run方法中加入了
try
{
Thread.currentThread().sleep(5000);
}
catch(Exception e)
{}

语句,控制登陆画面的时间。请问这样可以吗?

[此贴子已经被作者于2007-7-24 9:22:19编辑过]


----------------解决方案--------------------------------------------------------
另外,我学的是java的学习班,一期刚学完,不是正规的大学,所以。。。这个时候做毕业设计,其实确切的说,应该是期终设计,hoho,再次感谢拉
----------------解决方案--------------------------------------------------------
QUOTE:
以下是引用antonio_jiao在2007-7-24 9:20:36的发言:
另外,我学的是java的学习班,一期刚学完,不是正规的大学,所以。。。这个时候做毕业设计,其实确切的说,应该是期终设计,hoho,再次感谢拉

原来是这样。
----------------解决方案--------------------------------------------------------

  相关解决方案