開発メモ

開発用のメモです。

Java/Swing テンプレート

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Main extends JFrame {

    JMenuBar menuBar = new JMenuBar();
    JMenu file = new JMenu("ファイル(F)");
    JMenuItem exit = new JMenuItem("終了(E)");

    JMenu help = new JMenu("ヘルプ(H)");

    public static void main(String[] args) {
        try {
            String lf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
            UIManager.setLookAndFeel(lf);
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Main m = new Main();
        m.setSize(800, 600);
        m.setVisible(true);
    }

    private Main() {
        this.setLayout(new GridLayout(10, 10));
        this.setJMenuBar(menuBar);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        menuBar.add(file);
        file.setMnemonic(KeyEvent.VK_F);

        this.file.add(exit);
        exit.setMnemonic(KeyEvent.VK_E);
        exit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        menuBar.add(help);
        help.setMnemonic(KeyEvent.VK_H);

    }

}
Twitter: @asahina_alice