首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当选择JCombobox时GUI冻结,如何在Swing应用程序中使用actionListener中的条件语句?

当选择JCombobox时GUI冻结,如何在Swing应用程序中使用actionListener中的条件语句?
EN

Stack Overflow用户
提问于 2016-10-29 19:45:00
回答 1查看 598关注 0票数 0

我正在处理一个登录框架,在这个框架中,我将检索并填充用户创建的数据库中的JComboBox,然后根据从JComboBox中选择的项设置JLabel文本。我想推迟执行并显示“连接.”JLabel中的文本当用户从JComboBox中选择一个项目时,当我选择一个项目时,GUI冻结,5秒后它显示“连接”,它跳过了“Connecting.”JLabel。

事先非常感谢。

代码语言:javascript
运行
复制
package com.softoak.dba;

import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import javax.swing.JComboBox;

public class Login{

private JFrame frame;
static ArrayList<String> dbcom = new ArrayList<String>();
static String[] usecom;
JLabel status = new JLabel("•");
JLabel lblNotConnected;

/**
 * Launch the application.
 */
public static void main(String[] args) throws Exception{
    Connection m_Connection = DriverManager.getConnection("jdbc:sqlserver://localhost;integratedSecurity=true");

    String dbs =  "SELECT * FROM sys.databases WHERE owner_sid != 1";

    Statement st = m_Connection.createStatement();

    ResultSet m_ResultSet = st.executeQuery(dbs);
    dbcom.add("-None-");

    while(m_ResultSet.next()){
        dbcom.add(m_ResultSet.getString(1));
    }
    usecom = new String[dbcom.size()];
    usecom = dbcom.toArray(usecom);

    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Login window = new Login();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Login() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    status.setBounds(385, 235, 10, 10);
    status.setForeground(Color.red);
    frame.getContentPane().add(status);

    JLabel lblLoginApplication = new JLabel("Login Application");
    lblLoginApplication.setFont(new Font("Lucida Calligraphy", Font.BOLD, 20));
    lblLoginApplication.setBounds(120, 25, 220, 30);
    frame.getContentPane().add(lblLoginApplication);

    JComboBox comboBox = new JComboBox(usecom);
    comboBox.setBounds(230, 80, 110, 30);
    comboBox.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e)  {
                if (comboBox.getSelectedIndex() == 1 || comboBox.getSelectedIndex() == 2) {
                    lblNotConnected.setText("Connecting...");
                    try{
                        Thread.sleep(5000);
                  }catch(InterruptedException ex){
                        JOptionPane.showMessageDialog(null,ex.getMessage());
                  }
                    status.setForeground(Color.green);
                    lblNotConnected.setText("Connected");
                    JOptionPane.showMessageDialog(null, "Connected");
                }
                  else{
                      status.setForeground(Color.red);
                      lblNotConnected.setText("Not Connected");
                  }
                }
      });
    frame.getContentPane().add(comboBox);

    JLabel lblSelectDatabase = new JLabel("Select Database");
    lblSelectDatabase.setFont(new Font("Microsoft Sans Serif", Font.BOLD, 14));
    lblSelectDatabase.setBounds(91, 79, 129, 30);
    frame.getContentPane().add(lblSelectDatabase);

    lblNotConnected = new JLabel("Not Connected");
    lblNotConnected.setFont(new Font("Elephant", Font.PLAIN, 12));
    lblNotConnected.setBounds(280, 230, 110, 20);
    frame.getContentPane().add(lblNotConnected);
}

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-29 19:52:38

在侦听器中执行的代码在Event Dispatch Thread(EDT)上执行。当您使用Thread.sleep()时,这会导致EDT休眠,这意味着GUI无法重新绘制自己。有关更多信息,请阅读有关并发性的Swing教程中的部分。

出于上述原因,任何可能长时间运行的任务都不应该在EDT上执行。相反,您应该使用单独的线程。请参阅上述教程中的SwingWorker。您可以在那里使用Thread.sleep()并发布想要显示的值。

但是,为什么要让用户等待5秒呢?我知道我会很沮丧。

也许您应该使用ProgressMonitor来让用户知道您可能正在执行一个长期运行的任务。有关更多信息和工作示例,请参阅有关如何使用进度条的Swing教程中的部分。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40323017

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档