Bom dia, Pessoal!
Estou fazendo um programa que simula um rádio onde da para se escolher estação e tudo mais.
Até o momento consegui fazer o seguinte código.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tela;
import java.awt.event.MouseEvent;
/**
*
* @author André
*/
public class TelaRadio extends javax.swing.JFrame {
/**
* Creates new form TelaRadio
*/
public TelaRadio() {
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() {
lblEstacao = new javax.swing.JLabel();
lblVolume = new javax.swing.JLabel();
btnMais = new javax.swing.JButton();
btnMenos = new javax.swing.JButton();
btnMaisVolume = new javax.swing.JButton();
btnMenosVolume = new javax.swing.JButton();
btnLigaDesliga = new javax.swing.JToggleButton();
JPanelEstacao = new javax.swing.JPanel();
jtxtEstacao = new javax.swing.JTextField();
jtxtVolume = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
lblEstacao.setText("Estação");
lblVolume.setText("Volume");
btnMais.setText("+");
btnMais.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnMaisActionPerformed(evt);
}
});
btnMenos.setText("-");
btnMaisVolume.setText("+");
btnMenosVolume.setText("-");
btnLigaDesliga.setText("LIGAR");
btnLigaDesliga.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnLigaDesligaActionPerformed(evt);
}
});
JPanelEstacao.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jtxtEstacao.setEditable(false);
jtxtEstacao.setFont(new java.awt.Font("Dialog", 1, 18)); // NOI18N
jtxtEstacao.setHorizontalAlignment(javax.swing.JTextField.CENTER);
jtxtEstacao.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jtxtEstacaoActionPerformed(evt);
}
});
javax.swing.GroupLayout JPanelEstacaoLayout = new javax.swing.GroupLayout(JPanelEstacao);
JPanelEstacao.setLayout(JPanelEstacaoLayout);
JPanelEstacaoLayout.setHorizontalGroup(
JPanelEstacaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jtxtEstacao, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
);
JPanelEstacaoLayout.setVerticalGroup(
JPanelEstacaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jtxtEstacao, javax.swing.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE)
);
jtxtVolume.setEditable(false);
jtxtVolume.setFont(new java.awt.Font("Dialog", 1, 18)); // NOI18N
jtxtVolume.setHorizontalAlignment(javax.swing.JTextField.CENTER);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(27, 27, 27)
.addComponent(lblEstacao, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(24, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnLigaDesliga, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(lblVolume, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(JPanelEstacao, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jtxtVolume))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnMenos, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnMais, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(btnMaisVolume, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnMenosVolume, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGap(33, 33, 33))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(39, 39, 39)
.addComponent(lblEstacao, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(JPanelEstacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(lblVolume, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(btnMais)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnMenos)))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(btnMaisVolume)
.addGap(18, 18, 18)
.addComponent(btnMenosVolume))
.addComponent(jtxtVolume))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
.addComponent(btnLigaDesliga, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(25, 25, 25))
);
pack();
}// </editor-fold>
private void btnMaisActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btnLigaDesligaActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
btnLigaDesliga.setText("DESLIGA");
}
private void jtxtEstacaoActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @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(TelaRadio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TelaRadio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TelaRadio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TelaRadio.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 TelaRadio().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel JPanelEstacao;
private javax.swing.JToggleButton btnLigaDesliga;
private javax.swing.JButton btnMais;
private javax.swing.JButton btnMaisVolume;
private javax.swing.JButton btnMenos;
private javax.swing.JButton btnMenosVolume;
private javax.swing.JTextField jtxtEstacao;
private javax.swing.JTextField jtxtVolume;
private javax.swing.JLabel lblEstacao;
private javax.swing.JLabel lblVolume;
// End of variables declaration
}
Criei na parte de arrastar e clicar no NetBeans
O radio tem a seguinte interface
Ao clicar em Ligar o Botão muda de Texto para Desligar conforme essa parte do código
private void btnLigaDesligaActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
btnLigaDesliga.setText("DESLIGA");
}
Porém eu não sei como fazer para um segundo Clique trocar para a Frase Ligar novamente.
Outra dúvida seria com os botões de mais e menos, no código eu já tenho um incrimento de estação que esta dessa forma
public void incrementarEstacao() {
if (estacao < 110) {
estacao++;
} else {
estacao = 88;
}
}
Como eu poderia aplicar isso ao clicar em + e -, e como eu poderia configurar para ao clicar em Desligar que os text Fields ficassem em branco novamente.
Muito obrigado!