package tudelft.mentalhealth.motivatepersisting; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Locale; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; /** * An interactive example showing the {@link MotivationalAnswer} */ public class Example extends JFrame { private final ComboWithLabel trend = new ComboWithLabel( PclTrend.values(), "PCL Trend"); private final ComboWithLabel trust = new ComboWithLabel( Trust.values(), "trust"); private final JButton textButton = new JButton("make text"); private final JTextArea text = new JTextArea(4, 50); private final Locale NL = new Locale("NL", "nl"); private final Locale EN = new Locale("en", ""); private final ComboWithLabel locales = new ComboWithLabel( new Locale[] { NL, EN }, "language"); public Example() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(panel()); textButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Situation situation = new Situation(trend.getSelectedItem(), trust.getSelectedItem()); Locale locale = locales.getSelectedItem(); text.setText( new MotivationalAnswer(situation, locale).getText()); } }); pack(); setVisible(true); } private JPanel panel() { JPanel panel = new JPanel(new BorderLayout()); text.setWrapStyleWord(true); text.setLineWrap(true); panel.add(text, BorderLayout.CENTER); JPanel buttons = new JPanel(new FlowLayout()); buttons.add(trend); buttons.add(trust); buttons.add(locales); buttons.add(textButton); panel.add(buttons, BorderLayout.SOUTH); return panel; } public static void main(String[] args) { new Example(); } } /** * Combobox with a label * * @param the element type of the combobox. */ @SuppressWarnings("serial") class ComboWithLabel extends JPanel { private JComboBox combo; public ComboWithLabel(T[] items, String label) { this.combo = new JComboBox<>(items); setLayout(new BorderLayout()); add(new JLabel(" " + label), BorderLayout.NORTH); add(combo, BorderLayout.SOUTH); } @SuppressWarnings("unchecked") public T getSelectedItem() { return (T) combo.getSelectedItem(); } }