Importar um botão de um Jframe para outro

0 respostas
J

Pessoal, depois de vasculhar muito no Google, e até ler muitos exemplos e livros.

Fiquei impressionado com o conhecimento dos usuários deste fórum e por isso me cadastrei para postar esta minha dúvida.

Sou um usuário que sabe básico de java, mas aprendi bem rapido a usar o Swing e até a fazer a ligação de um Jframe com outro por meio de itemMenu ou Botoões o que podem acreditar: É Complicado para quem pega de primeira.

de qualquer forma, o que desejo fazer é o seguinte.

Estou fazendo uma bilheteria de Cinema em Java, e gostaria que quando o usuário entrasse na seção cadastrar número de salas (que é um Jframe chamado pelo Jframe principal “MENU”) e digitasse o número e confirmasse (ou seja, ele clica em um botão que tem um actionEvent), ele em seguida criava um Jbutton no Jframe “MENU”.

Simplificando tudo:

Quero saber como faço para um evento criar um botão, mas não na Jframe em que o botão se encontra (ou seja, local), e sim em uma outra que no meu caso seria a Jframe “MENU”.

Já tentei de tudo e fiz muitas buscas, mas ainda estou complicado para fazer isso.

Estou querendo fazer isso, pois a minha idéia é que fique a amostra as salas que confirmou no cadastro para no “MENU” o usuário apenas clicar na sala e ver a sinopse do filme da Sala.

Porém estou apanhando horrores, estou colocando abaixo o fonte do meu “MENU” e “Jframe Cadastro” para verem o que ja fiz.

PS: estou fazendo em NetBeans.
PS2: Alguem sabe me dizer se do jeito que esta no “MENU” para chamar o Jframe “Cadastro” esta correto? estou com receio de que ele esteja criando é outra Jframe, e não chamando a que eu já criei.

Codigo MENU:

Codigo CADASTRO SALA:

/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
    */

/*

  • CadastroSA.java
  • Created on 13/12/2010, 21:56:10
    */

package cinema20;

import javax.swing.JButton;

import javax.swing.JPanel;

import cinema20.*;

import cinema20.Menu;

import java.awt.Container;

import java.awt.FlowLayout;

import javax.swing.JLabel;

/**
*

  • @author Berthie
    */
    public class CadastroSA extends javax.swing.JFrame {

    /** Creates new form CadastroSA */
    
    public CadastroSA() {
    
    initComponents();
    
    setLocationRelativeTo( null );
    
    }
    

    /** 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.
      
      */
      
      @SuppressWarnings(unchecked)
      
      // 
      
      private void initComponents() {
      
      jLabel1 = new javax.swing.JLabel();
      
      NumeroSalas = new javax.swing.JTextField();
      
      jLabel2 = new javax.swing.JLabel();
      
      SalvarButton = new javax.swing.JButton();
      
      FecharButton = new javax.swing.JButton();
      
      jLabel3 = new javax.swing.JLabel();
      
      MinimoLabel = new javax.swing.JLabel();
      
      setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
      
      setTitle(“CadastroSalas”);
      
      setName(“CadastroSA”); // NOI18N
      
      jLabel1.setFont(new java.awt.Font(“Sylfaen”, 3, 18));
      
      jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
      
      jLabel1.setText(“SALAS DE CINEMA”);
      
      NumeroSalas.setHorizontalAlignment(javax.swing.JTextField.CENTER);
      
      NumeroSalas.setText(“1”);
      
      NumeroSalas.addActionListener(new java.awt.event.ActionListener() {
      
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      
      NumeroSalasActionPerformed(evt);
      
      }
      
      });
      

      jLabel2.setFont(new java.awt.Font(“Tahoma”, 0, 14));
      jLabel2.setText(“Número de Salas”);

      SalvarButton.setText(SALVAR);
      
      SalvarButton.addActionListener(new java.awt.event.ActionListener() {
      
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      
      SalvarButtonActionPerformed(evt);
      
      }
      
      });
      
      FecharButton.setText(“FECHAR”);
      
      FecharButton.addActionListener(new java.awt.event.ActionListener() {
      
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      
      FecharButtonActionPerformed(evt);
      
      }
      
      });
      

      jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
      jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/cadeira1.jpg"))); // NOI18N

      MinimoLabel.setFont(new java.awt.Font(“Tahoma”, 0, 14));
      MinimoLabel.setText(“Mínimo de uma sala”);

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      
      getContentPane().setLayout(layout);
      
      layout.setHorizontalGroup(
      
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      
      .addGroup(layout.createSequentialGroup()
      
      .addContainerGap()
      
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
      
      .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)
      
      .addComponent(jLabel3)
      
      .addComponent(jLabel2)
      
      .addComponent(NumeroSalas, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
      
      .addComponent(MinimoLabel))
      
      .addGroup(layout.createSequentialGroup()
      
      .addComponent(SalvarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
      
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 59, Short.MAX_VALUE)
      
      .addComponent(FecharButton, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)))
      
      .addContainerGap())
      
      );
      
      layout.setVerticalGroup(
      
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      
      .addGroup(layout.createSequentialGroup()
      
      .addComponent(jLabel1)
      
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
      
      .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
      
      .addGap(18, 18, 18)
      
      .addComponent(jLabel2)
      
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
      
      .addComponent(NumeroSalas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
      
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
      
      .addComponent(MinimoLabel)
      
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
      
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
      
      .addComponent(FecharButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
      
      .addComponent(SalvarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE))
      
      .addContainerGap())
      
      );
      

      pack();
      }//

    private void NumeroSalasActionPerformed(java.awt.event.ActionEvent evt) {
    
    int SalasCinema = (int) (Double.parseDouble(NumeroSalas.getText()));
    
    MinimoLabel.setText(SalasCinema + " Sala(s) criada(s) com Sucesso!!!");
    
    int i;
    
    if (SalasCinema == 0){
    
    MinimoLabel.setText(ERRO!! Número Inválido!!);
    
    } else {
    
    for (i=0; i<SalasCinema; i++){
    
    //this.dispose();
             }
         }
    

    }

    private void FecharButtonActionPerformed(java.awt.event.ActionEvent evt) {
    
    this.dispose();
    
    }
    
    private void SalvarButtonActionPerformed(java.awt.event.ActionEvent evt) {
    
    int SalasCinema = (int) (Double.parseDouble(NumeroSalas.getText()));
    
    MinimoLabel.setText(SalasCinema + " Sala(s) criada(s) com Sucesso!!!");
    
    int i;
    
    if (SalasCinema == 0){
    
    MinimoLabel.setText(ERRO!! Número Inválido!!);
    
    } else {
    
    //for (i=0; i<SalasCinema; i++){
    
///////////////////////////////////////////////////////////////////////////////////////////////////

AQUI ENTRA A LOGICA PARA CRIAR O BOTÃO NO JFRAME MENU 

///////////////////////////////////////////////////////////////////////////////////////////////////

}

//}

}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new CadastroSA().setVisible(true);
        }
    });
}

// Variables declaration - do not modify
private javax.swing.JButton FecharButton;
private javax.swing.JLabel MinimoLabel;
private javax.swing.JTextField NumeroSalas;
private javax.swing.JButton SalvarButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
// End of variables declaration

}

Agradeço a todos que me ajudarem! :smiley:

Criado 16 de dezembro de 2010
Respostas 0
Participantes 1