我参加了一个编程和解决问题的课程,而我们这个实验室给了我一些麻烦。我们的教授已经写出了游戏的基本代码" Cookie Clicker"并且已经要求我们填写剩下的部分以使游戏真正起作用(如果你还不熟悉游戏,那么你很可能无法提供帮助)。截至目前,游戏已经拥有了所有按钮,但是如果你点击它们中的任何一个,则没有任何反应。如果有人可以告诉我如何更新JLabel,以便按钮每次点击时都显示正确的数字,那么我们将非常感激。需要代码的部分由// Start和// End标记,几乎一直在底部。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class CookieClickerGUI extends javax.swing.JFrame {
//Instance variables
public int cookies;
public int cursors;
public int grandmas;
public int farms;
public int factories;
//Getters
public int getCookies()
{
return cookies;
}
public int getCursors()
{
return cursors;
}
public int getGrandmas()
{
return grandmas;
}
public int getFarms()
{
return farms;
}
public int getFactories()
{
return factories;
}
//Setters
public void setCookies()
{
this.cookies = cookies;
}
public void setCursors()
{
this.cursors = cursors;
}
public void setGrandmas()
{
this.grandmas = grandmas;
}
public void setFarms()
{
this.farms = farms;
}
public void setFactories()
{
this.factories = factories;
}
/**
* Creates new form CookieClickerGUI
*/
public CookieClickerGUI() {
initComponents();
cookieUpdater();
}
public void cookieUpdater() {
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
//here is your action
//update your cookies
}
};
Timer displayTimer = new Timer(1000, listener);
displayTimer.start();
}
/**
* 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">//GEN-BEGIN:initComponents
private void initComponents() {
cookieButton = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
numberOfCookiesLabel = new javax.swing.JLabel();
cursorButton = new javax.swing.JButton();
grandmaButton = new javax.swing.JButton();
farmButton = new javax.swing.JButton();
factoryButton = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
helperLabel = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
grandmaLabel = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
farmsLabel = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
factoriesLabel = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
helperCostLabel = new javax.swing.JLabel();
grandmaCostLabel = new javax.swing.JLabel();
farmsCostLabel = new javax.swing.JLabel();
factoriesCostLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setResizable(false);
cookieButton.setText("Cookie");
cookieButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cookieButtonActionPerformed(evt);
}
});
jLabel1.setText("Cookies:");
numberOfCookiesLabel.setText("0");
cursorButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cursorButtonActionPerformed(evt);
}
});
grandmaButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
grandmaButtonActionPerformed(evt);
}
});
farmButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
farmButtonActionPerformed(evt);
}
});
factoryButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
factoryButtonActionPerformed(evt);
}
});
jLabel2.setText("Number of Helpers:");
helperLabel.setText("0");
jLabel4.setText("Number of Grandmas:");
grandmaLabel.setText("0");
jLabel6.setText("Number of Farms:");
farmsLabel.setText("0");
jLabel8.setText("Number of Factories:");
factoriesLabel.setText("0");
jLabel3.setText("Cost:");
jLabel5.setText("Cost:");
jLabel7.setText("Cost:");
jLabel9.setText("Cost:");
helperCostLabel.setText("0");
grandmaCostLabel.setText("0");
farmsCostLabel.setText("0");
factoriesCostLabel.setText("0");
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.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(numberOfCookiesLabel))
.addComponent(cookieButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(cursorButton, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(helperLabel))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(helperCostLabel))))
.addGroup(layout.createSequentialGroup()
.addComponent(grandmaButton, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(grandmaLabel))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(grandmaCostLabel))))
.addGroup(layout.createSequentialGroup()
.addComponent(farmButton, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(farmsLabel))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(farmsCostLabel))))
.addGroup(layout.createSequentialGroup()
.addComponent(factoryButton, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel8)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(factoriesLabel))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel9)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(factoriesCostLabel)))))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(numberOfCookiesLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(cursorButton, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(helperLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(helperCostLabel))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(grandmaButton, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(grandmaLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(grandmaCostLabel)))))
.addComponent(cookieButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(farmButton, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(farmsLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel7)
.addComponent(farmsCostLabel))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(factoryButton, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel8)
.addComponent(factoriesLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel9)
.addComponent(factoriesCostLabel))))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
//Start
private void cookieButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cookieButtonActionPerformed
cookies = cookies + 1;
}//GEN-LAST:event_cookieButtonActionPerformed
private void cursorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cursorButtonActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_cursorButtonActionPerformed
private void grandmaButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_grandmaButtonActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_grandmaButtonActionPerformed
private void farmButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_farmButtonActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_farmButtonActionPerformed
private void factoryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_factoryButtonActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_factoryButtonActionPerformed
//End
/**
* @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(CookieClickerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(CookieClickerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(CookieClickerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(CookieClickerGUI.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 CookieClickerGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton cookieButton;
private javax.swing.JButton cursorButton;
private javax.swing.JLabel factoriesCostLabel;
private javax.swing.JLabel factoriesLabel;
private javax.swing.JButton factoryButton;
private javax.swing.JButton farmButton;
private javax.swing.JLabel farmsCostLabel;
private javax.swing.JLabel farmsLabel;
private javax.swing.JButton grandmaButton;
private javax.swing.JLabel grandmaCostLabel;
private javax.swing.JLabel grandmaLabel;
private javax.swing.JLabel helperCostLabel;
private javax.swing.JLabel helperLabel;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JLabel numberOfCookiesLabel;
// End of variables declaration//GEN-END:variables
}