| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 379 人关注过本帖
标题:线程问题
只看楼主 加入收藏
菜鸟笨鸟
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2006-9-2
收藏
 问题点数:0 回复次数:0 
线程问题

代码都在这 2个类 具体的效果就是在登陆窗口进行登陆判断 如果成功则在本窗口中显示进度条 等进度条结束后 登陆窗口消失启动主界面
问题是 如何在登陆窗口类里用线程控制进度条的开始 并且等待进度条结束后让登陆窗口消失

代码比较长 注释部分是有问题的部分

//进度条类

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

public class MyProgress extends JPanel implements ActionListener,ChangeListener
{


JProgressBar progressbar;
JLabel label;
Timer timer;

public MyProgress()
{
label = new JLabel("");
progressbar = new JProgressBar();
progressbar.setOrientation(JProgressBar.HORIZONTAL);
progressbar.setMinimum(0);
progressbar.setMaximum(100);
progressbar.setValue(0);
progressbar.setStringPainted(true);
progressbar.addChangeListener(this);
progressbar.setPreferredSize(new Dimension(200,30));
progressbar.setBorderPainted(false);
timer = new Timer(50,this);//计数器
progressbar.setOpaque(false);//设置透明
this.setOpaque(false);//设置透明
label.setForeground(Color.white);
this.add(label);
this.add(progressbar);
this.setVisible(true);
}

//该方法控制进度条的开始

public void control(){
timer.start();
}
//进度条控制
public void actionPerformed(ActionEvent e)
{

if(e.getSource() == timer)
{
int value = progressbar.getValue();

if( value < 100)
{
value++;
progressbar.setValue(value);
}
else
{
timer.stop();
progressbar.setValue(0);
}
}
}

//提示
public void stateChanged(ChangeEvent e1)
{
int value = progressbar.getValue();
if(e1.getSource() == progressbar)
{
label.setText("登陆成功!正在初始化界面:"+Integer.toString(value)+" %");
}
}
}



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

/**
* 登陆窗口类
*/
public class MyWindow extends JFrame implements Runnable {
DrawPanel bg;
JLabel labName = new JLabel();
JLabel labPass = new JLabel();
JTextField txtName = new JTextField();
JPasswordField pass = new JPasswordField();
JButton btnOK = new JButton();
JButton btnExit = new JButton();
LogoTxt txt = new LogoTxt();
MyProgress progress = null;
Thread t;//声名
public MyWindow() {
bg = new DrawPanel("login1.jpg", 700, 386); //自己写的DrawPanel类继承JPanel实现了画图功能
bg.setLayout(null);
btnExit.setBackground(Color.white);
labPass.setFont(new java.awt.Font("宋体", Font.PLAIN, 13));
labPass.setForeground(Color.white);
labName.setFont(new java.awt.Font("宋体", Font.PLAIN, 13));
labName.setForeground(Color.white);
txtName.setBorder(null);
pass.setBackground(Color.white);
pass.setBorder(null);

Toolkit toolkit = Toolkit.getDefaultToolkit(); //抽象的不能new 默认的工具包
Dimension screenSize = toolkit.getScreenSize(); //获得屏幕大小 返回的是Dimension类型
this.setBounds((screenSize.width - 700) / 2,
(screenSize.height - 386) / 2, 700, 386);

labName.setText("操作员");
labName.setBounds(new Rectangle(180, 139, 85, 30));
txtName.setText("t068312");

txtName.setBounds(new Rectangle(233, 146, 109, 20));
pass.setText("123456");
pass.setBounds(new Rectangle(232, 196, 112, 21));
btnOK.setBackground(Color.white);
btnOK.setBounds(new Rectangle(189, 246, 60, 26));
btnOK.setBorder(BorderFactory.createLineBorder(Color.black));
btnOK.setText("登陆");
btnExit.setBounds(new Rectangle(263, 246, 60, 26));
btnExit.setBorder(BorderFactory.createLineBorder(Color.black));
btnExit.setText("取消");
btnOK.addActionListener(new LoginLister());
btnExit.addActionListener(new LoginLister());

t = new Thread(this);//new一个

bg.add(labPass);
bg.add(labName);
bg.add(txtName);
bg.add(pass);
bg.add(btnExit);
bg.add(btnOK);
labPass.setText("口 令");

labPass.setBounds(new Rectangle(180, 191, 85, 30));
this.getContentPane().add(bg);
this.setUndecorated(true); //由于JWindow获得不了焦点所以将起转换为Frame装饰
this.setVisible(true);
}

public static void main(String[] args) {
MyWindow my = new MyWindow();
}

class LoginLister implements ActionListener {
public void actionPerformed(ActionEvent e) {
myListner(e);
}
}


private void myListner(ActionEvent e) {
String name = txtName.getText();
String password = new String(pass.getPassword());
if (e.getSource().equals(btnOK)) {
if (name.length() == 0) {
JOptionPane.showMessageDialog(null, "用户名不能为空!!", "警告",
JOptionPane.WARNING_MESSAGE);
txtName.requestFocus();
return;
}
if (password.length() == 0) {
JOptionPane.showMessageDialog(null, "密码不能为空!!", "警告",
JOptionPane.WARNING_MESSAGE);
pass.requestFocus();
return;
}
String sql = "select * from Login where users = '" + name + "'";
DBConnection dbc = new DBConnection();
dbc.getData(sql);
try {
if (!dbc.res.next()) {
JOptionPane.showMessageDialog(null, "用户名不存在!!", "警告",
JOptionPane.WARNING_MESSAGE);
txt.txttime("操作员" + name + "尝试登陆操作失败!");
txtName.setText("");
pass.setText("");
txtName.requestFocus();
return;
} else
if (!dbc.res.getString("password").equals(password)) {
JOptionPane.showMessageDialog(null, "密码错误!!", "警告",
JOptionPane.WARNING_MESSAGE);
txt.txttime("操作员" + name + "尝试登陆操作失败!密码错误");
pass.setText("");
pass.requestFocus();
return;
} else {
t.start();//如果登陆成功启动线程
dispose();
txt.txttime("操作员" + name + "登陆成功!");
MainFrame tt = new MainFrame(name);
}

} catch (HeadlessException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
dbc.close();
}
}
if (e.getSource().equals(btnExit)) { //取消按钮事件方法
System.exit(0);
}
}

public void run() { //重写run方法
progress = new MyProgress();//new进度条类
bg.add(progress);//将其填加到面版上
progress.setBounds(0, 350, 700, 50);//设置位置
progress.control();//启动进度条
try {
t.sleep(20000);//20秒睡眠
} catch (Exception ex) {
}
}
}

搜索更多相关主题的帖子: 线程 
2007-01-25 19:08
快速回复:线程问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.116851 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved