但我自己看了书和API想自学一下,书上和API上的例子都是写在一个JAVA文件里的,这些我都搞懂了
但我现在想写个东西然后MAIN方法的文件和线程是在两个JAVA文件里的我就搞不懂了,RUN里面到底应该写什么呢?书上和API上说,RUN里面写你要工作的语句,
我也写了,但我一运行我看任务管理器,我电脑的线程数就会变成4000多
void start() {
Thread thread = new Thread(this);
if (wfra.pbar.getValue() == 100) {
if (wfra.count == 1) {
wfra.pbarlab.setText("搜索已完成");
int result = JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog(wfra.con, "用户"+wfra.inputtxt.getText()+"欢迎您回来", "通过验证",
JOptionPane.INFORMATION_MESSAGE);
wfra.dispose();
wpan=new Workpane();
wpan.setVisible(true);
}
else {
JOptionPane.showMessageDialog(wfra.con, "无效的用户名或密码,请重新输入", "验证失败",
JOptionPane.ERROR_MESSAGE);
wfra.inputtxt.setText("");
wfra.inputword.setText("");
wfra.pbarlab.setText("搜索已完成");
}
}
thread.start();
}
public void run() {
start();
}
}
我已经感觉到我错的很离谱了,所以请指教下,谢谢
这个是登陆界面
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;
Workpane wpan;
Workframe() {
super("登陆界面");
Workthread wth = new Workthread();
wth.workame(this);
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));
exitbtn.addActionListener(this);
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(90, 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);
setResizable(false);
setVisible(true);
wth.start();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == landbtn) {
timer.start();
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) {}
}
if (e.getSource() == timer) {
int value = pbar.getValue();
if (value < 100) {
value++;
pbar.setValue(value);
}
else {
timer.stop();
pbar.setValue(0);
}
}
if(e.getSource()==exitbtn)
{
System.exit(0);
}
}
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();
}
}
这个是控制对话框的
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 Workthread
implements Runnable {
Workframe wfra;
Thread thread;
Workpane wpan;
Workthread() {}
void workame(Workframe wfra) {
this.wfra = wfra;
}
void start() {
Thread thread = new Thread(this);
if (wfra.pbar.getValue() == 100) {
if (wfra.count == 1) {
wfra.pbarlab.setText("搜索已完成");
int result = JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog(wfra.con, "用户"+wfra.inputtxt.getText()+"欢迎您回来", "通过验证",
JOptionPane.INFORMATION_MESSAGE);
wfra.dispose();
wpan=new Workpane();
wpan.setVisible(true);
}
else {
JOptionPane.showMessageDialog(wfra.con, "无效的用户名或密码,请重新输入", "验证失败",
JOptionPane.ERROR_MESSAGE);
wfra.inputtxt.setText("");
wfra.inputword.setText("");
wfra.pbarlab.setText("搜索已完成");
}
}
thread.start();
}
public void run() {
start();
}
}