Jpanel didn't show rest of it's component

Posted by Ali Amiri on Stack Overflow See other posts from Stack Overflow or by Ali Amiri
Published on 2012-09-08T09:35:32Z Indexed on 2012/09/08 9:37 UTC
Read the original article Hit count: 378

Filed under:
|
|
|
|

I have jfarame with a button and a JPanel as I named panel, I want after I clicked the button add an inner JPanel to my panel. But this but there is a problem with this! because after adding second panel it didn't add any other panel. my code is:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import javax.swing.JTextField;


public class DrawImages extends JFrame{

    int i;
    public DrawImages() {
        JButton btnNewButton = new JButton("New button");
        i = 0;
        getContentPane().add(btnNewButton, BorderLayout.SOUTH);
        setMinimumSize(new Dimension(1000,150));
        final JPanel panel = new JPanel();
        panel.setSize(995, 145);
        getContentPane().add(panel, BorderLayout.CENTER);
        panel.setLayout(null);


        final JLabel lblNewLabel = new JLabel("New label");
        lblNewLabel.setBounds(0, 0, 46, 14);
        panel.add(lblNewLabel);

        btnNewButton.addActionListener(new ActionListener() 
            public void actionPerformed(ActionEvent paramActionEvent) {
                JPanel panel_1 = new JPanel();
                //getContentPane().add(panel_1, BorderLayout.NORTH);
                panel_1.setLayout(null);
                JLabel imagelable = new JLabel(new ImageIcon("c:\\good.jpg"));
                imagelable.setBounds(70, 5, 105, 65);
                panel_1.add(imagelable);

                JLabel lblNewLabel_4 = new JLabel("Up Label");
                lblNewLabel_4.setBounds(5, 5, 65, 35);
                panel_1.add(lblNewLabel_4);

                JLabel lblNewLabel_2 = new JLabel("Down Label");
                lblNewLabel_2.setBounds(5, 25, 65, 65);
                panel_1.add(lblNewLabel_2);
                lblNewLabel.setText(""+i);
                panel_1.setBounds(5+170*i, 5, 170+170*i, 70);
                panel.add(panel_1);
                i++;
            }
        });
        panel.setMinimumSize(new Dimension(995,150));
    }

    public static void main(String[]args)
    {
        DrawImages drawImages = new DrawImages();
        drawImages.setVisible(true);
    }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about image