有2个按钮,用户输入用户名和密码后,数据会传到SQL里去判断密码
是否正确,不管正确于否都会弹出JOPTIONDIPANE对话框
然后我在Container里又加了个进度条,问题来了
因为我进度条和验证密码都是触发登陆按钮来实现的
我想做到等进度条读到100时才弹出JOPTIONDIPANE对话眶,我想在另外个文件里写个类实现线程接口来控制,但试了好久都不行,请各位指导~~~谢谢
我好象弄不来诶老是异常~~~我也没学过线程,就自己看了参考书和别人写的例子,我把代码贴出来麻烦帮我看下
这个是界面
package woke;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.io.*;
import java.sql.*;
class Workframe
extends JFrame implements ActionListener, ChangeListener {
Container con;
JPanel pan;
JButton landbtn, exitbtn, loginbtn;
JTextField inputtxt;
JPasswordField inputword;
JLabel txtlab, wordlab, idlab, pbarlab;
JComboBox idbox;
java.util.Vector idvor;
JProgressBar pbar;
Timer timer;
int x, y,count;
Workthread wth;
Workframe() {
super("登陆界面");
Workthread wth=new Workthread();
con = getContentPane();
con.setLayout(new BorderLayout());
pan = new JPanel();
pan.setLayout(null);
landbtn = new JButton("登陆");
landbtn.setBounds(new Rectangle(130, 350, 130, 50));
landbtn.addActionListener(this);
exitbtn = new JButton("退出");
exitbtn.setBounds(new Rectangle(530, 350, 130, 50));
loginbtn = new JButton("注册");
loginbtn.setBounds(new Rectangle(330, 350, 130, 50));
txtlab = new JLabel("用户名");
txtlab.setBounds(new Rectangle(130, 50, 200, 50));
wordlab = new JLabel("密码");
wordlab.setBounds(new Rectangle(130, 150, 200, 50));
idlab = new JLabel("权限");
idlab.setBounds(new Rectangle(130, 250, 200, 50));
inputtxt = new JTextField();
inputtxt.setBounds(new Rectangle(200, 60, 200, 30));
inputword = new JPasswordField();
inputword.setBounds(new Rectangle(200, 160, 200, 30));
idvor = new java.util.Vector();
idvor.addElement("管理人员");
idvor.addElement("公司员工");
idvor.addElement("普通客户");
idvor.addElement("VIP客户");
idbox = new JComboBox(idvor);
idbox.setBounds(new Rectangle(200, 260, 200, 30));
pbar = new JProgressBar();
pbar.setOrientation(JProgressBar.HORIZONTAL);
pbar.setMinimum(0);
pbar.setMaximum(100);
pbar.setValue(0);
pbar.setStringPainted(false);
pbar.addChangeListener(this);
pbar.setPreferredSize(new Dimension(200, 30));
pbar.setLocation(150, 320);
pbar.setBorderPainted(false);
pbarlab = new JLabel();
pbarlab.setBounds(new Rectangle(275, 400, 250, 50));
timer = new Timer(75, this);
pan.add(pbarlab);
pan.add(idbox);
pan.add(idlab);
pan.add(inputword);
pan.add(inputtxt);
pan.add(wordlab);
pan.add(txtlab);
pan.add(landbtn);
pan.add(exitbtn);
pan.add(loginbtn);
con.add(pan, BorderLayout.CENTER);
con.add(pbar, BorderLayout.SOUTH);
setSize(800, 500);
x = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth());
y = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight());
setLocation( (x - 800) / 2, (y - 500) / 2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == landbtn) {
timer.start();
}
if (e.getSource() == timer) {
int value = pbar.getValue();
if (value < 100) {
value++;
pbar.setValue(value);
}
else {
timer.stop();
pbar.setValue(0);
}
}
if (e.getSource() == landbtn) {
int num = idbox.getSelectedIndex();
String name = inputtxt.getText();
String password = new String(inputword.getPassword());
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException ex) {
}
try {
Connection con = DriverManager.getConnection("jdbc:odbc:sky");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(
"select*from Userinformation where Name='" + name +
"'and Password='" + password + "'and Popedom='" + num + "'");
if (rs.next()) {
count=1;
}
else {
count=0;
}
rs.close();
stmt.close();
stmt.close();
}
catch (SQLException ex1) {}
}
}
public void stateChanged(ChangeEvent e1) {
int value = pbar.getValue();
if (e1.getSource() == pbar) {
pbarlab.setText("正在验证用户信息请稍候,已完成搜索:" + Integer.toString(value) + " %");
}
}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
new Workframe();
}
}
这个是线程
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.io.*;
import java.sql.*;
class Workthread implements Runnable{
Workframe wfra;
Thread thread;
Workthread()
{
Workframe wfra=new Workframe();
Thread thread=new Thread(this);
thread.start();
}
public void run()
{
if(wfra.pbar.getValue()==100)
{
if(wfra.count ==1)
{
JOptionPane.showMessageDialog(wfra.con, "欢迎您回来!", "通过验证",
JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(wfra.con, "无效的用户名或密码,请重新输入", "验证失败",
JOptionPane.ERROR_MESSAGE);
}
}
}
}