Parar acao ao clicar no JButton

9 respostas
G

tenho que fazer

public static void main(String[] args) {
    int i=65;
while(i<91){
char c=(char) i;
i++;
if(i<91){i=i;}
else{i=65;}
}

    }}

eu tenho que fazer esse codigo mostrar o c quando clica no JButton. vlw a ajuda glr

9 Respostas

G

a janela esta criada

RodrigoKaos

Ninguem vai fazer pra vc, vc n teve capacidade pra criar uma janela, e dar up é contra as regras

G
RodrigoKaos:
Ninguem vai fazer pra vc, vc n teve capacidade pra criar uma janela, e dar up é contra as regras
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author user
 */
public class Cristina extends javax.swing.JFrame {

    /**
     * Creates new form Cristina
     */
    public Cristina() {
        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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        letra = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        letra.setText("sair letra");
        letra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                letraActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(173, Short.MAX_VALUE)
                .addComponent(letra)
                .addGap(184, 184, 184))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(208, Short.MAX_VALUE)
                .addComponent(letra)
                .addGap(74, 74, 74))
        );

        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()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(19, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

    private void letraActionPerformed(java.awt.event.ActionEvent evt) {                                      
        
    }                                     

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        
     
        
        
        
        
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Cristina.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Cristina.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Cristina.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Cristina.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Cristina().setVisible(true);
            
 
            
            
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton letra;
    // End of variables declaration                   
}
RodrigoKaos

[i]Sabe como se usa um booleano??

Cria um booleano e usa ele como condição
Usa o JButton pra alterar o valor desse booleano.

Seja feliz

[/i]

G

RodrigoKaos:
[i]Sabe como se usa um booleano??

Cria um booleano e usa ele como condição
Usa o JButton pra alterar o valor desse booleano.

Seja feliz

[/i]


mas qual é o booleano que o jbutton cria?

RodrigoKaos

[i]Ta de brincation with me cara?

Cria um booleano[/i]boolean brincation = true;Usa esse boollean como condição na sua estrutura de controle
e quando vc clicar no botão, essa condição muda pra false

G

RodrigoKaos:
[i]Ta de brincation with me cara?

Cria um booleano[/i]boolean brincation = true;Usa esse boollean como condição na sua estrutura de controle
e quando vc clicar no botão, essa condição muda pra false


esse “brincation” seria o nome do botao? e onde eu coloco o codico? no public static void ou no action performed?

RodrigoKaos

[i]Meu ultimo post nesse tópico :smiley:

Leia essa pesquisa e seja feliz!
http://goo.gl/D7YVfZ[/i]

gpd38

Você deve colocar em prática a ideia anterior, ou seja, utiliza um boolean

Cria uma flag que será booleana

boolean flag = false;

Chama o metodo que gera os valores e coloca uma condição de parada neste metodo quando a flag for ativada / desativada, ou seja, quando o botao for clicado a flag para o metodo chamado

if(flag == false && . . . .){ // continua a gerar os numeros / characteres }

Armazena o valor em uma variavel e mostra no lugar que vc precisa: TEXTAREA / JLABEL / JBUTTON, etc…

String valor = metodo(); jLabel3.setText("" + valor);

Acho que deve ser algo do tipo que vc esta querendo, mas TOME MUITO CUIDADO, pois você pode fazer o metodo ficar em loop e não conseguir para-lo. Seu projeto pode nem abrir se for o caso.

Criado 17 de setembro de 2013
Ultima resposta 19 de set. de 2013
Respostas 9
Participantes 3