OmikujiGUI.java

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class OmikujiGUI extends JFrame {

    private final JLabel resultLabel = new JLabel("—", SwingConstants.CENTER);
    private final JLabel messageLabel = new JLabel("ボタンを押して引け", SwingConstants.CENTER);
    private final JLabel luckyLabel = new JLabel("ラッキー:—", SwingConstants.CENTER);

    private final JTextArea historyArea = new JTextArea(10, 30);

    private final Random random = new Random();

    private static final String[] RESULTS = {"大吉", "中吉", "小吉", "吉", "末吉", "凶", "大凶"};
    private static final String[] MESSAGES = {
            "最強。今日は攻めていい。",
            "良い流れ。焦らず積み上げろ。",
            "小さく勝てる日。丁寧にいこう。",
            "安定。普通が一番強い。",
            "油断すると崩れる。慎重に。",
            "無理は禁物。守りでいけ。",
            "今日は撤退が正解。休め。"
    };
    private static final String[] COLORS = {"赤", "青", "緑", "黒", "白", "金", "紫"};

    public OmikujiGUI() {
        super("おみくじ(GUI版)");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setMinimumSize(new Dimension(520, 520));
        setLocationRelativeTo(null);

        // 全体レイアウト
        JPanel root = new JPanel(new BorderLayout(12, 12));
        root.setBorder(new EmptyBorder(16, 16, 16, 16));
        setContentPane(root);

        // 上部:タイトル
        JLabel title = new JLabel("おみくじアプリ", SwingConstants.LEFT);
        title.setFont(title.getFont().deriveFont(Font.BOLD, 22f));
        root.add(title, BorderLayout.NORTH);

        // 中央:結果表示パネル
        JPanel center = new JPanel();
        center.setLayout(new BoxLayout(center, BoxLayout.Y_AXIS));
        root.add(center, BorderLayout.CENTER);

        resultLabel.setFont(resultLabel.getFont().deriveFont(Font.BOLD, 56f));
        resultLabel.setBorder(new EmptyBorder(18, 12, 10, 12));

        messageLabel.setFont(messageLabel.getFont().deriveFont(Font.PLAIN, 16f));
        messageLabel.setBorder(new EmptyBorder(6, 12, 6, 12));

        luckyLabel.setFont(luckyLabel.getFont().deriveFont(Font.PLAIN, 15f));
        luckyLabel.setBorder(new EmptyBorder(6, 12, 14, 12));

        center.add(wrapCard(resultLabel));
        center.add(Box.createVerticalStrut(10));
        center.add(wrapCard(messageLabel));
        center.add(Box.createVerticalStrut(8));
        center.add(wrapCard(luckyLabel));
        center.add(Box.createVerticalStrut(12));

        // 履歴
        JLabel historyTitle = new JLabel("履歴");
        historyTitle.setFont(historyTitle.getFont().deriveFont(Font.BOLD, 16f));
        historyTitle.setBorder(new EmptyBorder(4, 2, 6, 2));
        center.add(historyTitle);

        historyArea.setEditable(false);
        historyArea.setLineWrap(true);
        historyArea.setWrapStyleWord(true);
        historyArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 13));
        historyArea.setText("(まだありません)");

        JScrollPane scroll = new JScrollPane(historyArea);
        scroll.setBorder(BorderFactory.createLineBorder(new Color(220, 220, 220)));
        center.add(scroll);

        // 下部:ボタン
        JPanel bottom = new JPanel(new GridLayout(1, 3, 10, 10));
        JButton drawBtn = new JButton("引く");
        JButton clearBtn = new JButton("履歴クリア");
        JButton exitBtn = new JButton("終了");

        drawBtn.setFont(drawBtn.getFont().deriveFont(Font.BOLD, 14f));

        drawBtn.addActionListener(e -> drawOmikuji());
        clearBtn.addActionListener(e -> clearHistory());
        exitBtn.addActionListener(e -> dispose());

        bottom.add(drawBtn);
        bottom.add(clearBtn);
        bottom.add(exitBtn);

        root.add(bottom, BorderLayout.SOUTH);
    }

    private JPanel wrapCard(JComponent comp) {
        JPanel p = new JPanel(new BorderLayout());
        p.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(225, 225, 225)),
                new EmptyBorder(6, 8, 6, 8)
        ));
        p.add(comp, BorderLayout.CENTER);
        return p;
    }

    private void drawOmikuji() {
        int idx = random.nextInt(RESULTS.length);

        String result = RESULTS[idx];
        String message = MESSAGES[idx];

        int luckyNumber = random.nextInt(99) + 1; // 1〜99
        String luckyColor = COLORS[random.nextInt(COLORS.length)];

        resultLabel.setText(result);
        messageLabel.setText(message);
        luckyLabel.setText("ラッキーナンバー:" + luckyNumber + " / ラッキーカラー:" + luckyColor);

        String time = new SimpleDateFormat("yyyy/MM/dd HH:mm").format(new Date());
        String line = time + "  →  " + result + "(#" + luckyNumber + " / " + luckyColor + ")";

        appendHistory(line);
    }

    private void appendHistory(String line) {
        String current = historyArea.getText();
        if (current.equals("(まだありません)")) current = "";
        if (!current.isEmpty()) current = current + "\n";
        historyArea.setText(current + line);
        historyArea.setCaretPosition(historyArea.getDocument().getLength());
    }

    private void clearHistory() {
        historyArea.setText("(まだありません)");
        resultLabel.setText("—");
        messageLabel.setText("ボタンを押して引け");
        luckyLabel.setText("ラッキー:—");
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new OmikujiGUI().setVisible(true));
    }
}

投稿者: chosuke

趣味はゲームやアニメや漫画などです

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です