Gente comecei a programar em JAVA recentemente, demorei um pouco para pegar a matéria e agora estou um pouco atolado. Meu professor de JAVA solicitou que fizéssemos um código em JAVA e informou que o código seria parecido com a prova.
Bem… O exercício proposto pelo professor é o seguinte:
Faça uma aplicação em JAVA que guarde informações de 10 Policiais e Bandidos (use vetores para guardar as informações). A aplicação deverá ter uma tela principal que ira chamar uma tela secundária para registrar policial ou Bandido. Os personagens deveram ter nome, saúde, inteligência, força(Apenas o Policial), destreza(Apenas o Bandido). Programe um botão que mostre as informações dos policiais ou bandidos registrados.
- Crie a classe personagem
- Crie a classe que herda personagem
- Crie uma tela principal
3.1. Crie uma tela secundária que é chamada pela principal para registrar os bandidos
3.2. Crie uma tela secundária que é chamada pela principal para registrar os policiais - Implemente os botões para poder guardar, limpar, mostrar e sair
Eu fui desenvolvendo o programa durante a semana, mas como eu disse no começo eu demorei um pouco e fiquei atolado e agora eu preciso de ajuda no meu programa. Quando eu clico no botão Criar ou Mostrar ele não faz as ações, também não dá erro. Simplesmente não faz. Será que vocês conseguem me ajudar a solucionar esse problema.
Eis o código que eu fiz.
CLASSE PERSONAGEM
public abstract class Personagem
{
private String name;
private int atrPV, atrINT;
public Personagem() {}
public Personagem(String name, int atrPV, int atrINT)
{
this.name = name;
this.atrPV = atrPV;
this.atrINT = atrINT;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
public void setAtrPV( int atrPV)
{
this.atrPV = atrPV;
}
public int getAtrPV()
{
return this.atrPV;
}
public void setAtrINT(int atrINT)
{
this.atrINT = atrINT;
}
public int getAtrINT()
{
return this.atrINT;
}
}
CLASSE BANDIDO
public class Bandido extends Personagem
{
private int atrDES;
public Bandido(){ super(); }
public Bandido(String name, int atrPV, int atrINT ,int atrDES)
{
super(name, atrPV, atrINT);
this.atrDES = atrDES;
}
public void setAtrDES(int atrDES)
{
this.atrDES = atrDES;
}
public int getAtrDES()
{
return this.atrDES;
}
}
CLASSE POLICIAL
public class Policial extends Personagem
{
private int atrFOR;
public Policial(){ super(); }
public Policial(String name, int atrPV, int atrINT ,int atrFOR)
{
super(name, atrPV, atrINT);
this.atrFOR = atrFOR;
}
public void setAtrFOR(int atrFOR)
{
this.atrFOR = atrFOR;
}
public int getAtrFOR()
{
return this.atrFOR;
}
}
Usamos o NetBeans para criar o Frame
FRAME PRINCIPAL (Não fiz nenhum código, só criei a tela)
package Personagens;
/**
*
* @author Guilherme
*/
public class FramePrincipal extends javax.swing.JFrame
{
/**
* Creates new form FramePrincipal
*/
public FramePrincipal()
{
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() {
Panel = new javax.swing.JPanel();
menuBar = new javax.swing.JMenuBar();
menuRegistrar = new javax.swing.JMenu();
mItemBandido = new javax.swing.JMenuItem();
mItemPolicial = new javax.swing.JMenuItem();
menuSair = new javax.swing.JMenu();
mItemSair = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout PanelLayout = new javax.swing.GroupLayout(Panel);
Panel.setLayout(PanelLayout);
PanelLayout.setHorizontalGroup(
PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
PanelLayout.setVerticalGroup(
PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 279, Short.MAX_VALUE)
);
menuRegistrar.setText("Registar");
mItemBandido.setText("Bandido");
menuRegistrar.add(mItemBandido);
mItemPolicial.setText("Policial");
menuRegistrar.add(mItemPolicial);
menuBar.add(menuRegistrar);
menuSair.setText("Sair");
mItemSair.setText("Sair");
menuSair.add(mItemSair);
menuBar.add(menuSair);
setJMenuBar(menuBar);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @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(FramePrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(FramePrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(FramePrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(FramePrincipal.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 FramePrincipal().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel Panel;
private javax.swing.JMenuItem mItemBandido;
private javax.swing.JMenuItem mItemPolicial;
private javax.swing.JMenuItem mItemSair;
private javax.swing.JMenuBar menuBar;
private javax.swing.JMenu menuRegistrar;
private javax.swing.JMenu menuSair;
// End of variables declaration
}
FRAME BANDIDO
package Personagens;
/**
*
* @author Guilherme
*/
public class FrameBandido extends javax.swing.JFrame {
/**
* Creates new form FrameBandido
*/
private static final Bandido[] bandido = new Bandido[10];
//private final int QUANTIDADE_BANDIDO = 0;
private String name = null;
private int atrPV, atrINT, atrDES = 0;
public FrameBandido()
{
initComponents();
//bandido = new Bandido[QUANTIDADE_BANDIDO];
}
/**
* 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() {
panel = new javax.swing.JPanel();
txLogo = new javax.swing.JLabel();
lbName = new javax.swing.JLabel();
lbPV = new javax.swing.JLabel();
lbINT = new javax.swing.JLabel();
lbDES = new javax.swing.JLabel();
txName = new javax.swing.JTextField();
slPV = new javax.swing.JSlider();
slINT = new javax.swing.JSlider();
slDES = new javax.swing.JSlider();
btnLimpar = new javax.swing.JButton();
btnCriar = new javax.swing.JButton();
btnMostar = new javax.swing.JButton();
btnSair = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
txLogo.setFont(new java.awt.Font("Tahoma", 0, 30)); // NOI18N
txLogo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
txLogo.setText("BANDIDO");
txLogo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
lbName.setText("Nome:");
lbPV.setText("Saúde:");
lbINT.setText("Inteligência:");
lbDES.setText("Destreza");
slPV.setValue(0);
slINT.setValue(0);
slDES.setMaximum(20);
slDES.setValue(0);
btnLimpar.setText("Limpar");
btnLimpar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnLimparActionPerformed(evt);
}
});
btnCriar.setText("Criar");
btnCriar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCriarActionPerformed(evt);
}
});
btnMostar.setText("Mostar");
btnMostar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnMostarActionPerformed(evt);
}
});
btnSair.setText("Sair");
btnSair.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSairActionPerformed(evt);
}
});
javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
panel.setLayout(panelLayout);
panelLayout.setHorizontalGroup(
panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(txLogo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, panelLayout.createSequentialGroup()
.addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(lbDES, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(lbPV, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(lbINT, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)
.addComponent(lbName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txName, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
.addComponent(slPV, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
.addComponent(slINT, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
.addComponent(slDES, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(btnMostar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE)
.addComponent(btnLimpar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnCriar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnSair, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addContainerGap(13, Short.MAX_VALUE))
);
panelLayout.setVerticalGroup(
panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(txLogo)
.addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(panelLayout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lbName, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txName, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnCriar, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelLayout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(lbPV, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelLayout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(slPV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGroup(panelLayout.createSequentialGroup()
.addGap(58, 58, 58)
.addComponent(btnLimpar, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lbINT, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(slINT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnMostar, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lbDES, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(slDES, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnSair, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
pack();
}// </editor-fold>
private void btnCriarActionPerformed(java.awt.event.ActionEvent evt) {
criar();
}
private void btnLimparActionPerformed(java.awt.event.ActionEvent evt) {
limpar();
}
private void btnMostarActionPerformed(java.awt.event.ActionEvent evt) {
mostar();
}
private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {
sair();
}
/**
* @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(FrameBandido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(FrameBandido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(FrameBandido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(FrameBandido.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 FrameBandido().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnCriar;
private javax.swing.JButton btnLimpar;
private javax.swing.JButton btnMostar;
private javax.swing.JButton btnSair;
private javax.swing.JLabel lbDES;
private javax.swing.JLabel lbINT;
private javax.swing.JLabel lbName;
private javax.swing.JLabel lbPV;
private javax.swing.JPanel panel;
private javax.swing.JSlider slDES;
private javax.swing.JSlider slINT;
private javax.swing.JSlider slPV;
private javax.swing.JLabel txLogo;
private javax.swing.JTextField txName;
// End of variables declaration
public void criar()
{
for(int cont = 0; cont < bandido.length; cont++)
{
this.name = txName.getText();
bandido[cont] = new Bandido(null, 0, 0, 0);
bandido[cont].setName(name);
/*bandido[cont].setName(txName.getText());
bandido[cont].setAtrPV(slPV.getValue());*/
/*if(bandido[cont] != null)
{
bandido[cont].setName(txName.getText());
}
else
{
System.out.println("O Objeto é NULO");
}*/
/*bandido[cont].setName();
bandido[cont].getAtrPV(slPV.getValue());
bandido[cont].getAtrINT(slINT.getValue());
bandido[cont].getAtrDES(slDES.getValue());*/
}
}
public void mostar()
{
for (int cont = 0; cont < bandido.length; cont++)
{
bandido[cont] = new Bandido(null, 0, 0, 0);
//this.txName.setText(name);
//this.slPV.setValue(bandido[cont].getAtrPV());
//this.txName.getName();
this.txName.setText(name);
}
}
public void limpar()
{
this.txName.setText(null);
this.slPV.setValue(0);
this.slINT.setValue(0);
this.slDES.setValue(0);
}
public void sair()
{
System.exit(0);
}
}
Então gente… Será que vocês conseguem ajudar a solucionar meu problema antes que as provas cheguem e eu continue empacado?
Sou grato pela ajuda de vocês.