-
Notifications
You must be signed in to change notification settings - Fork 5
/
App.kt
82 lines (72 loc) · 2.09 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
76
77
78
79
80
81
82
package example
import java.awt.*
import javax.swing.*
fun makeUI(): Component {
val p1 = JPanel(GridBagLayout())
val b1 = BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("GridBagLayout"),
BorderFactory.createMatteBorder(5, 10, 15, 20, Color.RED),
)
p1.border = b1
p1.add(JButton("GridBagLayout"))
val p2 = JPanel(CenterLayout())
val b2 = BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("CenterLayout"),
BorderFactory.createMatteBorder(5, 10, 15, 20, Color.GREEN),
)
p2.border = b2
p2.add(JButton("CenterLayout"))
return JPanel(GridLayout(0, 1, 0, 10)).also {
it.add(p1)
it.add(p2)
it.preferredSize = Dimension(320, 240)
}
}
private class CenterLayout : LayoutManager {
override fun addLayoutComponent(
name: String,
comp: Component,
) {
// not needed
}
override fun removeLayoutComponent(comp: Component) {
// not needed
}
override fun preferredLayoutSize(container: Container) =
container.getComponent(0)?.let {
val size = it.preferredSize
val insets = container.insets
size.width += insets.left + insets.right
size.height += insets.top + insets.bottom
size
} ?: Dimension()
override fun minimumLayoutSize(c: Container) = preferredLayoutSize(c)
override fun layoutContainer(container: Container) {
val c = container.getComponent(0)
c.size = c.preferredSize
if (container is JComponent) {
val size = c.size
val r = SwingUtilities.calculateInnerArea(container, null)
val x = r.x + (r.width - size.width) / 2
val y = r.y + (r.height - size.height) / 2
c.setBounds(x, y, size.width, size.height)
}
}
}
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
}
}
}