1

当用户单击新的按钮加载并获取用户信息时,我正在加载一个JFrame AdminFrame(带有添加用户和按钮)。当信息提交到服务器时,需要一些时间(因为服务器是基于网络的),在那段时间我的两个面板都卡住了......我想要的是使可点击可用,以便用户可以添加新用户或可以添加位置...这是我的添加按钮的模板代码...不写原始代码,因为原始代码太乱了..addLocationaddUserJFrame addUserAdminFrame AdminPanel

public class AdminPanel extends JFrame{
       public AdminPanel(){
           Initalize();//Do Initalize Stuff
       }
       public void addBtnActionPerformed(){
           //load Child Form
       }
}//End of class

现在这里是我的 AddUser 面板的 adduser 按钮 Templete 代码 Orignal 代码太乱了..

public class AddUser extends JFrame{
       public AddUser(){
           Initalize();//Do Initalize Stuff
       }
       public void addUserBtnActionPerformed(){
           /*
              take user's form input values make a http request send to server
              get response...now what i have to do here to make parent form clickable?
           */
       }
}//End of class
4

2 回答 2

4

据我所知,您的问题是您没有在Event Dispatch Thread上创建 GUI,然后又在您的初始线程上创建另一个JFrame可能长时间运行的任务- 因为第一个也是在初始线程上创建的。 JFrame

1)不要JFrame不必要地扩展类,因为您可能想要扩展另一个非常必要但由于java中缺乏多重继承而不能的类。而是JFrame在实例上创建和实例化并调用方法,例如:

JFrame frame=new JFrame();
frame.setDefaultCloseOperation(..);
frame.add(..);

2) 在Event Dispatch Thread上创建 Swing 组件,如下所示:

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
           //create Swing and UI components here
        }
    });

3) 参见多个 JFrame 的使用,好/坏做法?. 而是使用JDialog 我认为您将要使用的或CardLayout(允许在单个JFrame/Container 上的各种组件之间翻转)。

4) 在 EDT 上使用Swing Worker执行长时间运行的任务(否则 GUI,包括应用程序的其他打开窗口,将变得无响应)

于 2012-11-23T08:26:10.400 回答
4

您需要使用 SwingWorker 将繁重/冗长的操作分派到另一个线程中,从而允许 UI 线程(EDT)响应事件。

这是一个非常简单的代码,展示了如何使用 SwingWorker(这里模拟了与服务器的连接Thread.sleep()):

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestJFrame implements ActionListener {
    private JProgressBar progress;
    private JButton startButton;
    private JButton testButton;
    private SwingWorker<Void, Integer> worker;

    public void initUI() {
        JFrame frame = new JFrame(TestJFrame.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        progress = new JProgressBar(0, 100);
        startButton = new JButton("Start work");
        testButton = new JButton("Test me while work is in progress");
        startButton.addActionListener(this);
        testButton.addActionListener(this);
        JPanel buttonPanel = new JPanel(new FlowLayout());
        buttonPanel.add(startButton);
        buttonPanel.add(testButton);
        frame.add(buttonPanel, BorderLayout.NORTH);
        frame.add(progress, BorderLayout.SOUTH);
        frame.setSize(600, 400);
        frame.setVisible(true);
    }

    private void showTestDialog() {
        if (worker != null) {
            JOptionPane.showMessageDialog(testButton, "You made a test. See how I still respond while heavy job is in progress?");
        } else {
            JOptionPane.showMessageDialog(testButton,
                    "You made a test, but no job is progress. Hit the \"Start work\" button and hit me again after.");
        }
    }

    private void startWork() {
        if (worker != null) {
            return;
        }
        startButton.setEnabled(false);
        worker = new SwingWorker<Void, Integer>() {

            @Override
            protected Void doInBackground() throws Exception {
                // Outside EDT, we cannot modify the UI, but we can perform lengthy operations
                // without blocking the UI
                for (int i = 0; i < 10; i++) {
                    publish(i * 10);
                    Thread.sleep(1000);
                }
                return null;
            }

            @Override
            protected void process(List<Integer> chunks) {
                // Inside EDT, here we can modify the UI
                super.process(chunks);
                // We only care about the last one
                progress.setValue(chunks.get(chunks.size() - 1));
            }

            @Override
            protected void done() {
                // Inside EDT, he we can modify the UI
                super.done();
                progress.setValue(100);
                startButton.setEnabled(true);
                worker = null;
            }

        };
        worker.execute();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == startButton) {
            startWork();
        } else if (e.getSource() == testButton) {
            showTestDialog();
        }
    }

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
            UnsupportedLookAndFeelException {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestJFrame().initUI();
            }

        });
    }
}
于 2012-11-23T08:36:47.527 回答