【发布时间】:2021-04-13 03:55:09
【问题描述】:
我已经开始玩一个非常基础的记忆游戏了。
private void tbtnCard3ActionPerformed(java.awt.event.ActionEvent evt) {
tbtnCard3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Card3Logo.png")));
if(tbtnCard5.isSelected()){
score++;
lblScore.setText(""+score);
}
}
private void tbtnCard4ActionPerformed(java.awt.event.ActionEvent evt) {
tbtnCard4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Card7EWaste.png")));
if(tbtnCard7.isSelected()){
score++;
lblScore.setText(""+score);
}
}
private void tbtnCard5ActionPerformed(java.awt.event.ActionEvent evt) {
tbtnCard5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Card3Logo.png")));
if(tbtnCard3.isSelected()){
score++;
lblScore.setText(""+score);
}
}
使用 Java Swing,我用我需要的图像设置了图标。在底部我有一个按钮调用 start 以便首先显示所有按钮,直到按下 start btn。
private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {
tbtnCard1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/BlankImage.png")));
tbtnCard2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/BlankImage.png")));
tbtnCard3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/BlankImage.png")));
tbtnCard4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/BlankImage.png")));
tbtnCard5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/BlankImage.png")));
tbtnCard6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/BlankImage.png")));
tbtnCard7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/BlankImage.png")));
tbtnCard8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/BlankImage.png")));
tbtnCard9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/BlankImage.png")));
tbtnCard10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/BlankImage.png")));
tbtnCard11.setIcon(new javax.swing.ImageIcon(getClass().getResource("/BlankImage.png")));
tbtnCard12.setIcon(new javax.swing.ImageIcon(getClass().getResource("/BlankImage.png")));
tbtnCard13.setIcon(new javax.swing.ImageIcon(getClass().getResource("/BlankImage.png")));
tbtnCard14.setIcon(new javax.swing.ImageIcon(getClass().getResource("/BlankImage.png")));
tbtnCard15.setIcon(new javax.swing.ImageIcon(getClass().getResource("/BlankImage.png")));
tbtnCard16.setIcon(new javax.swing.ImageIcon(getClass().getResource("/BlankImage.png")));
tbtnCard17.setIcon(new javax.swing.ImageIcon(getClass().getResource("/BlankImage.png")));
tbtnCard18.setIcon(new javax.swing.ImageIcon(getClass().getResource("/BlankImage.png")));
tbtnCard19.setIcon(new javax.swing.ImageIcon(getClass().getResource("/BlankImage.png")));
tbtnCard20.setIcon(new javax.swing.ImageIcon(getClass().getResource("/BlankImage.png")));
}
我唯一的问题是,当玩家从一组 2 张牌中选择了一张错误的牌后,两张牌都应该回到空白图像。我该怎么办?
【问题讨论】:
-
考虑使用 Swing
Timer在稍有延迟后切换按钮的状态 - for example
标签: java swing user-interface