Created
April 15, 2012 09:11
-
-
Save Cherubi/2391384 to your computer and use it in GitHub Desktop.
TilastoivaKysely
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package kysely; | |
import java.awt.BorderLayout; | |
import java.awt.Component; | |
import java.awt.Container; | |
import java.awt.Dimension; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.util.Collection; | |
import javax.swing.*; | |
public class Kysely implements Runnable, ActionListener { | |
private Collection<Kysymys> kysymykset; | |
private Tilasto tilasto; | |
//private Tilastoija tilastoija; | |
public Kysely(Collection<Kysymys> kysymykset, Tilasto tilasto) { | |
this.kysymykset = kysymykset; | |
this.tilasto = tilasto; | |
} | |
@Override | |
public void run() { | |
JFrame frame = new JFrame("Kysely"); | |
frame.setPreferredSize(new Dimension(500, 400)); | |
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); | |
luoKomponentit(frame.getContentPane()); | |
frame.pack(); | |
frame.setVisible(true); | |
} | |
private void luoKomponentit(Container container) { | |
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS)); | |
luoKysymykset(container); | |
JButton valmis = new JButton("Valmis"); | |
container.add(valmis); | |
valmis.addActionListener(this); | |
} | |
private void luoKysymykset(Container container) { | |
for (Kysymys kysymys : kysymykset) { | |
JPanel paneli = new JPanel(); | |
paneli.setLayout(new BoxLayout(paneli, BoxLayout.X_AXIS)); | |
JLabel kysymysLabel = new JLabel(kysymys.getKysymys()); | |
paneli.add(kysymysLabel); | |
ButtonGroup ryhma = new ButtonGroup(); | |
for (String vastausvaihtoehto : kysymys.getVaihtoehdot()) { | |
JRadioButton vaihtoehto = new JRadioButton(vastausvaihtoehto); | |
paneli.add(vaihtoehto); | |
ryhma.add(vaihtoehto); | |
//kysymys.lisaaJRadioButton(vaihtoehto); | |
} | |
container.add(paneli); | |
} | |
} | |
public void actionPerformed(ActionEvent ae) { | |
System.out.println("Tapahtui jotain."); | |
tilastoiVastaukset(); | |
} | |
private void tilastoiVastaukset() { | |
for (Kysymys kysymys : kysymykset) { | |
tilasto.tilastoiVastaus(kysymys.getKysymys(), ""); | |
//tilasto.tilastoiVastaus(kysymys.getKysymys(), kysymys.haeValittu()); | |
//System.out.println(kysymys.haeValittu()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package kysely; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.List; | |
import javax.swing.JRadioButton; | |
public class Kysymys { | |
private String kysymys; | |
private ArrayList<String> vaihtoehdot; | |
private ArrayList<JRadioButton> radiot; | |
public Kysymys(String kysymys) { | |
this.kysymys = kysymys; | |
this.vaihtoehdot = new ArrayList<String>(); | |
this.radiot = new ArrayList<JRadioButton>(); | |
} | |
public Kysymys(String kysymys, Collection<String> vaihtoehdot) { | |
this.kysymys = kysymys; | |
this.vaihtoehdot = new ArrayList<String>(vaihtoehdot); | |
} | |
public void lisaaVaihtoehto(String vaihtoehto) { | |
vaihtoehdot.add(vaihtoehto); | |
} | |
public String getKysymys() { | |
return kysymys; | |
} | |
public List<String> getVaihtoehdot() { | |
return vaihtoehdot; | |
} | |
public void lisaaJRadioButton(JRadioButton radio) { | |
this.radiot.add(radio); | |
} | |
/*public String haeValittu() { | |
for (JRadioButton radio : radiot) { | |
if (radio.isSelected()) { | |
return radio.getText(); | |
} | |
} | |
return ""; | |
}*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment