Estou com dúvida para executar um comando de fechar o Jpanel e retornar ao Jframe com opção de abrir outro Jpanel, pois do jeito que está não está limpando a tela. o programa abre o jpanel mas ele fica fixo.
Programa com o menu
package revisao;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class painelCadastro extends javax.swing.JFrame {
private JMenuBar jMenuBar1;
private JMenu jMenu5;
private JPanel jPanel1;
private JMenuItem jMenuItem6;
private JMenuItem jMenuItem5;
private JMenuItem jMenuItem4;
private JMenu jMenu2;
private JMenuItem jMenuItem3;
private JMenuItem jMenuItem2;
private JMenuItem jMenuItem1;
private JMenu jMenu1;
//editado manualmente para chamar as telas
private NewJPanelEstagiario JPanelEstagiario = new NewJPanelEstagiario();
private NewJPanelFuncionario JPanelFuncionario = new NewJPanelFuncionario();
private NewJPanelDiasTrabalhados JPanelDiasTrabalhados = new NewJPanelDiasTrabalhados();
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
painelCadastro inst = new painelCadastro();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public painelCadastro() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
jPanel1 = new JPanel();
getContentPane().add(jPanel1, BorderLayout.CENTER);
}
{
jMenuBar1 = new JMenuBar();
setJMenuBar(jMenuBar1);
{
jMenu1 = new JMenu();
jMenuBar1.add(jMenu1);
jMenu1.setText("Cadastrar");
{
jMenuItem1 = new JMenuItem();
jMenu1.add(jMenuItem1);
jMenuItem1.setText("Funcionários");
jMenuItem1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
}
{
jMenuItem2 = new JMenuItem();
jMenu1.add(jMenuItem2);
jMenuItem2.setText("Estagiário");
jMenuItem2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});
}
{
jMenuItem6 = new JMenuItem();
jMenu1.add(jMenuItem6);
jMenuItem6.setText("Horas Trabalhadas");
jMenuItem6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jMenuItem6ActionPerformed(evt);
}
});
}
{
jMenuItem3 = new JMenuItem();
jMenu1.add(jMenuItem3);
jMenuItem3.setText("Sair");
jMenuItem3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jMenuItem3ActionPerformed(evt);
}
});
}
}
{
jMenu5 = new JMenu();
jMenuBar1.add(jMenu5);
jMenu5.setText("Calcular");
{
jMenuItem5 = new JMenuItem();
jMenu5.add(jMenuItem5);
jMenuItem5.setText("Salário");
}
}
{
jMenu2 = new JMenu();
jMenuBar1.add(jMenu2);
jMenu2.setText("Ajuda");
{
jMenuItem4 = new JMenuItem();
jMenu2.add(jMenuItem4);
jMenuItem4.setText("Sobre");
}
}
}
pack();
this.setSize(515, 390);
} catch (Exception e) {
e.printStackTrace();
}
}
private void jMenuItem3ActionPerformed(ActionEvent evt) {
//botão sair no menu Cadastros
System.exit(0);
}
private void jMenuItem2ActionPerformed(ActionEvent evt) {
//botão Estagiário no menu Cadastros
JPanelEstagiario.setVisible(true);
this.getContentPane().remove(jPanel1);
this.getContentPane().add(JPanelEstagiario);
JPanelEstagiario.setPreferredSize(new java.awt.Dimension(507, 340));
pack();
}
private void jMenuItem1ActionPerformed(ActionEvent evt) {
//botão Funcionário no menu Cadastros
JPanelFuncionario.setVisible(true);
this.getContentPane().remove(jPanel1);
this.getContentPane().add(JPanelFuncionario);
JPanelFuncionario.setPreferredSize(new java.awt.Dimension(507, 340));
pack();
}
private void jMenuItem6ActionPerformed(ActionEvent evt) {
//botão Dias no menu Cadastros
JPanelDiasTrabalhados.setVisible(true);
this.getContentPane().remove(jPanel1);
this.getContentPane().add(JPanelDiasTrabalhados);
JPanelDiasTrabalhados.setPreferredSize(new java.awt.Dimension(507, 340));
pack();
}
}
Programa com o Jpanel com o botão abandonar
package revisao;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.WindowConstants;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import sun.java2d.Disposer;
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class NewJPanelEstagiario extends javax.swing.JPanel {
private JLabel jLabel1;
private JTextField jTextField1;
private JLabel jLabel2;
private JLabel jLabel3;
private JLabel jLabel4;
private JLabel jLabel5;
private JButton jButton1;
private JLabel jLabel6;
private JButton jButton3;
private JButton jButton2;
private JTextField jTextField5;
private JTextField jTextField4;
private JTextField jTextField3;
private JTextField jTextField2;
/**
* Auto-generated main method to display this
* JPanel inside a new JFrame.
*/
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new NewJPanelEstagiario());
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public NewJPanelEstagiario() {
super();
initGUI();
}
private void initGUI() {
try {
setPreferredSize(new Dimension(400, 300));
this.setLayout(null);
{
jLabel1 = new JLabel();
this.add(jLabel1);
jLabel1.setText("Nome");
jLabel1.setBounds(22, 31, 190, 14);
}
{
jTextField1 = new JTextField();
this.add(jTextField1);
jTextField1.setBounds(22, 47, 333, 21);
}
{
jLabel2 = new JLabel();
this.add(jLabel2);
jLabel2.setText("Data de Nascimento");
jLabel2.setBounds(22, 78, 169, 14);
}
{
jTextField2 = new JTextField();
this.add(jTextField2);
jTextField2.setBounds(22, 95, 94, 21);
}
{
jLabel3 = new JLabel();
this.add(jLabel3);
jLabel3.setText("Qual a área do estágio?");
jLabel3.setBounds(22, 124, 283, 14);
}
{
jTextField3 = new JTextField();
this.add(jTextField3);
jTextField3.setBounds(22, 140, 249, 21);
}
{
jLabel4 = new JLabel();
this.add(jLabel4);
jLabel4.setText("Instituição de ensino?");
jLabel4.setBounds(22, 173, 179, 14);
}
{
jTextField4 = new JTextField();
this.add(jTextField4);
jTextField4.setBounds(22, 190, 249, 21);
}
{
jLabel5 = new JLabel();
this.add(jLabel5);
jLabel5.setText("Qual a quantidade de horas que ira trabalhar?");
jLabel5.setBounds(22, 220, 304, 14);
}
{
jTextField5 = new JTextField();
this.add(jTextField5);
jTextField5.setBounds(22, 237, 94, 21);
}
{
jButton2 = new JButton();
this.add(jButton2);
jButton2.setText("Limpar");
jButton2.setBounds(136, 267, 98, 21);
}
{
jButton3 = new JButton();
this.add(jButton3);
jButton3.setText("Abandonar");
jButton3.setBounds(253, 267, 102, 21);
jButton3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
}
{
this.add(jLabel6);
jLabel6.setText("Cadastro de Estagiário");
jLabel6.setBounds(22, 11, 291, 14);
jLabel6.setFont(new java.awt.Font("Tahoma",1,14));
jLabel6.setForeground(new java.awt.Color(0,0,255));
}
{
jLabel6 = new JLabel();
jButton1 = new JButton();
this.add(jButton1);
jButton1.setText("Gravar");
jButton1.setBounds(28, 267, 88, 21);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void jButton3ActionPerformed(ActionEvent evt) {
//botão abandonar no NewJPanelEstagiario
}
}