-
Notifications
You must be signed in to change notification settings - Fork 5
/
App.kt
75 lines (66 loc) · 1.99 KB
/
App.kt
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
package example
import java.awt.*
import java.text.DateFormat
import java.text.ParsePosition
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import java.util.TimeZone
import javax.swing.*
fun makeUI(): Component {
val format = SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US)
val df = DateFormat.getDateTimeInstance()
// df.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo"))
df.timeZone = TimeZone.getDefault()
val field = JTextField(30)
field.text = format.format(Date())
val formatButton = JButton("format")
formatButton.addActionListener { field.text = format.format(Date()) }
val textArea = JTextArea()
textArea.isEditable = false
val parseButton = JButton("parse")
parseButton.addActionListener {
val date = format.parse(field.text.trim(), ParsePosition(0))
val str = date?.let { df.format(date) } ?: "error"
textArea.append(str + "\n")
}
val bp = JPanel(GridLayout(1, 0, 2, 2))
bp.add(formatButton)
bp.add(parseButton)
val c = GridBagConstraints()
val p = JPanel(GridBagLayout())
p.border = BorderFactory.createTitledBorder("DateFormat")
c.insets = Insets(2, 2, 2, 2)
c.fill = GridBagConstraints.HORIZONTAL
c.anchor = GridBagConstraints.LINE_END
c.weightx = 1.0
p.add(field, c)
c.insets = Insets(2, 0, 2, 2)
c.fill = GridBagConstraints.NONE
c.weightx = 0.0
c.gridy = 1
p.add(bp, c)
return JPanel(BorderLayout()).also {
it.add(p, BorderLayout.NORTH)
it.add(JScrollPane(textArea))
it.border = BorderFactory.createEmptyBorder(5, 5, 5, 5)
it.preferredSize = Dimension(320, 240)
}
}
fun main() {
EventQueue.invokeLater {
runCatching {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
}.onFailure {
it.printStackTrace()
Toolkit.getDefaultToolkit().beep()
}
JFrame().apply {
defaultCloseOperation = WindowConstants.EXIT_ON_CLOSE
contentPane.add(makeUI())
pack()
setLocationRelativeTo(null)
isVisible = true
}
}
}