I'm working on a project for school in which I have to create a Milionaire Game.
I created all the graphic part with the GUI builder and now I'm trying to figure out how to make Buttons interact.
I've got a Frame in which I have the Buttons with the prizes and a Panel with a CardLayout which shows a Label with the question and the 4 answer Buttons.
My purpose is to set enabled the next prize button everytime an answer is correct but I can't figure out how to make prizes Buttons and answer Buttons interact.
When a prize Button is clicked I'd like to show in my CardLayout Panel, a new Panel which shows a new question with its buttons, but never reshows one of the previous questions.
Questions are taken randomly from a String ArrayList.
So pratically I need to understand how I can enable prize buttons when an answer is correct and how to create a method that remove a question String from the ArrayList when used, so that next questions would never be equal.
Here are the codes for my Frame class
public class NuovaPartita extends javax.swing.JFrame {
public NuovaPartita() {
initComponents();
getContentPane().setBackground(Color.BLACK);
setLocationRelativeTo(null);
}
private void initComponents() {
quindici = new Premi();
quattordici = new Premi();
tredici = new Premi();
dodici = new Premi();
undici = new Premi();
dieci = new Premi();
nove = new Premi();
otto = new Premi();
sette = new Premi();
sei = new Premi();
cinque = new Premi();
quattro = new Premi();
tre = new Premi();
due = new Premi();
uno = new Premi();
gioco = new javax.swing.JPanel();
domanda1 = new milionarioesame.Domanda1();
domanda2 = new milionarioesame.Domanda1();
domanda3 = new milionarioesame.Domanda1();
domanda4 = new milionarioesame.Domanda1();
domanda5 = new milionarioesame.Domanda1();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("CHI VUOL ESSER MILIONARIO?");
setName("nuovaPartita"); // NOI18N
setResizable(false);
setSize(new java.awt.Dimension(1600, 900));
quindici.setText("1.000.000 €");
quindici.setForeground(Color.RED);
quattordici.setText("500.000 €");
tredici.setText("250.000 €");
dodici.setText("125.000 €");
undici.setText("64.000 €");
dieci.setText("32.000 €");
dieci.setForeground(Color.RED);
nove.setText("16.000 €");
otto.setText("8.000 €");
sette.setText("4.000 €");
sei.setText("2.000 €");
cinque.setText("1.000 €");
cinque.setForeground(Color.RED);
cinque.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cinqueActionPerformed(evt);
}
});
quattro.setText("400 €");
quattro.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
quattroActionPerformed(evt);
}
});
tre.setText("300 €");
tre.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
treActionPerformed(evt);
}
});
due.setText("200 €");
due.setEnabled(true);
due.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
dueActionPerformed(evt);
}
});
uno.setText("100 €");
uno.setEnabled(true);
uno.setBackground(Color.YELLOW);
gioco.setBackground(new java.awt.Color(0, 0, 0));
gioco.setLayout(new java.awt.CardLayout());
gioco.add(domanda1, "card2");
domanda2.dom = domanda2.domande1.get(domanda2.idx2);
gioco.add(domanda2, "card3");
domanda3.dom = domanda3.domande1.get(domanda3.idx3);
gioco.add(domanda3, "card4");
domanda4.dom = domanda4.domande1.get(domanda4.idx4);
gioco.add(domanda4, "card5");
domanda5.dom = domanda5.domande1.get(domanda5.idx5);
gioco.add(domanda5, "card6");
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(tredici, javax.swing.GroupLayout.PREFERRED_SIZE, 420, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(dodici, javax.swing.GroupLayout.PREFERRED_SIZE, 420, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(undici, javax.swing.GroupLayout.PREFERRED_SIZE, 420, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(dieci, javax.swing.GroupLayout.PREFERRED_SIZE, 420, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(nove, javax.swing.GroupLayout.PREFERRED_SIZE, 420, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(otto, javax.swing.GroupLayout.PREFERRED_SIZE, 420, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(sette, javax.swing.GroupLayout.PREFERRED_SIZE, 420, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(sei, javax.swing.GroupLayout.PREFERRED_SIZE, 420, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cinque, javax.swing.GroupLayout.PREFERRED_SIZE, 420, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(quattro, javax.swing.GroupLayout.PREFERRED_SIZE, 420, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tre, javax.swing.GroupLayout.PREFERRED_SIZE, 420, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(due, javax.swing.GroupLayout.PREFERRED_SIZE, 420, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(uno, javax.swing.GroupLayout.PREFERRED_SIZE, 420, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(quindici, javax.swing.GroupLayout.PREFERRED_SIZE, 420, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(quattordici, javax.swing.GroupLayout.PREFERRED_SIZE, 420, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(gioco, javax.swing.GroupLayout.DEFAULT_SIZE, 1145, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(quindici, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(quattordici, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tredici, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(dodici, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(undici, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(dieci, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(nove, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(otto, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(sette, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(6, 6, 6)
.addComponent(sei, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(cinque, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(quattro, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tre, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(due, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(uno, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(49, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(gioco, javax.swing.GroupLayout.DEFAULT_SIZE, 876, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void dueActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
gioco.removeAll();
gioco.add(domanda2);
gioco.repaint();
gioco.revalidate();
}
private void treActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
gioco.removeAll();
gioco.add(domanda3);
gioco.repaint();
gioco.revalidate();
}
private void quattroActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
gioco.removeAll();
gioco.add(domanda4);
gioco.repaint();
gioco.revalidate();
}
private void cinqueActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
gioco.removeAll();
gioco.add(domanda5);
gioco.repaint();
gioco.revalidate();
}
/**
* @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://ift.tt/1cNmMj1
*/
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(NuovaPartita.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NuovaPartita.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NuovaPartita.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NuovaPartita.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 NuovaPartita().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton cinque;
private javax.swing.JButton dieci;
private javax.swing.JButton dodici;
private milionarioesame.Domanda1 domanda1;
private milionarioesame.Domanda1 domanda2;
private milionarioesame.Domanda1 domanda3;
private milionarioesame.Domanda1 domanda4;
private milionarioesame.Domanda1 domanda5;
private javax.swing.JButton due;
private javax.swing.JPanel gioco;
private javax.swing.JButton nove;
private javax.swing.JButton otto;
private javax.swing.JButton quattordici;
private javax.swing.JButton quattro;
private javax.swing.JButton quindici;
private javax.swing.JButton sei;
private javax.swing.JButton sette;
private javax.swing.JButton tre;
private javax.swing.JButton tredici;
private javax.swing.JButton undici;
public javax.swing.JButton uno;
// End of variables declaration
and for my Panel class. PS: I removed the layout generated code to make number of characters fit with Stackoverflow standards.
public class Domanda1 extends javax.swing.JPanel {
public Domanda1() {
initComponents();
}
private void initComponents() {
domanda = new javax.swing.JLabel();
button1 = new Button();
button4 = new Button();
button3 = new Button();
button2 = new Button();
haiPerso = new javax.swing.JLabel();
setBackground(new java.awt.Color(0, 0, 0));
setPreferredSize(new java.awt.Dimension(1160, 890));
domanda.setText("<html>" + dom + "</html>");
domanda.setFont(cms);
domanda.setForeground(Color.WHITE);
domanda.setHorizontalAlignment(JLabel.CENTER);
button1.setText("button1");
if (dom.equals(d1)) //equals per le stringhe, NON ==
{
button1.setText(r11);
};
if (dom.equals(d2))
{
button1.setText(r21);
};
if (dom.equals(d3))
{
button1.setText(r31);
};
if (dom.equals(d4))
{
button1.setText(r41);
};
if (dom.equals(d5))
{
button1.setText(r51);
};
if (dom.equals(d6))
{
button1.setText(r61);
};
if (dom.equals(d7))
{
button1.setText(r71);
};
if (dom.equals(d8))
{
button1.setText(r81);
};
if (dom.equals(d9))
{
button1.setText(r91);
};
if (dom.equals(d10))
{
button1.setText(r101);
};
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button1ActionPerformed(evt);
}
});
button4.setText("button4");
if (dom.equals(d1))
{
button4.setText(r14);
};
if (dom.equals(d2))
{
button4.setText(r24);
};
if (dom.equals(d3))
{
button4.setText(r34);
};
if (dom.equals(d4))
{
button4.setText(r44);
};
if (dom.equals(d5))
{
button4.setText(r54);
};
if (dom.equals(d6))
{
button4.setText(r64);
};
if (dom.equals(d7))
{
button4.setText(r74);
};
if (dom.equals(d8))
{
button4.setText(r84);
};
if (dom.equals(d9))
{
button4.setText(r94);
};
if (dom.equals(d10))
{
button4.setText(r104);
};
button4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button4ActionPerformed(evt);
}
});
button3.setText("button3");
if (dom.equals(d1))
{
button3.setText(r13);
};
if (dom.equals(d2))
{
button3.setText(r23);
};
if (dom.equals(d3))
{
button3.setText(r33);
};
if (dom.equals(d4))
{
button3.setText(r43);
};
if (dom.equals(d5))
{
button3.setText(r53);
};
if (dom.equals(d6))
{
button3.setText(r63);
};
if (dom.equals(d7))
{
button3.setText(r73);
};
if (dom.equals(d8))
{
button3.setText(r83);
};
if (dom.equals(d9))
{
button3.setText(r93);
};
if (dom.equals(d10))
{
button3.setText(r103);
};
button3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button3ActionPerformed(evt);
}
});
button2.setText("button2");
if (dom.equals(d1))
{
button2.setText(r12);
};
if (dom.equals(d2))
{
button2.setText(r22);
};
if (dom.equals(d3))
{
button2.setText(r32);
};
if (dom.equals(d4))
{
button2.setText(r42);
};
if (dom.equals(d5))
{
button2.setText(r52);
};
if (dom.equals(d6))
{
button2.setText(r62);
};
if (dom.equals(d7))
{
button2.setText(r72);
};
if (dom.equals(d8))
{
button2.setText(r82);
};
if (dom.equals(d9))
{
button2.setText(r92);
};
if (dom.equals(d10))
{
button2.setText(r102);
};
button2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button2ActionPerformed(evt);
}
});
haiPerso.setText("HAI PERSO");
haiPerso.setHorizontalAlignment(JLabel.CENTER);
haiPerso.setVisible(false);
haiPerso.setFont(cms);
haiPerso.setForeground(Color.RED);
private void button1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (button1.getText().equals(r11) || button1.getText().equals(r51) || button1.getText().equals(r61))
{
button1.setBackground(Color.GREEN);
button1.setForeground(Color.BLACK);
button2.setEnabled(false);
button3.setEnabled(false);
button4.setEnabled(false);
haiPerso.setText("CORRETTO!");
haiPerso.setForeground(Color.GREEN);
haiPerso.setVisible(true);
}
else
{
button1.setBackground(Color.red);
haiPerso.setVisible(true);
button2.setEnabled(false);
button3.setEnabled(false);
button4.setEnabled(false);
}
}
private void button2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (button2.getText().equals(r72) || button2.getText().equals(r82))
button2.setBackground(Color.GREEN);
button2.setForeground(Color.BLACK);
button1.setEnabled(false);
button3.setEnabled(false);
button4.setEnabled(false);
haiPerso.setText("CORRETTO!");
haiPerso.setForeground(Color.GREEN);
haiPerso.setVisible(true);
}
else
{
button2.setBackground(Color.red);
haiPerso.setVisible(true);
button1.setEnabled(false);
button3.setEnabled(false);
button4.setEnabled(false);
}
}
private void button3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (button3.getText().equals(r23) || button3.getText().equals(r43) || button3.getText().equals(r93))
{
button3.setBackground(Color.GREEN);
button3.setForeground(Color.BLACK);
button2.setEnabled(false);
button1.setEnabled(false);
button4.setEnabled(false);
haiPerso.setText("CORRETTO!");
haiPerso.setForeground(Color.GREEN);
haiPerso.setVisible(true);
}
else
{
button3.setBackground(Color.red);
haiPerso.setVisible(true);
button2.setEnabled(false);
button1.setEnabled(false);
button4.setEnabled(false);
}
}
private void button4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (button4.getText().equals(r34) || button4.getText().equals(r104))
{
button4.setBackground(Color.GREEN);
button4.setForeground(Color.BLACK);
button2.setEnabled(false);
button3.setEnabled(false);
button1.setEnabled(false);
haiPerso.setText("CORRETTO!");
haiPerso.setForeground(Color.GREEN);
haiPerso.setVisible(true);
}
else
{
button4.setBackground(Color.red);
haiPerso.setVisible(true);
button2.setEnabled(false);
button3.setEnabled(false);
button1.setEnabled(false);
}
}
// Variables declaration - do not modify
public javax.swing.JButton button1;
public javax.swing.JButton button2;
public javax.swing.JButton button3;
public javax.swing.JButton button4;
public javax.swing.JLabel domanda;
private javax.swing.JLabel haiPerso;
// End of variables declaration
String d1 = new String("Alessandro Manzoni, noto");
String d2 = new String("Quale di queste città NON fa provincia nel Lazio?");
String d3 = new String("Se si moltiplicano fra di loro tutte le cifre che si trovano sul telefono fisso, si ottiene");
String d4 = new String("Quale di questi animali vive in acque marine?");
String d5 = new String("Cosa significa edulcorare?");
String d6 = new String("Quanti sono i cuccioli di dalmata rapiti in un famoso film Disney?");
String d7 = new String("Quale tra questi noti best-seller di Stephen King è ambientato in una prigione?");
String d8 = new String("Quale di questi pianeti è più vicino al Sole?");
String d9 = new String("Quale tra questi NON è il nome di una moneta?");
String d10 = new String("Quanti sono i magi che si recano a visitare Gesù bambino?");
String r11 = new String("Scrittore");
String r12 = new String("Esploratore");
String r13 = new String("Politico");
String r14 = new String("Medico");
String r21 = new String("Roma");
String r22 = new String("Viterbo");
String r23 = new String("Formia");
String r24 = new String("Frosinone");
String r31 = new String("Circa 350.000");
String r32 = new String("Più di 500.000");
String r33 = new String("Meno di 500");
String r34 = new String("0");
String r41 = new String("Castoro");
String r42 = new String("Coccodrillo");
String r43 = new String("Delfino");
String r44 = new String("Rana");
String r51 = new String("Addolcire");
String r52 = new String("Rimproverare");
String r53 = new String("Educare");
String r54 = new String("Colorare");
String r61 = new String("99");
String r62 = new String("101");
String r63 = new String("100");
String r64 = new String("90");
String r71 = new String("Shining");
String r72 = new String("Il miglio verde");
String r73 = new String("Carrie");
String r74 = new String("Insomnia");
String r81 = new String("Urano");
String r82 = new String("Venere");
String r83 = new String("Terra");
String r84 = new String("Giove");
String r91 = new String("Lira");
String r92 = new String("Grano");
String r93 = new String("Pulvino");
String r94 = new String("Corona");
String r101 = new String("5");
String r102 = new String("2");
String r103 = new String("1");
String r104 = new String("3");
Font cms = new Font("Comic Sans MS", 0, 30);
String[] aDomande1 = {d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13, d14, d15, d16, d17, d18, d19, d20};
ArrayList<String> domande1 = new ArrayList(Arrays.asList(aDomande1));
Random rand = new Random();
int idx = rand.nextInt(domande1.size());
int idx2 = rand.nextInt(domande1.size() - (idx));
int idx3 = rand.nextInt(domande1.size() - (idx2));
int idx4 = rand.nextInt(domande1.size() - (idx3));
int idx5 = rand.nextInt(domande1.size() - (idx4));
String dom = domande1.get(idx);
Aucun commentaire:
Enregistrer un commentaire