Chamar formulário

10 respostas
flaviotomazio

O que tem de errado ness código?

package visual;

import visual.FormAluno;

//RESTO DO CÓDIGO

private void jmiAlunosActionPerformed(java.awt.event.ActionEvent evt) {                                          
        //Chamar o formulário de alunos
        FormMaterias frmMaterias = new FormMaterias();
        frmMaterias.setVisible(true);
    }

10 Respostas

andredeividi

tenta assim

frmMaterias.show();
flaviotomazio

não rodou

andredeividi

tem como vc postar o codigo da tela que vc quer abrir?

abraço

flaviotomazio

Ele diz algo relacionado a cannot find symbol.

Ps.: O main consegue achar o FormPrincipal e setar o visible, mas o o FormPrincipal e o Sobre estão num pacote chamado visual, e ai não consegue rodar.

Fonte do que ta sendo chamado:
package visual;

public class FormSobre extends javax.swing.JDialog {
    
    /** Creates new form FormSobre */
    public FormSobre(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Código Gerado ">                          
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jbtnOk = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setModal(true);
        setResizable(false);
        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setText("Sistema de Controle de Aulas");

        jLabel2.setText("Projeto");

        jLabel3.setText("Aluno: Aluno");

        jLabel4.setText("Matr\u00edcula: ");

        jLabel5.setText("Campus: ");

        jLabel6.setText("E-mail: ");

        jbtnOk.setText("Ok");
        jbtnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnOkActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel6))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel3))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel4))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jbtnOk))
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING))))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGap(3, 3, 3)
                .addComponent(jLabel2)
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4))
                    .addComponent(jbtnOk))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>                        

    private void jbtnOkActionPerformed(java.awt.event.ActionEvent evt) {                                       
        dispose();
    }                                      
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FormSobre(new javax.swing.JFrame(), true).setVisible(true);
            }
        });
    }
    
    // Declaração de variáveis - não modifique                     
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JButton jbtOk;
    private javax.swing.JButton jbtnOk;
    // Fim da declaração de variáveis                   
    
}
Fonte do que chama o Sobre:
package visual;

import visual.FormAluno;
import visual.FormSobre;
import visual.FormMaterias;

public class FormPrincipal extends javax.swing.JFrame {
    
    /** Creates new form FormPrincipal */
    public FormPrincipal() {
        initComponents();      
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Código Gerado ">                          
    private void initComponents() {
        jMenuBar1 = new javax.swing.JMenuBar();
        jmCadastros = new javax.swing.JMenu();
        jmiAlunos = new javax.swing.JMenuItem();
        jmiTurmas = new javax.swing.JMenuItem();
        jmiAulas = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        jmiSair = new javax.swing.JMenuItem();
        jmRelatorios = new javax.swing.JMenu();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();
        jmSobre = new javax.swing.JMenu();
        jmiSobre = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Sistema de Aulas - Projeto Integrador");
        jMenuBar1.setAutoscrolls(true);
        jmCadastros.setText("Cadastros");
        jmiAlunos.setText("Alunos");
        jmiAlunos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiAlunosActionPerformed(evt);
            }
        });

        jmCadastros.add(jmiAlunos);

        jmiTurmas.setText("Turmas");
        jmCadastros.add(jmiTurmas);

        jmiAulas.setText("Aulas");
        jmCadastros.add(jmiAulas);

        jmCadastros.add(jSeparator1);

        jmiSair.setText("Sair");
        jmiSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiSairActionPerformed(evt);
            }
        });

        jmCadastros.add(jmiSair);

        jMenuBar1.add(jmCadastros);

        jmRelatorios.setText("Relat\u00f3rios");
        jMenuItem6.setText("Aulas por alunos");
        jmRelatorios.add(jMenuItem6);

        jMenuItem7.setText("Total recebido");
        jmRelatorios.add(jMenuItem7);

        jMenuBar1.add(jmRelatorios);

        jmSobre.setText("Sobre");
        jmiSobre.setText("Sobre o Sistema");
        jmiSobre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiSobreActionPerformed(evt);
            }
        });

        jmSobre.add(jmiSobre);

        jMenuBar1.add(jmSobre);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 477, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 279, Short.MAX_VALUE)
        );
        pack();
    }// </editor-fold>                        

    private void jmiAlunosActionPerformed(java.awt.event.ActionEvent evt) {                                          
        //FormAluno frmAluno = new FormAluno();
        //frmAluno.visible();
    }                                         

    private void jmiSobreActionPerformed(java.awt.event.ActionEvent evt) {                                         
       FormSobre frmSobre = new FormSobre();
       frmSobre.setVisible(true);
    }                                        

    private void jmiSairActionPerformed(java.awt.event.ActionEvent evt) {                                        
        System.exit(0);
    }                                       
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FormPrincipal().setVisible(true);
            }
        });
    }
    
    // Declaração de variáveis - não modifique                     
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JMenu jmCadastros;
    private javax.swing.JMenu jmRelatorios;
    private javax.swing.JMenu jmSobre;
    private javax.swing.JMenuItem jmiAlunos;
    private javax.swing.JMenuItem jmiAulas;
    private javax.swing.JMenuItem jmiSair;
    private javax.swing.JMenuItem jmiSobre;
    private javax.swing.JMenuItem jmiTurmas;
    // Fim da declaração de variáveis                   
    
}

EDIT: Tentei retirar os imports pois eles estão no mesmo pacote, mas continua com o erro, porém sem os imports os formulários aparecem no ObjectInspector

Ps.: essa janela que está sendo chama é do tipo javax.swing.JDialog

andredeividi

no seu initComponents;

setSize(462, 370); Dimension Tela = this.getToolkit().getScreenSize(); this.setLocation((int) (Tela.getWidth() - this.getWidth()) / 2, (int) (Tela.getHeight() - this.getHeight()) / 2); this.show();

continua chamando assim

private void jmiAlunosActionPerformed(java.awt.event.ActionEvent evt) { //Chamar o formulário de alunos FormMaterias frmMaterias = new FormMaterias(); frmMaterias.setVisible(true); }

flaviotomazio

Esse

setSize(462, 370); Dimension Tela = this.getToolkit().getScreenSize(); this.setLocation((int) (Tela.getWidth() - this.getWidth()) / 2, (int) (Tela.getHeight() - this.getHeight()) / 2); this.show();

Eu coloco nos inits dos formulários ou no principal?

O Dimension Tela = this.getToolkit().getScreenSize(); da erro.

flaviotomazio

Não rodou. Mas valeu pela ajuda

andredeividi

Cara o erro ai é que vc não esta definindo o tamanho da sua tela
a qual vc quer abrir esse codigo deve ficar no init da tela que vc quer abrir

abraço

flaviotomazio

Mas eu defini o tamanho, mas assim mesmo lá no principal ele fala de um erro de construtor.

EDIT: Uma screen

flaviotomazio

Problema resolvido, como ele estava chamando um JDialog eu tinha que informar o form pai.

FormMaterias frmMaterias = new FormMaterias(this,true); frmMaterias.setVisible(true);

Criado 23 de junho de 2007
Ultima resposta 24 de jun. de 2007
Respostas 10
Participantes 2