我做了一个图片拼图游戏。实际上我使用Netbeans 8.2 GUI Builder来创建游戏,我做了Timer
并移动。
但我很困惑如何改变图像。我在各个网站上搜索过但没有帮助。
这是我的代码:
package projetu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.Timer;
import static projetu.fasil.cont;
public class t02 extends javax.swing.JFrame {
static int cont = 0;
int i = 0;
private Timer timer;
private int currentSegundo = 0;
private int currentMinuto = 0;
private int currentHora = 0;
private int velocidade = 1000;
public t02() {
initComponents();
b9.setVisible(false);
}
public void contamuda() {
txtconta.setText(String.valueOf(cont++));
}
private void iniciarContagem() {
ActionListener action;
action = new ActionListener() {
public void actionPerformed(ActionEvent e) {
currentSegundo++;
if (currentSegundo == 60) {
currentMinuto++;
currentSegundo = 0;
}
if (currentMinuto == 60) {
currentHora++;
currentMinuto = 0;
}
String hr = currentHora <= 9 ? "0" + currentHora : currentHora + "";
String min = currentMinuto <= 9 ? "0" + currentMinuto : currentMinuto + "";
String seg = currentSegundo <= 9 ? "0" + currentSegundo : currentSegundo + "";
tempu.setText(hr + ":" + min + ":" + seg);
}
};
this.timer = new Timer(velocidade, action);
this.timer.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">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
b1 = new javax.swing.JButton();
b2 = new javax.swing.JButton();
b3 = new javax.swing.JButton();
b4 = new javax.swing.JButton();
b5 = new javax.swing.JButton();
b6 = new javax.swing.JButton();
b7 = new javax.swing.JButton();
b8 = new javax.swing.JButton();
b9 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
txtconta = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
tempu = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jLabel3 = new javax.swing.JLabel();
jPanel3 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setLayout(new java.awt.GridLayout(3, 3));
b1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais/t11.gif"))); // NOI18N
b1.setBorder(null);
b1.setBorderPainted(false);
b1.setContentAreaFilled(false);
b1.setFocusable(false);
b1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b1ActionPerformed(evt);
}
});
jPanel1.add(b1);
b2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais/t12.gif"))); // NOI18N
b2.setBorder(null);
b2.setBorderPainted(false);
b2.setContentAreaFilled(false);
b2.setFocusable(false);
b2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b2ActionPerformed(evt);
}
});
jPanel1.add(b2);
b3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais/t13.gif"))); // NOI18N
b3.setBorder(null);
b3.setBorderPainted(false);
b3.setContentAreaFilled(false);
b3.setFocusable(false);
b3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b3ActionPerformed(evt);
}
});
jPanel1.add(b3);
b4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais/t14.gif"))); // NOI18N
b4.setBorder(null);
b4.setBorderPainted(false);
b4.setContentAreaFilled(false);
b4.setFocusable(false);
b4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b4ActionPerformed(evt);
}
});
jPanel1.add(b4);
b5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais/t15.gif"))); // NOI18N
b5.setBorder(null);
b5.setBorderPainted(false);
b5.setContentAreaFilled(false);
b5.setFocusable(false);
b5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b5ActionPerformed(evt);
}
});
jPanel1.add(b5);
b6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais/t16.gif"))); // NOI18N
b6.setBorder(null);
b6.setBorderPainted(false);
b6.setContentAreaFilled(false);
b6.setFocusable(false);
b6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b6ActionPerformed(evt);
}
});
jPanel1.add(b6);
b7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais/t17.gif"))); // NOI18N
b7.setBorder(null);
b7.setBorderPainted(false);
b7.setContentAreaFilled(false);
b7.setFocusable(false);
b7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b7ActionPerformed(evt);
}
});
jPanel1.add(b7);
b8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais/t18.gif"))); // NOI18N
b8.setBorder(null);
b8.setBorderPainted(false);
b8.setContentAreaFilled(false);
b8.setFocusable(false);
b8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b8ActionPerformed(evt);
}
});
jPanel1.add(b8);
b9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais/t19.gif"))); // NOI18N
b9.setBorder(null);
b9.setBorderPainted(false);
b9.setContentAreaFilled(false);
b9.setFocusable(false);
b9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b9ActionPerformed(evt);
}
});
jPanel1.add(b9);
jLabel1.setFont(new java.awt.Font("Trebuchet MS", 1, 36)); // NOI18N
jLabel1.setText("Muda :");
txtconta.setFont(new java.awt.Font("Trebuchet MS", 1, 36)); // NOI18N
txtconta.setText("0");
jLabel2.setFont(new java.awt.Font("Trebuchet MS", 1, 36)); // NOI18N
jLabel2.setText("Tempu =");
tempu.setFont(new java.awt.Font("Trebuchet MS", 1, 36)); // NOI18N
tempu.setText("00:00:00");
jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tais01/t02_lbl.jpg"))); // NOI18N
jLabel3.setAutoscrolls(true);
jLabel3.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
jLabel3.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(0, 89, Short.MAX_VALUE))
);
jButton1.setFont(new java.awt.Font("Trebuchet MS", 1, 18)); // NOI18N
jButton1.setText("Komesa");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton3.setFont(new java.awt.Font("Trebuchet MS", 1, 18)); // NOI18N
jButton3.setText("Valor");
jButton4.setFont(new java.awt.Font("Trebuchet MS", 1, 18)); // NOI18N
jButton4.setText("Sai ");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton2.setFont(new java.awt.Font("Trebuchet MS", 1, 18)); // NOI18N
jButton2.setText("Hili Imagen");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton4)
.addContainerGap())
);
jLabel4.setFont(new java.awt.Font("Trebuchet MS", 1, 36)); // NOI18N
jLabel4.setText("Game puzzle tais no uma adat iha Timor Leste");
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)
.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtconta, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(52, 52, 52)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(tempu))
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 639, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 0, Short.MAX_VALUE))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 56, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 413, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 52, Short.MAX_VALUE)
.addComponent(txtconta, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(tempu, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
pack();
}// </editor-fold>
private void b1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contamuda();
if (b2.isVisible() == false) {
b2.setText(b1.getText());
b2.setIcon(b1.getIcon());
b1.setVisible(false);
b2.setVisible(true);
}
if (b4.isVisible() == false) {
b4.setText(b1.getText());
b4.setIcon(b1.getIcon());
b1.setVisible(false);
b4.setVisible(true);
}
}
private void b2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contamuda();
if (b1.isVisible() == false) {
b1.setText(b2.getText());
b1.setIcon(b2.getIcon());
b2.setVisible(false);
b1.setVisible(true);
}
if (b3.isVisible() == false) {
b3.setText(b2.getText());
b3.setIcon(b2.getIcon());
b2.setVisible(false);
b3.setVisible(true);
}
if (b5.isVisible() == false) {
b5.setText(b2.getText());
b5.setIcon(b2.getIcon());
b2.setVisible(false);
b5.setVisible(true);
}
}
private void b3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contamuda();
if (b2.isVisible() == false) {
b2.setText(b3.getText());
b2.setIcon(b3.getIcon());
b3.setVisible(false);
b2.setVisible(true);
}
if (b6.isVisible() == false) {
b6.setText(b3.getText());
b6.setIcon(b3.getIcon());
b3.setVisible(false);
b6.setVisible(true);
}
}
private void b4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contamuda();
if (b1.isVisible() == false) {
b1.setText(b4.getText());
b1.setIcon(b4.getIcon());
b4.setVisible(false);
b1.setVisible(true);
}
if (b5.isVisible() == false) {
b5.setText(b4.getText());
b5.setIcon(b4.getIcon());
b4.setVisible(false);
b5.setVisible(true);
}
if (b7.isVisible() == false) {
b7.setText(b4.getText());
b7.setIcon(b4.getIcon());
b4.setVisible(false);
b7.setVisible(true);
}
}
private void b5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contamuda();
if (b2.isVisible() == false) {
b2.setText(b5.getText());
b2.setIcon(b5.getIcon());
b5.setVisible(false);
b2.setVisible(true);
}
if (b4.isVisible() == false) {
b4.setText(b4.getText());
b4.setIcon(b4.getIcon());
b5.setVisible(false);
b4.setVisible(true);
}
if (b6.isVisible() == false) {
b6.setText(b5.getText());
b6.setIcon(b5.getIcon());
b5.setVisible(false);
b6.setVisible(true);
}
if (b8.isVisible() == false) {
b8.setText(b5.getText());
b8.setIcon(b5.getIcon());
b5.setVisible(false);
b8.setVisible(true);
}
}
private void b6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contamuda();
if (b9.isVisible() == false) {
b9.setText(b6.getText());
b9.setIcon(b6.getIcon());
b6.setVisible(false);
b9.setVisible(true);
}
if (b3.isVisible() == false) {
b3.setText(b6.getText());
b3.setIcon(b6.getIcon());
b6.setVisible(false);
b3.setVisible(true);
}
if (b5.isVisible() == false) {
b5.setText(b6.getText());
b5.setIcon(b6.getIcon());
b6.setVisible(false);
b5.setVisible(true);
}
}
private void b7ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contamuda();
if (b4.isVisible() == false) {
b4.setText(b7.getText());
b4.setIcon(b7.getIcon());
b7.setVisible(false);
b4.setVisible(true);
}
if (b8.isVisible() == false) {
b8.setText(b7.getText());
b8.setIcon(b7.getIcon());
b7.setVisible(false);
b8.setVisible(true);
}
}
private void b8ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contamuda();
if (b5.isVisible() == false) {
b5.setText(b8.getText());
b5.setIcon(b8.getIcon());
b8.setVisible(false);
b5.setVisible(true);
}
if (b7.isVisible() == false) {
b7.setText(b7.getText());
b7.setIcon(b7.getIcon());
b8.setVisible(false);
b7.setVisible(true);
}
if (b9.isVisible() == false) {
b9.setText(b8.getText());
b9.setIcon(b8.getIcon());
b8.setVisible(false);
b9.setVisible(true);
}
}
private void b9ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contamuda();
if (b6.isVisible() == false) {
b6.setText(b9.getText());
b6.setIcon(b9.getIcon());
b9.setVisible(false);
b6.setVisible(true);
}
if (b8.isVisible() == false) {
b8.setText(b9.getText());
b8.setIcon(b9.getIcon());
b9.setVisible(false);
b8.setVisible(true);
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
iniciarContagem();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
new hili_imagen().setVisible(true);
dispose();
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
new game().setVisible(true);
dispose();
}
/**
* @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(t02.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(t02.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(t02.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(t02.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new t02().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton b1;
private javax.swing.JButton b2;
private javax.swing.JButton b3;
private javax.swing.JButton b4;
private javax.swing.JButton b5;
private javax.swing.JButton b6;
private javax.swing.JButton b7;
private javax.swing.JButton b8;
private javax.swing.JButton b9;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JLabel tempu;
private javax.swing.JLabel txtconta;
// End of variables declaration
}