-1
エラー:イメージでJButtonをシャッフルする方法は?ここ
for(int h = 0; h <= picture; h++)
{
list.add(picture[h]);
picture.addActionListener(this);
}
Collections.shuffle(list);
これは私の完全なコードです:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.Timer;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.Collections;
public class PrimeMinister extends javax.swing.JFrame {
private Timer t; //declare timer
ArrayList list = new ArrayList();
/**
* Creates new form PrimeMinister
*/
public PrimeMinister() {
super("Prime Minister");
initComponents();
}
private int currCounter; //declare for boolean true and continue another statement
private void wrongChoice(int counter){
switch(counter){
case 0: jTextField1.setText("Wrong!! Please try again! Who is the 1st Prime Minister of Malaysia?"); break;
case 1: jTextField1.setText("Wrong!! Please try again! Who is the 2nd Prime Minister of Malaysia?"); break;
case 2: jTextField1.setText("Wrong!! Please try again! Who is the 3rd Prime Minister of Malaysia?"); break;
case 3: jTextField1.setText("Wrong!! Please try again! Who is the 4th Prime Minister of Malaysia?"); break;
case 4: jTextField1.setText("Wrong!! Please try again! Who is the 5th Prime Minister of Malaysia?"); break;
case 5: jTextField1.setText("Wrong!! Please try again! Who is the 6th Prime Minister of Malaysia?"); break;
}
}// wrong choice if click different Prime Minister
/**
* 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() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
lbl_Timer = new javax.swing.JTextField();
jTextField1 = new javax.swing.JTextField();
jButton9 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel1.setText("Find the Prime Minister of Malaysia");
jLabel2.setFont(new java.awt.Font("Tahoma", 2, 14)); // NOI18N
jLabel2.setText("Can you list the Prime Ministers of Malaysia in chronological order?");
jButton1.setIcon(new javax.swing.ImageIcon("C:\\Users\\user_PC\\Desktop\\PM\\najib.jpg")); // NOI18N
jButton1.setName("jButton[1]"); // NOI18N
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setIcon(new javax.swing.ImageIcon("C:\\Users\\user_PC\\Desktop\\PM\\T_PakLah.jpg")); // NOI18N
jButton2.setName("jButton[2]"); // NOI18N
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setIcon(new javax.swing.ImageIcon("C:\\Users\\user_PC\\Desktop\\PM\\tarahman.jpg")); // NOI18N
jButton3.setName("jButton[3]"); // NOI18N
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setIcon(new javax.swing.ImageIcon("C:\\Users\\user_PC\\Desktop\\PM\\Tun_Hussein_Onn_portrait.jpg")); // NOI18N
jButton4.setName("jButton[4]"); // NOI18N
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton5.setIcon(new javax.swing.ImageIcon("C:\\Users\\user_PC\\Desktop\\PM\\TunMahathir4th.jpg")); // NOI18N
jButton5.setName("jButton[5]"); // NOI18N
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
jButton6.setIcon(new javax.swing.ImageIcon("C:\\Users\\user_PC\\Desktop\\PM\\TunRazak2nd.jpg")); // NOI18N
jButton6.setText("jButton6");
jButton6.setInheritsPopupMenu(true);
jButton6.setName("jButton[6]"); // NOI18N
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});
jButton7.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jButton7.setText("New Game");
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});
jButton8.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jButton8.setText("Quit");
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton8ActionPerformed(evt);
}
});
jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jLabel3.setText(" To start the game: Clicks on the image of the first Prime Minister of Malaysia!");
jLabel4.setText("Timer:");
lbl_Timer.setText("00");
lbl_Timer.setToolTipText("");
lbl_Timer.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
lbl_TimerActionPerformed(evt);
}
});
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jButton9.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jButton9.setText("Reset");
jButton9.setToolTipText("");
jButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton9ActionPerformed(evt);
}
});
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()
.addGap(57, 57, 57)
.addComponent(jLabel2))
.addGroup(layout.createSequentialGroup()
.addGap(101, 101, 101)
.addComponent(jLabel1))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 1, Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(15, 15, 15)
.addComponent(jLabel3))
.addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(31, 31, 31)
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(lbl_Timer, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(127, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(20, 20, 20)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton7)
.addComponent(jButton8)
.addComponent(jLabel4)
.addComponent(lbl_Timer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton9))
.addContainerGap(41, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void lbl_TimerActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(currCounter == 1){
jTextField1.setText("Correct!! this is Tun Abdul Razak 2nd Prime Minister of Malaysia");
currCounter = 2; //continue next 3rd Prime Minister
}
else {
wrongChoice(currCounter);//refer to switch case statement
}
}
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.dispose();
}
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
for(int h=0;h<=picture;h++)
{
list.add(picture[h]);
picture.addActionListener(this);
}
Collections.shuffle(list);
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(currCounter == 3){
currCounter = 4; //continue next 5th Prime Minister
jTextField1.setText("Correct!! this is Tun DR. Mahathir 4th Prime Minister of Malaysia");
}
else
{
wrongChoice(currCounter);//refer to switch case statement
}
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(currCounter == 5){
currCounter = 6; //Final Prime Minister
jTextField1.setText("Correct!! this is Najib Razak 6th Prime Minister of Malaysia");
t.stop();
}
else
{
wrongChoice(currCounter); //refer to switch case statement
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(currCounter == 4){
currCounter = 5; //continue next 6th Prime Minister
jTextField1.setText("Correct!! this is Abdullah Ahmad Badawi 5th Prime Minister of Malaysia");
}
else
{
wrongChoice(currCounter);//refer to switch case statement
}
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
currCounter = 0;
System.out.println(currCounter); // debug: show current pm
t = new Timer(1000, new ActionListener(){
int currTime = 0;
@Override
public void actionPerformed(ActionEvent e){
lbl_Timer.setText(currTime + "");
currTime ++;
}
});
t.start();
if(currCounter == 0){
currCounter = 1;//continue next 2nd Prime Minister
jTextField1.setText("Correct!! this is Tunku Abdul Rahman 1st Prime Minister of Malaysia");
}
else{
wrongChoice(currCounter);//refer to switch case statement
}
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(currCounter == 2){
currCounter = 3;//continue next 4rd Prime Minister
jTextField1.setText("Correct!! this is Tun Hussein Onn 3rd Prime Minister of Malaysia");
}
else{
wrongChoice(currCounter);//refer to switch case statement
}
}
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jTextField1.setText("");
lbl_Timer.setText("");
}
/**
* @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(PrimeMinister.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(PrimeMinister.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(PrimeMinister.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(PrimeMinister.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
// Original array
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PrimeMinister().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField lbl_Timer;
// End of variables declaration
private JButton picture[]= {jButton1, jButton2, jButton3, jButton4,
jButton5, jButton6};
}
エラーですか?これを[mcve]に減らしてください。 –