Ciao ragazzi!
Vi riscrivo perchè h un problema stupido e non riesco ad uscirene. Il codice della mia applicazione per la visualizzazione di coppie di immagini e scelta di una tra le varie coppie è tanto. L'unico problema è che ogni volta che durante l'esecuzione spunto una checkbox, il suo valore è sempre false. Ho fatto dei test per verificarlo(stampando un array contenente il valre isSelected() di ogni checkbox).Mi spiego con qualche linea di codice. Tutto il programma va bene,ed è come mi aspettavo ma ho solo un problemino con le checkbox. Ho semplificato il codice ma in sostanza è questo:
classe myFrame che dopo aver visualizzato un frame crea un oggetto di tipo screen...
di seguito la classe screen..codice:Screen schermo = new Screen(timeOfImage,timeOfChoice,numOfImages, timeBeforeChoice, timeBetwImage);
codice:public class Screen extends JFrame implements ActionListener { private int timeOfImage; private int timeOfChoice; private int numOfImages; private int timeBetwImage; private int timeBeforeChoice; private JPanel contentPane; private final int MILLE = 1000; private JLabel label; public int index; private Timer timerDisplay; private Timer timerBetweenImages; private Timer timerBeforeChoice; public PrintStream output; private int scelta; private int i; public ImageIcon[] image = new ImageIcon[8]; private Timer timerChoice; private int number; { image[0] = new ImageIcon(getClass().getResource("/Copia di album/1.jpg")); image[1] = new ImageIcon(getClass().getResource("/Copia di album/2.jpg")); image[2] = new ImageIcon(getClass().getResource("/Copia di album/3.jpg")); image[3] = new ImageIcon(getClass().getResource("/Copia di album/4.jpg")); image[4] = new ImageIcon(getClass().getResource("/Copia di album/5.jpg")); image[5] = new ImageIcon(getClass().getResource("/Copia di album/6.jpg")); image[6] = new ImageIcon(getClass().getResource("/Copia di album/7.jpg")); image[7] = new ImageIcon(getClass().getResource("/Copia di album/8.jpg")); } public Screen(int timeOfImage, int timeOfChoice, int numOfImages, int timeBeforeChoice, int timeBetwImage) throws FileNotFoundException { // variabili inserite in un form dall'utente this.timeOfImage = timeOfImage; this.timeOfChoice = timeOfChoice; this.numOfImages = numOfImages; this.timeBeforeChoice = timeBeforeChoice; this.timeBetwImage = timeBetwImage; number = numOfImages; setUndecorated(true); setBackground(Color.BLACK); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); contentPane = new JPanel(); contentPane.setBackground(Color.BLACK); setContentPane(contentPane); contentPane.setLayout(new GridLayout(1, 1)); //posiziono label al centro per visualizzare singola immagine e lo aggiungo label = new JLabel("", SwingConstants.CENTER); contentPane.add(label); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); setBounds(0, 0, screenSize.width, screenSize.height); //creo timer per il tempo di visualizzazione di ogni singola immagine timerDisplay = new Timer(this.timeOfImage * MILLE, this); timerDisplay.setRepeats(false); timerDisplay.setActionCommand("blackScreen"); //timer tra le immagini timerBetweenImages = new Timer(this.timeBetwImage * MILLE, this); timerBetweenImages.setRepeats(false); timerBetweenImages.setActionCommand("image"); //timer prima della scelta timerBeforeChoice = new Timer(this.timeBeforeChoice * MILLE, this); timerBeforeChoice.setRepeats(false); timerBeforeChoice.setActionCommand("Choice"); //tempo di scelta timerChoice = new Timer(this.timeOfChoice * MILLE, this); timerChoice.setRepeats(false); timerChoice.setActionCommand("End"); //creo file csv per i risultati FileOutputStream file = new FileOutputStream("output.csv"); output = new PrintStream(file); output.println("Time;User;ShortExcitement;LongExcitement;Engagement;Image"); //faccio partire il primo timer timerDisplay.start(); } @Override public void actionPerformed(ActionEvent e) { // tempo tra immagini consecutive if (e.getActionCommand().equals("blackScreen")) { label.setIcon(null); if (index != numOfImages) { timerBetweenImages.start(); } else { //se sono finite le immagini allora faccio passare tempo prima della loro scelta label.setIcon(null); timerBeforeChoice.start(); } //mostro le singole immagini } else if (index < numOfImages && e.getActionCommand().equals("image")) { label.setIcon(image[index]); index++; timerDisplay.start(); output.println(); } else if (e.getActionCommand().equals("Choice")) { contentPane.remove(label); //modifico il layout per inserire tanti pannelli quanto sono le immagini contentPane.setLayout(new GridLayout(1, this.numOfImages)); repaint(); validate(); boolean[] o = new boolean[numOfImages]; //creo pannello JPanel panel1 = new JPanel(); panel1.setBackground(Color.BLACK); panel1.setLayout(new GridLayout(2, 1)); //aggiungo immagine e checkbox al pannello JLabel lblNewLabel_1 = new JLabel(""); lblNewLabel_1.setBackground(Color.BLACK); lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER); lblNewLabel_1.setIcon(image[i]); panel1.add(lblNewLabel_1); JCheckBox chckbxNewCheckBox = new JCheckBox("Immagine " + (i + 1)); chckbxNewCheckBox.setBackground(Color.BLACK); chckbxNewCheckBox.setForeground(Color.WHITE); chckbxNewCheckBox.setHorizontalAlignment(SwingConstants.CENTER); chckbxNewCheckBox.setFont(new Font("Tahoma", Font.BOLD, 14)); panel1.add(chckbxNewCheckBox); contentPane.add(panel1); //creo pannello JPanel panel2 = new JPanel(); panel2.setBackground(Color.BLACK); panel2.setLayout(new GridLayout(2, 1)); //aggiungo immagine e checkbox al pannello JLabel lblNewLabel_2 = new JLabel(""); lblNewLabel_2.setBackground(Color.BLACK); lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER); lblNewLabel_2.setIcon(image[i + 1]); panel2.add(lblNewLabel_2); JCheckBox chckbxNewCheckBox2 = new JCheckBox("Immagine " + (i + 2)); chckbxNewCheckBox2.setBackground(Color.BLACK); chckbxNewCheckBox2.setForeground(Color.WHITE); chckbxNewCheckBox2.setHorizontalAlignment(SwingConstants.CENTER); chckbxNewCheckBox2.setFont(new Font("Tahoma", Font.BOLD, 14)); panel2.add(chckbxNewCheckBox2); i++; //aggiungo pannello al contentPane contentPane.add(panel2); repaint(); validate(); timerChoice.start(); o[0] = chckbxNewCheckBox.isSelected(); o[1] = chckbxNewCheckBox.isSelected(); output.println(scelta); ///PROBLEMAAAAAAAAAAAA sempre false anche se possono essere selezionate for (int j = 0; j <= 1; j++) { if (o[j]) { scelta = j; } System.out.println(o[j]); } } else if (e.getActionCommand().equals("End")) { contentPane.removeAll(); repaint(); validate(); index = this.numOfImages; //System.out.println(index); this.numOfImages = this.numOfImages + number; //System.out.println(numOfImages); contentPane.setLayout(new GridLayout(1, 1)); contentPane.add(label); if (numOfImages <= image.length) { timerDisplay.start(); } else { dispose(); } } } }INSOMMA è sempre false.vorrei sapere se il problema sta:codice:///PROBLEMAAAAAAAAAAAA sempre false anche se possono essere selezionate for (int j = 0; j <=1;j++){ if(o[j]) scelta = j; System.out.println(o[j]);
1-nei timer
2-nel dover associare un actionlistener alle checkbox
Purtroppo sono poco ferrat su questi due concetti quindi vorrei capire secondo voi in quale direzione dovrei spingermi.
grazie

Rispondi quotando

