source: MotivateDuringTherapy/src/main/java/tudelft/mentalhealth/motivatepersisting/Example.java@ 7

Last change on this file since 7 was 2, checked in by Wouter Pasman, 6 years ago

extra buttons etc

File size: 2.4 KB
Line 
1package tudelft.mentalhealth.motivatepersisting;
2
3import java.awt.BorderLayout;
4import java.awt.FlowLayout;
5import java.awt.event.ActionEvent;
6import java.awt.event.ActionListener;
7import java.util.Locale;
8
9import javax.swing.JButton;
10import javax.swing.JComboBox;
11import javax.swing.JFrame;
12import javax.swing.JLabel;
13import javax.swing.JPanel;
14import javax.swing.JTextArea;
15
16/**
17 * An interactive example showing the {@link MotivationalAnswer}
18 */
19public class Example extends JFrame {
20 private final ComboWithLabel<PclTrend> trend = new ComboWithLabel<PclTrend>(
21 PclTrend.values(), "PCL Trend");
22 private final ComboWithLabel<Trust> trust = new ComboWithLabel<Trust>(
23 Trust.values(), "trust");
24 private final JButton textButton = new JButton("make text");
25 private final JTextArea text = new JTextArea(4, 50);
26 private final Locale NL = new Locale("NL", "nl");
27 private final Locale EN = new Locale("en", "");
28 private final ComboWithLabel<Locale> locales = new ComboWithLabel<Locale>(
29 new Locale[] { NL, EN }, "language");
30
31 public Example() {
32 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
33 add(panel());
34
35 textButton.addActionListener(new ActionListener() {
36 @Override
37 public void actionPerformed(ActionEvent e) {
38 Situation situation = new Situation(trend.getSelectedItem(),
39 trust.getSelectedItem());
40 Locale locale = locales.getSelectedItem();
41 text.setText(
42 new MotivationalAnswer(situation, locale).getText());
43 }
44 });
45
46 pack();
47 setVisible(true);
48
49 }
50
51 private JPanel panel() {
52 JPanel panel = new JPanel(new BorderLayout());
53 text.setWrapStyleWord(true);
54 text.setLineWrap(true);
55 panel.add(text, BorderLayout.CENTER);
56 JPanel buttons = new JPanel(new FlowLayout());
57 buttons.add(trend);
58 buttons.add(trust);
59 buttons.add(locales);
60 buttons.add(textButton);
61 panel.add(buttons, BorderLayout.SOUTH);
62
63 return panel;
64 }
65
66 public static void main(String[] args) {
67 new Example();
68 }
69}
70
71/**
72 * Combobox with a label
73 *
74 * @param <T> the element type of the combobox.
75 */
76@SuppressWarnings("serial")
77class ComboWithLabel<T> extends JPanel {
78 private JComboBox combo;
79
80 public ComboWithLabel(T[] items, String label) {
81 this.combo = new JComboBox<>(items);
82 setLayout(new BorderLayout());
83 add(new JLabel(" " + label), BorderLayout.NORTH);
84 add(combo, BorderLayout.SOUTH);
85 }
86
87 @SuppressWarnings("unchecked")
88 public T getSelectedItem() {
89 return (T) combo.getSelectedItem();
90 }
91}
Note: See TracBrowser for help on using the repository browser.