简体   繁体   English

在 JPanel 中动态添加 JLabel(重新验证不起作用)

[英]Add JLabel dynamically in a JPanel (revalidate didn't work)

I have a method that tries to add labels dynamically in JPanel in the PackingByVolDialog class:我有一个方法尝试在 PackingByVolDialog 类的 JPanel 中动态添加标签:

public static void addContent( String content, String container) {

        contentPanel = new JPanel();
        changingLabel = new JLabel(); 
        changingLabel.setText(container+"\n"+content);
        contentPanel.add(changingLabel);
        contentPanel.revalidate();
        contentPanel.repaint();
        dialog.add(contentPanel);
    }

and in an other class , I call the addContent method as follows:在另一个类中,我调用addContent方法如下:

for (int u=0; u<containers.size()-1; u++) {

          PackingByVolDialog.addContent(content, container);

                 } //end for

This is the MCVE (?) I was asked to make ( I hope it's correct and helpful)这是我被要求制作的 MCVE (?) (我希望它是正确和有帮助的)

public class VolDialog extends JDialog {

    private static JLabel label = new JLabel() ;
    private static JPanel contentPanel = new JPanel();
    private static VolDialog dialog = new VolDialog(); 


  public static void main(String[] args) {
        try {
             dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
             dialog.add(contentPanel);
             dialog.revalidate();
             dialog.repaint();
            dialog.setVisible(true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}


 public static void addContent( String text) {

    contentPanel = new JPanel();
    changingLabel = new JLabel(); 
    changingLabel.setText(text);
    contentPanel.add(changingLabel);
    }


 public class content {

       for (int u=0; u<N; u++) {

               VolDialog.addContent("text");

             } //end for
          }

even when using repaint and revalidate of the JPanel, only the last output is displayed in the window.即使使用 JPanel 的repaintrevalidate ,窗口中也只显示最后一个输出。

Does anyone know where the problem is?有谁知道问题出在哪里?

The project I am doing is too big and complicated我做的项目太大太复杂

Which is why you always need to create and post an MCVE when asking a question.这就是为什么您在提问时总是需要创建和发布 MCVE。 We don't have time to look at and understand your entire project.我们没有时间查看和了解您的整个项目。 It is up to you to understand the problem you are asking about and then simplify the code to duplicate the problem.由您来了解您所询问的问题,然后简化代码以复制问题。 Many times when you do this you will find your problem.很多时候,当你这样做时,你会发现你的问题。 If not, then you have a simple piece of code to post.如果没有,那么您可以发布一段简单的代码。

it is not an easy task.这不是一件容易的事。

Once you do it a couple of times, like anything in life, it becomes easier and easier.一旦你做了几次,就像生活中的任何事情一样,它变得越来越容易。

I was in a rush because I don't have time at all to write new codes我很匆忙,因为我根本没有时间编写新代码

Well you should still produce well structured and designed code.好吧,您仍然应该生成结构良好且设计良好的代码。 It takes no more time to create an MVCE properly using normal Swing coding conventions.使用正常的 Swing 编码约定正确创建 MVCE 不再需要时间。

For example:例如:

  1. You should not be extending JDialog.您不应该扩展 JDialog。 All you are doing is adding components to the dialog so in the main() method you just create the dialog.您所做的就是向对话框添加组件,因此在 main() 方法中您只需创建对话框。

  2. You should not be using static variables and methods.您不应该使用静态变量和方法。 This indicates poor design of the code.这表明代码设计不佳。

  3. Class names should start with an upper case character类名应以大写字符开头

  4. Your code doesn't compile.您的代码无法编译。

  5. You never create a new instance of the "content" class, so the addContent(...) method is never invoked.您永远不会创建“内容”类的新实例,因此永远不会调用 addContent(...) 方法。 So this mean you never even tested the code you posted.所以这意味着你甚至从未测试过你发布的代码。

When you ask the question YOU need to take the time to do things properly and give us the information we need to solve the problem.当您提出问题时,您需要花时间正确处理事情,并向我们提供解决问题所需的信息。

only the last output is displayed in the window.窗口中只显示最后一个输出。

The default layout manager of Swing frames and dialogs is a BorderLayout . Swing 框架和对话框的默认布局管理器是BorderLayout You can only add a single component to the "CENTER" of the BorderLayout (which is the default when you don't specify a constraint).您只能将单个组件添加到 BorderLayout 的“CENTER”(这是您未指定约束时的默认设置)。

So basically you need to change the layout manager of the dialog.所以基本上你需要改变对话框的布局管理器。 Try changing:尝试改变:

dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setLayout( new FlowLayout() );

to see the difference.看到差异。

If you need more help then post a proper MCVE.如果您需要更多帮助,请发布适当的 MCVE。 One that actually compiles and is well designed.一个实际编译并且设计良好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM