forked from rick2785/JavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaLesson26.java
More file actions
136 lines (77 loc) · 3.11 KB
/
JavaLesson26.java
File metadata and controls
136 lines (77 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import javax.swing.*;
import java.awt.event.*;
// Object that allows me to use height & width units
import java.awt.Dimension;
// Used to get todays date to use with Calendar
import java.util.Date;
// Editor for JSpinner that allows easy incrementing of dates
import javax.swing.SpinnerDateModel;
// Calendar provides methods that make it easy to work with dates
import java.util.Calendar;
public class JavaLesson26 extends JFrame{
JButton button1;
JSpinner spinner1, spinner2, spinner3, spinner4;
String outputString = "";
public static void main(String[] args){
new JavaLesson26();
}
public JavaLesson26(){
this.setSize(400,400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("My Fifth Frame");
JPanel thePanel = new JPanel();
// Create a button
button1 = new JButton("Get Answer");
ListenForButton lForButton = new ListenForButton();
button1.addActionListener(lForButton);
thePanel.add(button1);
// Create a basic 1-9 number spinner
spinner1 = new JSpinner();
thePanel.add(spinner1);
// Create a spinner with initial number, starting number,
// max number, increment with each click
spinner2 = new JSpinner(new SpinnerNumberModel(1, 1, 100, 1));
thePanel.add(spinner2);
// Create a spinner using default values
String[] weekDays = {"Mon", "Tues", "Weds", "Thurs", "Fri"};
spinner3 = new JSpinner(new SpinnerListModel(weekDays));
// Set the size for the spinner so that everything fits
Dimension d = spinner3.getPreferredSize();
d.width = 80;
spinner3.setPreferredSize(d);
thePanel.add(spinner3);
// Get todays date
Date todaysDate = new Date();
// Create a date spinner & set default to today, no minimum, or max
// Increment the days on button presses
// Can also increment YEAR, MONTH, or DAY_OF_MONTH
spinner4 = new JSpinner(new SpinnerDateModel(todaysDate, null, null,
Calendar.DAY_OF_MONTH));
// DateEditor is an editor that handles displaying & editing the dates
JSpinner.DateEditor dateEditor = new JSpinner.DateEditor(spinner4, "dd/MM/yy");
spinner4.setEditor(dateEditor);
thePanel.add(spinner4);
/*
You can add a change listener with Spinners as well
ListenForSpinner lForSpinner = new ListenForSpinner();
Tell Java that you want to be alerted when an event
occurs on the spinner
spinner4.addChangeListener(lForSpinner);
*/
this.add(thePanel);
this.setVisible(true);
}
private class ListenForButton implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource() == button1){
outputString += "Spinner 1 Value: " + spinner1.getValue() + "\n";
outputString += "Spinner 2 Value: " + spinner2.getValue() + "\n";
outputString += "Spinner 3 Value: " + spinner3.getValue() + "\n";
outputString += "Spinner 4 Value: " + spinner4.getValue() + "\n";
JOptionPane.showMessageDialog(JavaLesson26.this, outputString, "Information", JOptionPane.INFORMATION_MESSAGE);
outputString = "";
}
}
}
}