gpt4 book ai didi

java - 在 JDialogs 之间创建 Swing 动画

转载 作者:行者123 更新时间:2023-11-30 07:27:13 25 4
gpt4 key购买 nike

大家好,首先,我想做的是在用户按下输入对话框中的 OK 按钮和他已成功登录的数据库响应之间运行动画。

我有一个类负责设置 UI 和动画,并且分别有一个单独的类用于登录对话框和数据库。我曾尝试使用 boolean 值制作动画,但它只是卡住了,只有在我关闭对话框后才会动画。我该如何正确设置。这是我尝试过的以及两个类中的相关代码。谢谢

对于登录对话框,loginInput.class:

   class LoginInput extends JDialog {
private JPanel myPanel;
private JPanel buttons_panel;
private JTextField username;
private JPasswordField password;
private JLabel username_label;
private JLabel password_label;
private JButton ok;
private JButton cancel;
protected static boolean succeeded;


public LoginInput(JFrame parent){
super(parent,"Login", true);
initUI(parent);

}

private void initUI(JFrame parent) {

myPanel = new JPanel(new GridLayout(3,2,1,1));
buttons_panel = new JPanel(new FlowLayout());

username_label = new JLabel("Username: ");
password_label = new JLabel("Password: ");
username = new JTextField(20);
password = new JPasswordField(20);

ok = new JButton("Ok");
ok.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

LoginDB db = new LoginDB(getUsername(), getPassword());

try {
db.createConn();
db.execute();

} catch (SQLException ex) {
Logger.getLogger(LoginInput.class.getName()).log(Level.SEVERE, null, ex);
}

/*check if login input is correct and display information dialog accordingly*/
// try {
// if (db.checkUserInput(getUsername(), getPassword())){
if(isSucceeded() == true ){
JOptionPane.showMessageDialog(LoginInput.this, "Hi" + getUsername()+ "! " +
"You have successfully Logged in", "XLite", JOptionPane.INFORMATION_MESSAGE);
succeeded = true;
dispose();

}else{

JOptionPane.showMessageDialog(LoginInput.this,"Invalid username or password","Login",JOptionPane.ERROR_MESSAGE);
// reset username and password
username.setText("");
password.setText("");
succeeded = false;
}
// } catch (SQLException ex) {
//Logger.getLogger(LoginInput.class.getName()).log(Level.SEVERE, null, ex);
//}
}
});

cancel = new JButton("Cancel");
cancel.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
dispose();
}
});

myPanel.add(username_label);
myPanel.add(username);
myPanel.add(password_label);
myPanel.add(password);

buttons_panel.add(ok);
buttons_panel.add(cancel);

getContentPane().add(myPanel, BorderLayout.CENTER);
getContentPane().add(buttons_panel, BorderLayout.PAGE_END);

pack();
setResizable(false);
setLocationRelativeTo(parent);
}


/*return the username from the textbox*/
public String getUsername(){
return username.getText().trim();
}

/*return the password from the textbox*/
public String getPassword(){
return new String(password.getPassword());
}

/*return login status*/
public boolean isSucceeded() {
return succeeded;
}
}

对于主类:

  public myTest() {
initComponents();
String image_name = "/my/myTest/resource/Envelope.jpg";
Image = new ImageIcon(this.getClass().getResource(image_name));
currentImage = Image.getImage();
prepareImage(currentImage, this);
envelope.setX(xpos); //envelope is a class with getters and setters for x and y positions
envelope.setY(ypos);
timer = new Timer(150, this);
//timer.start();
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
loginClient(); //method to call the login dialog class
/*if (login_page.animate == true) {
timer.start();
} */



public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new myTest().setVisible(true);
}
});
}

private void loginClient() {
login_page = new LoginInput(this); // login dialog class
login_page.setSize(220, 150);
login_page.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
Dimension d = jPanel1.getSize();
envelope.setX(envelope.getX() + xmove);
envelope.setY(envelope.getY() + ymove);
if (envelope.getX() > d.getWidth() - imageWidth) {
xmove *= -0.1;
}
if (xpos < 1) {
xmove *= -1;
}
if (envelope.getY() <= d.getHeight() - imageHeight) {
ymove *= -1;
}
repaint();
}

@Override
public void paint(Graphics g) {
super.paint(g);
g.drawImage(currentImage, envelope.getX(), envelope.getY(), this);
g.dispose();
}

对于 loginDB 类:

public class LoginDB extends SwingWorker<Object, Object> {
public Connection conn;
String driver;
String URL;
//username and password to check database
String username;
String password;

//remporary user name and password to hold data
String user;
String pwd;

myTest testview = new myTest();
Timer timer;
boolean result = false;


public LoginDB(String username, String password){
this.user = username;
this.pwd = password;


}


/*create a database connection object with the connection driver Manager*/
public void createConn() throws SQLException{
String filename = "C:/PROJECTS/com/me/login_client/LoginDB";
driver = "sun.jdbc.odbc.JdbcOdbcDriver";
//URL = "jdbc:odbc:XliteLoginDB";
URL = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)}; DBQ=";
URL += filename.trim() + ";DriverID=22;READONLY=true}";
username = "";
password = "";

try {
Class.forName(driver);
conn = DriverManager.getConnection(URL, username, password);

} catch (ClassNotFoundException ex) {
Logger.getLogger(LoginDB.class.getName()).log(Level.SEVERE, null, ex);
}
}

public boolean checkUserInput(String username, String password) throws SQLException{
boolean result;

Statement query = conn.createStatement();
ResultSet rs = query.executeQuery("SELECT Username, Password FROM XliteLogin where " +
"Username='" + username + "'AND Password='" + password +"'");

//ResultSet rs = query.executeQuery("SELECT FROM XliteLogin where " +
// "Username='" + username + "'AND Password= '" + password +"'");
if(rs.next()){
result = true;
}else{
result =false;
}
query.close();
conn.close();

return result;
}

@Override
protected Object doInBackground() throws Exception {

publish(checkUserInput(user,pwd));
return checkUserInput(user,pwd);
}


//@Override
protected void process(boolean chunks) {
testview.timer.start();
}


@Override
protected void done(){

testview.timer.start();
}


}

编辑:似乎无法从 SwingWorker 类或主类之外的任何其他类启动计时器。

最佳答案

您正在阻止 event dispatch thread .相反,从 doInBackground() 中的 SwingWorker 访问数据库并在 process() 中同步动画,如本 example 所示.

补充:关于动画,示例的process()实现将新发布的数据附加到JTextArea,但后台线程也可能通过调用产生动画事件设置进度()。因为监听器在 EDT 上得到通知,所以任何组件都可以更新,而不仅仅是 JProgressBar

关于java - 在 JDialogs 之间创建 Swing 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9893586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com