Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

java - add component to jpanel on runtime

I have a JPanel and JButton on the JFrame.
on runtime add JLabel to JPanel When click 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I see you create a JLabel called _lbl:

 JLabel _lbl=new JLabel();

but you never add it to your panel. Instead you add a new JLabel with no text to your panel:

 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 _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.

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.

Rather use a LayoutManager:

or if you only have a single component on the JPanel simply call add(label); as it will stretch to the JPanel size.

UPDATE:

Here is a small sample. Simply adds JLabels to the JPanel each time JButton is pressed:

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);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...