{"id":26248,"date":"2025-12-27T04:52:40","date_gmt":"2025-12-26T19:52:40","guid":{"rendered":"http:\/\/www.tyosuke20xx.com\/blog\/?p=26248"},"modified":"2025-12-27T04:52:43","modified_gmt":"2025-12-26T19:52:43","slug":"omikujigui-java","status":"publish","type":"post","link":"http:\/\/www.tyosuke20xx.com\/blog\/?p=26248","title":{"rendered":"OmikujiGUI.java"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>import javax.swing.*;\nimport javax.swing.border.EmptyBorder;\nimport java.awt.*;\nimport java.text.SimpleDateFormat;\nimport java.util.Date;\nimport java.util.Random;\n\npublic class OmikujiGUI extends JFrame {\n\n    private final JLabel resultLabel = new JLabel(\"\u2014\", SwingConstants.CENTER);\n    private final JLabel messageLabel = new JLabel(\"\u30dc\u30bf\u30f3\u3092\u62bc\u3057\u3066\u5f15\u3051\", SwingConstants.CENTER);\n    private final JLabel luckyLabel = new JLabel(\"\u30e9\u30c3\u30ad\u30fc\uff1a\u2014\", SwingConstants.CENTER);\n\n    private final JTextArea historyArea = new JTextArea(10, 30);\n\n    private final Random random = new Random();\n\n    private static final String&#91;] RESULTS = {\"\u5927\u5409\", \"\u4e2d\u5409\", \"\u5c0f\u5409\", \"\u5409\", \"\u672b\u5409\", \"\u51f6\", \"\u5927\u51f6\"};\n    private static final String&#91;] MESSAGES = {\n            \"\u6700\u5f37\u3002\u4eca\u65e5\u306f\u653b\u3081\u3066\u3044\u3044\u3002\",\n            \"\u826f\u3044\u6d41\u308c\u3002\u7126\u3089\u305a\u7a4d\u307f\u4e0a\u3052\u308d\u3002\",\n            \"\u5c0f\u3055\u304f\u52dd\u3066\u308b\u65e5\u3002\u4e01\u5be7\u306b\u3044\u3053\u3046\u3002\",\n            \"\u5b89\u5b9a\u3002\u666e\u901a\u304c\u4e00\u756a\u5f37\u3044\u3002\",\n            \"\u6cb9\u65ad\u3059\u308b\u3068\u5d29\u308c\u308b\u3002\u614e\u91cd\u306b\u3002\",\n            \"\u7121\u7406\u306f\u7981\u7269\u3002\u5b88\u308a\u3067\u3044\u3051\u3002\",\n            \"\u4eca\u65e5\u306f\u64a4\u9000\u304c\u6b63\u89e3\u3002\u4f11\u3081\u3002\"\n    };\n    private static final String&#91;] COLORS = {\"\u8d64\", \"\u9752\", \"\u7dd1\", \"\u9ed2\", \"\u767d\", \"\u91d1\", \"\u7d2b\"};\n\n    public OmikujiGUI() {\n        super(\"\u304a\u307f\u304f\u3058\uff08GUI\u7248\uff09\");\n        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\n        setMinimumSize(new Dimension(520, 520));\n        setLocationRelativeTo(null);\n\n        \/\/ \u5168\u4f53\u30ec\u30a4\u30a2\u30a6\u30c8\n        JPanel root = new JPanel(new BorderLayout(12, 12));\n        root.setBorder(new EmptyBorder(16, 16, 16, 16));\n        setContentPane(root);\n\n        \/\/ \u4e0a\u90e8\uff1a\u30bf\u30a4\u30c8\u30eb\n        JLabel title = new JLabel(\"\u304a\u307f\u304f\u3058\u30a2\u30d7\u30ea\", SwingConstants.LEFT);\n        title.setFont(title.getFont().deriveFont(Font.BOLD, 22f));\n        root.add(title, BorderLayout.NORTH);\n\n        \/\/ \u4e2d\u592e\uff1a\u7d50\u679c\u8868\u793a\u30d1\u30cd\u30eb\n        JPanel center = new JPanel();\n        center.setLayout(new BoxLayout(center, BoxLayout.Y_AXIS));\n        root.add(center, BorderLayout.CENTER);\n\n        resultLabel.setFont(resultLabel.getFont().deriveFont(Font.BOLD, 56f));\n        resultLabel.setBorder(new EmptyBorder(18, 12, 10, 12));\n\n        messageLabel.setFont(messageLabel.getFont().deriveFont(Font.PLAIN, 16f));\n        messageLabel.setBorder(new EmptyBorder(6, 12, 6, 12));\n\n        luckyLabel.setFont(luckyLabel.getFont().deriveFont(Font.PLAIN, 15f));\n        luckyLabel.setBorder(new EmptyBorder(6, 12, 14, 12));\n\n        center.add(wrapCard(resultLabel));\n        center.add(Box.createVerticalStrut(10));\n        center.add(wrapCard(messageLabel));\n        center.add(Box.createVerticalStrut(8));\n        center.add(wrapCard(luckyLabel));\n        center.add(Box.createVerticalStrut(12));\n\n        \/\/ \u5c65\u6b74\n        JLabel historyTitle = new JLabel(\"\u5c65\u6b74\");\n        historyTitle.setFont(historyTitle.getFont().deriveFont(Font.BOLD, 16f));\n        historyTitle.setBorder(new EmptyBorder(4, 2, 6, 2));\n        center.add(historyTitle);\n\n        historyArea.setEditable(false);\n        historyArea.setLineWrap(true);\n        historyArea.setWrapStyleWord(true);\n        historyArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 13));\n        historyArea.setText(\"\uff08\u307e\u3060\u3042\u308a\u307e\u305b\u3093\uff09\");\n\n        JScrollPane scroll = new JScrollPane(historyArea);\n        scroll.setBorder(BorderFactory.createLineBorder(new Color(220, 220, 220)));\n        center.add(scroll);\n\n        \/\/ \u4e0b\u90e8\uff1a\u30dc\u30bf\u30f3\n        JPanel bottom = new JPanel(new GridLayout(1, 3, 10, 10));\n        JButton drawBtn = new JButton(\"\u5f15\u304f\");\n        JButton clearBtn = new JButton(\"\u5c65\u6b74\u30af\u30ea\u30a2\");\n        JButton exitBtn = new JButton(\"\u7d42\u4e86\");\n\n        drawBtn.setFont(drawBtn.getFont().deriveFont(Font.BOLD, 14f));\n\n        drawBtn.addActionListener(e -> drawOmikuji());\n        clearBtn.addActionListener(e -> clearHistory());\n        exitBtn.addActionListener(e -> dispose());\n\n        bottom.add(drawBtn);\n        bottom.add(clearBtn);\n        bottom.add(exitBtn);\n\n        root.add(bottom, BorderLayout.SOUTH);\n    }\n\n    private JPanel wrapCard(JComponent comp) {\n        JPanel p = new JPanel(new BorderLayout());\n        p.setBorder(BorderFactory.createCompoundBorder(\n                BorderFactory.createLineBorder(new Color(225, 225, 225)),\n                new EmptyBorder(6, 8, 6, 8)\n        ));\n        p.add(comp, BorderLayout.CENTER);\n        return p;\n    }\n\n    private void drawOmikuji() {\n        int idx = random.nextInt(RESULTS.length);\n\n        String result = RESULTS&#91;idx];\n        String message = MESSAGES&#91;idx];\n\n        int luckyNumber = random.nextInt(99) + 1; \/\/ 1\u301c99\n        String luckyColor = COLORS&#91;random.nextInt(COLORS.length)];\n\n        resultLabel.setText(result);\n        messageLabel.setText(message);\n        luckyLabel.setText(\"\u30e9\u30c3\u30ad\u30fc\u30ca\u30f3\u30d0\u30fc\uff1a\" + luckyNumber + \" \/ \u30e9\u30c3\u30ad\u30fc\u30ab\u30e9\u30fc\uff1a\" + luckyColor);\n\n        String time = new SimpleDateFormat(\"yyyy\/MM\/dd HH:mm\").format(new Date());\n        String line = time + \"  \u2192  \" + result + \"\uff08#\" + luckyNumber + \" \/ \" + luckyColor + \"\uff09\";\n\n        appendHistory(line);\n    }\n\n    private void appendHistory(String line) {\n        String current = historyArea.getText();\n        if (current.equals(\"\uff08\u307e\u3060\u3042\u308a\u307e\u305b\u3093\uff09\")) current = \"\";\n        if (!current.isEmpty()) current = current + \"\\n\";\n        historyArea.setText(current + line);\n        historyArea.setCaretPosition(historyArea.getDocument().getLength());\n    }\n\n    private void clearHistory() {\n        historyArea.setText(\"\uff08\u307e\u3060\u3042\u308a\u307e\u305b\u3093\uff09\");\n        resultLabel.setText(\"\u2014\");\n        messageLabel.setText(\"\u30dc\u30bf\u30f3\u3092\u62bc\u3057\u3066\u5f15\u3051\");\n        luckyLabel.setText(\"\u30e9\u30c3\u30ad\u30fc\uff1a\u2014\");\n    }\n\n    public static void main(String&#91;] args) {\n        SwingUtilities.invokeLater(() -> new OmikujiGUI().setVisible(true));\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[76],"tags":[],"class_list":["post-26248","post","type-post","status-publish","format-standard","hentry","category-java"],"aioseo_notices":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/26248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=26248"}],"version-history":[{"count":1,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/26248\/revisions"}],"predecessor-version":[{"id":26249,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/26248\/revisions\/26249"}],"wp:attachment":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26248"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}