Java Swing JLabels在buildGUI方法中显示,但从其他方法添加时则不显示 [英] Java Swing JLabels show in a buildGUI method but not if added from another method

查看:61
本文介绍了Java Swing JLabels在buildGUI方法中显示,但从其他方法添加时则不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一位业余爱好者,在写射箭记分卡.该程序运行良好,但代价是18行中每行都包含19个相同代码的部分.我正在尝试通过使用方法调用来压缩代码. 我正在使用Java SE6和Mig Layout

I'm an amateur writing an archery score card. The programme works well but at the cost of 19 sections of identical code each of 18 lines. I'm trying to condense the code by using a method call. I'm using Java SE6 and Mig Layout

这是GUI中有效的代码部分. GUI如下所示

Here is the section of code in the GUI which works. The GUI is called as below

主页(包含主要方法)-> ChoiceGUI-> buildScoresPanel

HomePage (containing the main method) -> ChoiceGUI -> buildScoresPanel

    public  void buildScoresPanelMIG(JPanel scoresPanel) {        

    for (row = 0; row<(int)numberofrows; row++){  
       scoresPanel.add(scorelabel1[row],"gapleft 0,w 35px, hmin 35px,split 18");
       scoresPanel.add(scorelabel2[row],"gap before 0px,gapleft 0,w 35px, hmin 35px");
       scoresPanel.add(scorelabel3[row],"gap before 0px,gapleft 0,w 35px, hmin 35px");
       scoresPanel.add(scorelabel4[row],"gap before 0px,gapleft 0,w 35px, hmin 35px");
       scoresPanel.add(scorelabel5[row],"gap before 0px,gapleft 0,w 35px, hmin 35px");
       scoresPanel.add(scorelabel6[row],"gap before 0px,gapleft 0,w 35px, hmin 35px");
       //another 12 Jlabels              }
    }

但是,如果我将代码放入方法中并按如下所示进行调用,即使我尝试了revalidate(),repaint()和setVisible(true),Jlabel也不会显示

If, however I put the code in a method and call it as below the Jlabels won't show even though I've tried revalidate() repaint() and setVisible(true)

    public  void buildScoresPanelMIG(JPanel scoresPanel) {

           for (row = 0; row<(int)numberofrows; row++){  

              addScoreLabels();

           }
    }

    public void addScoreLabels(){

     scoresPanel.add(scorelabel1[row],"gapleft 0,w 35px, hmin 35px,split 18");
     scoresPanel.add(scorelabel2[row],"gap before 0px,gapleft 0,w 35px, hmin 35px");
     scoresPanel.add(scorelabel3[row],"gap before 0px,gapleft 0,w 35px, hmin 35px");
     scoresPanel.add(scorelabel4[row],"gap before 0px,gapleft 0,w 35px, hmin 35px");
     scoresPanel.add(scorelabel5[row],"gap before 0px,gapleft 0,w 35px, hmin 35px");
     scoresPanel.add(scorelabel6[row],"gap before 0px,gapleft 0,w 35px, hmin 35px");
    //another 12 labels
     //scoresPanel.revalidate(); 
     //scoresPanel.repaint();
     //scoresPanel.setVisible(true);
  }

我已经拖了很长时间试图解决该问题,并且我意识到我对Swing组件的工作原理有一个基本的误解,如果有人能够解释,我将不胜感激.

I have trawled the internet for quite a while trying to solve the problem and I realise that I have a fundamental misunderstanding of how Swing components work and would be grateful if someone could explain.

推荐答案

也尝试将scoresPanel作为参数传递给您的addScoreLabels()方法:

Try passing scoresPanel as an argument to your addScoreLabels() method too:

addScoreLabels(scoresPanel);

...

public void addScoreLabels(JPanel scoresPanel) { ...

正如Chris Cooney在评论中指出的那样,您可能在scoresPanel字段变量中存储了一个不同的面板,该变量在第一种方法中被局部变量隐藏,而在第二种方法中则未被隐藏.

As Chris Cooney points out in the comments, you probably have a different panel stored in a scoresPanel field variable, which is being hidden by a local variable in the first method, but not in the second.

这篇关于Java Swing JLabels在buildGUI方法中显示,但从其他方法添加时则不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆