简体   繁体   English

在运行时将组件添加到jpanel

[英]add component to jpanel on runtime

I have a JPanel and JButton on the JFrame . 我在JFrame上有一个JPanelJButton
on runtime add JLabel to JPanel When click JButton . 在运行时将JLabel添加到JPanel单击JButton

I use of the following code: 我使用以下代码:

 panel.setLayout(null);

 jLabel _lbl=new jLabel();
 _lbl.setText("Label");
 panel.add(_lbl);
 panel.validate();

but no display any JLabel in JPanel . 但是没有在JPanel显示任何JLabel

I see you create a JLabel called _lbl : 我看到你创建了一个名为_lblJLabel

 JLabel _lbl=new JLabel();

but you never add it to your panel. 但你永远不会把它添加到你的面板。 Instead you add a new JLabel with no text to your panel: 相反,您添加一个没有文本的新JLabel到您的面板:

 panel.add(new JLabel());

This would ofcourse construct an empty label which wont be visible. 这将构成一个不可见的空标签。

Also try calling revalidate() and repaint() on your JPanel instance after adding the JLabel like so: 在添加JLabel之后,尝试在JPanel实例上调用revalidate()repaint() ,如下所示:

JLabel _lbl=new JLabel("Label");//make label and assign text in 1 line

panel.add(_lbl);//add label we made

panel.revalidate();
panel.repaint();

With this you may also need to call pack() on your frames instance so as to resize the JFrame to fit the new components. 有了这个,你可能还需要在frame实例上调用pack() ,以便调整JFrame大小以适应新的组件。

Also please never use a null / Absolute layout this is very bad practice (unless doing animation) and may prove to be problematic and very hard to use. 另外请永远不要使用null / Absolute布局这是非常糟糕的做法(除非做动画)并且可能被证明是有问题且非常难以使用。

Rather use a LayoutManager : 而是使用LayoutManager

or if you only have a single component on the JPanel simply call add(label); 或者如果你在JPanel上只有一个组件,只需调用add(label); as it will stretch to the JPanel size. 因为它将延伸到JPanel大小。

UPDATE: 更新:

Here is a small sample. 这是一个小样本。 Simply adds JLabel s to the JPanel each time JButton is pressed: 每次按下JButton只需将JLabel添加到JPanel

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class JavaApplication116 {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new JavaApplication116().createAndShowUI();
            }
        });
    }

    private void createAndShowUI() {
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        initComponents(frame);

        frame.setResizable(false);
        frame.pack();
        frame.setVisible(true);
    }

    private void initComponents(final JFrame frame) {
        final JPanel panel = new JPanel(new FlowLayout());
        JButton button = new JButton("Add label");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JLabel _lbl = new JLabel("Label");//make label and assign text in 1 line

                panel.add(_lbl);//add label we made

                panel.revalidate();
                panel.repaint();

                frame.pack();//so our frame resizes to compensate for new components
            }
        });
        frame.getContentPane().add(panel, BorderLayout.CENTER);
        frame.getContentPane().add(button, BorderLayout.SOUTH);
    }
}

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

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