Código frame:
Código:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Framecito extends JFrame implements ActionListener{
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Framecito frame = new Framecito();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Framecito() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
Panelcito p=new Panelcito();
Container conteiner=getContentPane();
JButton btnClick = new JButton("Click");
btnClick.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
conteiner.removeAll();
p.setVisible(true);
conteiner.add(p);
revalidate();
repaint();
}
});
btnClick.setBounds(149, 121, 89, 23);
contentPane.add(btnClick);
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
Código Panel:
Código:
import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
public class Panelcito extends JPanel {
/**
* Create the panel.
*/
public Panelcito() {
setBackground(Color.GREEN);
setLayout(null);
JLabel labell = new JLabel("ESTÁS DENTRO DEL PANEL");
labell.setFont(new Font("Tahoma", Font.PLAIN, 15));
labell.setBounds(101, 131, 254, 14);
add(labell);
}
}