CardLayout能不能选定要显示的JPanel啊???
还有个问题,我用Swing写的界面,连接SQL验证密码
但是不知道为什么编译运行的时候电脑CPU使用率会到90%几,请问这个是我编码的问题还是什么???
谢谢
[此贴子已经被作者于2007-6-20 23:48:06编辑过]
能帮我看看是什么问题嘛??
这个是登陆界面
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();
}
}
这个是主界面
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 Workpane extends JFrame{
Workframe wfra;
Workthread wth;
Container con;
JPanel pan;
JButton choosebtn,musicbtn;
int x,y;
CardLayout c=new CardLayout();
Workpane()
{
super("主界面");
con=getContentPane();
con.setLayout(c);
pan=new JPanel();
pan.setLayout(null);
choosebtn=new JButton("请选择您要查看的版块");
choosebtn.setBounds(new Rectangle(130, 350, 130, 50));
musicbtn=new JButton("您也可以");
musicbtn.setBounds(new Rectangle(530, 350, 130, 50));
pan.add(choosebtn);
pan.add(musicbtn);
con.add("one",pan);
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(false);
}
}