O programa é um exercício de uma lista que tirei da internet.
Funciona assim:
Uma caixa de texto deve ler o número de vagas disponíveis de um estacionamento. Outra caixa de texto iniciando-se com o valor da caixa de texto anterior deve armasenar a quantidade de vagas disponíveis e outra iniciando-se com 0, as ocupadas.
Ao entrar um veículo, um sensor representado por um botão deverá incrementar o número de vagas ocupadas e redusir em uma unidade as vagas disponíveis. Tal valor deve ser atualisado nas respectivas caixas de texto. E se sair um veículo, deverá ser acionado outro botão, o mesmo deverá ser feito só que ao envez de incrementar as vagas ocupadas, redusirá e ao envez de reduzir as disponíveis, encrementará.
Fazendo isto, sabe-se as vagas totais, desponíveis e ocupadas em um estacionamento.
Eu fiz em netbeans, tentei de tudo, alterar de private para public, mudar a lógica diversas vezes, porêm ao apertar um botão(pela primeira vez), o valor da caixas caixas de texto são zerados e não entedi o porque disto.
package estacionamentointeligente;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
/**
* The application's main frame.
*/
public class EstacionamentointeligenteView extends FrameView {
String svt, svd, svo;
int nvt, nvo, nvd;
public EstacionamentointeligenteView(SingleFrameApplication app) {
super(app);
initComponents();
// status bar initialization - message timeout, idle icon and busy animation, etc
ResourceMap resourceMap = getResourceMap();
int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
messageTimer = new Timer(messageTimeout, new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
messageTimer.setRepeats(false);
int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
for (int i = 0; i < busyIcons.length; i++) {
busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
}
busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
public void actionPerformed(ActionEvent e) {
busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
}
});
idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
// connecting action tasks to status bar via TaskMonitor
TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
String propertyName = evt.getPropertyName();
if ("started".equals(propertyName)) {
if (!busyIconTimer.isRunning()) {
busyIconIndex = 0;
busyIconTimer.start();
}
} else if ("done".equals(propertyName)) {
busyIconTimer.stop();
} else if ("message".equals(propertyName)) {
String text = (String)(evt.getNewValue());
messageTimer.restart();
} else if ("progress".equals(propertyName)) {
int value = (Integer)(evt.getNewValue());
}
}
});
}
@Action
public void showAboutBox() {
if (aboutBox == null) {
JFrame mainFrame = EstacionamentointeligenteApp.getApplication().getMainFrame();
aboutBox = new EstacionamentointeligenteAboutBox(mainFrame);
aboutBox.setLocationRelativeTo(mainFrame);
}
EstacionamentointeligenteApp.getApplication().show(aboutBox);
}
/** 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() {
mainPanel = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
menuBar = new javax.swing.JMenuBar();
javax.swing.JMenu fileMenu = new javax.swing.JMenu();
javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
javax.swing.JMenu helpMenu = new javax.swing.JMenu();
javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
mainPanel.setName("mainPanel"); // NOI18N
org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(estacionamentointeligente.EstacionamentointeligenteApp.class).getContext().getResourceMap(EstacionamentointeligenteView.class);
jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
jButton1.setName("jButton1"); // NOI18N
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
jButton2.setName("jButton2"); // NOI18N
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
jTextField1.setName("jTextField1"); // NOI18N
jTextField1.addCaretListener(new javax.swing.event.CaretListener() {
public void caretUpdate(javax.swing.event.CaretEvent evt) {
jTextField1CaretUpdate(evt);
}
});
jTextField1.addInputMethodListener(new java.awt.event.InputMethodListener() {
public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
jTextField1CaretPositionChanged(evt);
}
public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
}
});
jTextField1.addAncestorListener(new javax.swing.event.AncestorListener() {
public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
jTextField1AncestorMoved(evt);
}
public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
}
public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
}
});
jTextField2.setBackground(resourceMap.getColor("jTextField2.background")); // NOI18N
jTextField2.setEditable(false);
jTextField2.setText(resourceMap.getString("jTextField2.text")); // NOI18N
jTextField2.setDisabledTextColor(resourceMap.getColor("jTextField2.disabledTextColor")); // NOI18N
jTextField2.setName("jTextField2"); // NOI18N
jTextField3.setBackground(resourceMap.getColor("jTextField3.background")); // NOI18N
jTextField3.setEditable(false);
jTextField3.setDisabledTextColor(resourceMap.getColor("jTextField3.disabledTextColor")); // NOI18N
jTextField3.setName("jTextField3"); // NOI18N
jTextField3.setSelectedTextColor(resourceMap.getColor("jTextField3.selectedTextColor")); // NOI18N
jTextField3.setText(""+nvo);
jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
jLabel1.setName("jLabel1"); // NOI18N
jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
jLabel2.setName("jLabel2"); // NOI18N
jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
jLabel3.setName("jLabel3"); // NOI18N
javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
mainPanel.setLayout(mainPanelLayout);
mainPanelLayout.setHorizontalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel3)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jTextField1)
.addComponent(jTextField2)
.addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
mainPanelLayout.setVerticalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addGap(74, 74, 74)
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(mainPanelLayout.createSequentialGroup()
.addGap(62, 62, 62)
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addGap(18, 18, 18)
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addGap(18, 18, 18)
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))))
.addContainerGap(38, Short.MAX_VALUE))
);
menuBar.setName("menuBar"); // NOI18N
fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
fileMenu.setName("fileMenu"); // NOI18N
javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(estacionamentointeligente.EstacionamentointeligenteApp.class).getContext().getActionMap(EstacionamentointeligenteView.class, this);
exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
exitMenuItem.setName("exitMenuItem"); // NOI18N
fileMenu.add(exitMenuItem);
menuBar.add(fileMenu);
helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
helpMenu.setName("helpMenu"); // NOI18N
aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
aboutMenuItem.setName("aboutMenuItem"); // NOI18N
helpMenu.add(aboutMenuItem);
menuBar.add(helpMenu);
setComponent(mainPanel);
setMenuBar(menuBar);
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
nvo=nvo+1;
nvd=nvd-1;
jTextField2.setText(""+nvd);
jTextField3.setText(""+nvo);
// TODO add your handling code here:
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
nvo=nvo-1;
nvd=nvd+1;
jTextField2.setText(""+nvd);
jTextField3.setText(""+nvo);
}
private void jTextField1AncestorMoved(javax.swing.event.AncestorEvent evt) {
}
private void jTextField1CaretPositionChanged(java.awt.event.InputMethodEvent evt) {
// TODO add your handling code here:
}
private void jTextField1CaretUpdate(javax.swing.event.CaretEvent evt) {
svt = jTextField1.getText();
svd=svt;
svo="0";
nvt = Integer.parseInt(svt);
jTextField2.setText(svd);
jTextField3.setText(svo);
// TODO add your handling code here:
}
// Variables declaration - do not modify
public javax.swing.JButton jButton1;
public javax.swing.JButton jButton2;
public javax.swing.JLabel jLabel1;
public javax.swing.JLabel jLabel2;
public javax.swing.JLabel jLabel3;
public javax.swing.JTextField jTextField1;
public javax.swing.JTextField jTextField2;
public javax.swing.JTextField jTextField3;
public javax.swing.JPanel mainPanel;
public javax.swing.JMenuBar menuBar;
// End of variables declaration
private final Timer messageTimer;
private final Timer busyIconTimer;
private final Icon idleIcon;
private final Icon[] busyIcons = new Icon[15];
private int busyIconIndex = 0;
private JDialog aboutBox;
}