【问题标题】:Jframe Jpanel - display result while window openedJframe Jpanel - 打开窗口时显示结果
【发布时间】:2020-03-31 03:12:06
【问题描述】:

我正在尝试使用 Jframe 在窗口应用程序中显示客户端详细信息。一切都很好,只是客户端需要等到进程完成才能在 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();

【问题讨论】:

  • 您在等待时要显示什么?为什么是网格布局?
  • client1 详细信息应显示,直到在循环中处理 client2 详细信息。同样每个客户的详细信息要一一显示

标签: swing jframe jpanel


【解决方案1】:

您可能应该使用 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,然后当产生新信息时,它会通过向 EDT 发布一个偶数来更新 gui .

【讨论】:

  • @MMMMS 我稍微清理了一下,删除了setBounds 调用,而是使用框布局,我在 lamda 表达式之外创建了标签。我不确定现在编译还有什么问题。
  • 语法错误,插入“;”在 new Thread(r).start() 处完成 LocalVariableDeclarationStatement; //启动工作线程。 } 这里
  • @MMMMS 现在可以编译了。
猜你喜欢
  • 2013-06-12
  • 1970-01-01
  • 1970-01-01
  • 2013-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-24
  • 1970-01-01
  • 2018-10-22
相关资源
最近更新 更多