【问题标题】:Adding JLabels to a Panel inside a Panel inside another Panel将 JLabels 添加到另一个面板内的面板内的面板
【发布时间】:2014-04-28 18:51:12
【问题描述】:

我有一个“主”面板。我想在主面板中有一个“侧面”面板。侧面由另外两个面板组成,我们称之为一个图形面板和一个支持面板。我正在尝试将标签从主标签添加到 SupportPanel,但没有发生任何变化。

这是我的侧面板:

public class LateralSupportPane extends JPanel{

private final static int WIDTH = 240;
private final static int HEIGHT = 740;
private GraphicPanel gp;
private SupportPanel sp;


public LateralSupportPane(){
    this.gp = new GraphicPanel();
    this.sp = new SupportPanel();
    this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
    this.setLayout(new GridLayout(2, 1));
    //this.setBorder(BorderFactory.createLineBorder(Color.black));
    this.add(gp);
    this.add(sp);

    this.setVisible(true);
}

    public void addLabel(String label){
    sp.addLabel(label);
}


public void paintComponent(Graphics g){
    super.paintComponent(g);
    gp.paintComponent(g);
}

public void addLabel(String label){
    sp.addLabel(label);
}

这是我的支持面板:

public class SupportPanel extends JPanel{
private JLabel label;
private final static int WIDTH = 240;
private final static int HEIGHT = 370;

public SupportPanel(){

    this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
    label = new JLabel();
    label.setText("<html>BlaBla</html>");
    this.setLayout(new GridLayout(10, 1));
    this.add(label);
    this.setVisible(true);
}

public JLabel getLabel() {
    return label;
}

public void addLabel(String text){
    JLabel label = new JLabel(text);
    if(this.getComponentCount() < 10){
        this.add(label);
    } else {
        this.remove(0);
        this.add(label);
    }
}

我从主面板调用侧面板的 addLabel。

编辑:这是所有面板的框架。板本身是添加到框架中的面板。该板还有另一个面板,即黑色矩形和字符串所在的区域。然后侧面板由另外 2 个面板组成,GraphicPanel(黑色矩形)和 supportPanel,这是我想要放置标签的区域。

Board

验证所有面板没有任何进展。

【问题讨论】:

    标签: java swing jpanel jlabel


    【解决方案1】:

    不确定我是否正确地理解它,但它接缝,你必须在插入新标签后验证你的面板;

    public static void main(String[] args) {
        JFrame frame = new JFrame("test");
        frame.setSize(900, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new CardLayout());
        frame.setVisible(true);
        LateralSupportPane p = new LateralSupportPane();
        frame.add(p);
        frame.validate();
        p.addLabel("test 2");
        p.validate();
    }
    

    如您所见,添加标签后,将执行验证并在表单上绘制对象。 您的方法 addLabel(String label) 应该在其末尾调用此方法。

    【讨论】:

    • 尝试在 addLabel(String label) 和 addLabel() 中添加验证,所以在每个面板中,但没有任何效果。我将在帖子中添加进一步的解释。
    • 这有点令人困惑,我在LateralSupportPane 方法中添加了验证,它工作正常。您能提供更多代码,以便我按照您的方式编译吗?
    • 我知道,结构令人困惑,但我无法想象另一种方式来实现在我的整体结构中动态添加和删除的 JLabel 列表。我不能给你更多代码,因为我必须粘贴整个板代码,而且你可以想象,它相当庞大。尝试在主框架内放置一个面板。在这个面板中另一个面板和最后一个面板中,设置了一个 2 行和 1 列的网格布局,其他 2 个面板。在其中添加标签并调用我的 addLabel(String text) 方法。谢谢
    • 你为什么不用JList?它接缝你需要与运动信息进行某种聊天。 JListJLabel 好得多
    • 我什至不知道JList的存在!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-15
    • 2012-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多