摇摆父的JFrame / JPanel的不可用/点击而孩子面板被装载(Swing Parent JF

2019-10-17 09:56发布

我加载JFrame AdminFrame (与增加用户和addLocation按钮就可以了),当用户点击的addUser按钮,新JFrame addUser载荷和需要的用户信息。 当信息被提交到服务器时,它在这段时间里我的两个面板被困需要一定的时间(因为服务器是基于网络的)......我想是做AdminFrame 点击使用,能够让用户可以添加新用户或可添加位置......这是我的AdminPanel ■添加按钮的模板代码......不写原来的代码,因为原来的代码太乱..

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

现在,这里是我的ADDUSER小组的adduser按钮Templete代码一部开拓创新的代码太messay ..

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

Answer 1:

您需要使用的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();
            }

        });
    }
}


Answer 2:

您的问题,据我可以告诉的是你没有创建您的事件指派线程图形用户界面,并且比反过来创建另一个JFrame 再次与您最初线程可能长时间运行的任务-作为第一个JFrame还对初始线程创建。

1)不要延长JFrame类不必要的,因为你可能想另一个类,这是非常必要的,但不能延长由于缺乏在Java中多重继承的。 而是创建和实例JFrame ,并调用实例类似的方法:

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

2)创建于Swing组件事件指派线程像这样:

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

3)见多JFrames,好/坏的做法的用处? 。 而是请使用JDialog ,我想是你将去支持或CardLayout (支持在单个各个组件之间翻转JFrame /容器。

4)使用Swing的工人对EDT(或其他GUI长时间运行的任务,inluding您的应用程序的其他打开的窗口,将成为反应迟钝)



文章来源: Swing Parent JFrame/JPanel unusable/clickable while child panel is loaded