Jframe Jpanel - 在 window 打开时显示结果

Jframe Jpanel - display result while window opened

我正在尝试使用 Jframe 在 window 应用程序中显示客户端详细信息。一切都很好,除了客户端需要等到过程完成才能在 window(Jframe) 中显示结果。

有没有办法在设置 Jframe.setVisible(true) 后动态显示结果 (Jpanel.add());

注意:我是这个代码的新手

我试过的代码:

        JFrame frame = new JFrame();
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }});
        JPanel panel = new JPanel();
        frame.setVisible(true);
        int y=100;
        for (int i = 0; i < 10; i++) {
            //connecting DB and fetching data #single loop may take 5 secs to complete the process
            panel.add(new JLabel("Client"+i)).setBounds(50,y, 100, 30); //after added, this should display in opened window
            y=y+20;
            //connecting DB and fetching data
            for(int clinetDetails=0;clinetDetails< 3;clinetDetails++) {
                panel.add(new JLabel("ClientDetails"+clinetDetails)).setBounds(50,y, 100, 30);
                y=y+20;
            }
            panel.add(new JLabel("Client :"+i+" Completed")).setBounds(50,y, 100, 30);
            y=y+20;
//          frame.pack();

        }
        panel.setLayout(new GridLayout(0, 1));

        JScrollPane scrollPane = new JScrollPane(panel);
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
        frame.setPreferredSize(new Dimension(300, 300));
        frame.pack();

您可能应该使用 SwingWorker,但这会给您一个想法。

首先你应该在 EDT 上做一些摆动工作,所以我们从创建 GUI 开始。

static public void main(String[] args){
     EventQueue.invokeLater( ()->createGui());
}

static public void createGui(){
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      JPanel panel = new JPanel();
      frame.setVisible(true);


    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

    JScrollPane scrollPane = new JScrollPane(panel);
    frame.getContentPane().setLayout(new BorderLayout());
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setPreferredSize(new Dimension(300, 300));
    frame.pack();

    Runnable r = ()->{
        for (int i = 0; i < 10; i++) {
        //connecting DB and fetching data #single loop may take 5 secs to complete the process     //all of the swing work should be done on the EDT.
            final String clientName = "Client"+i;
            EventQueue.invokeLater( ()->{
                panel.add(new JLabel( clientName ));
            } );
            //connecting DB and fetching data
            for(int clinetDetails=0;clinetDetails< 3;clinetDetails++) {
                    String clientD = "ClientDetails "+clinetDetails;
                    EventQueue.invokeLater( ()->{
                       panel.add(new JLabel(clientD));
                       panel.add(new JLabel( clientName + " Completed" ) );                     
                    });
             }
        }
    };
    new Thread(r).start(); //starts the working thread.
}

这将启动一个 gui,然后当新线程启动时,您将释放 edt,以便 java 可以显示您的 gui,然后随着新信息的产生,它会更新 gui向美国东部时间发布偶数。