【Java】【Swing】モジュール提供用テンプレートを作ってみた Part3 ~Login画面 その1~

はじめに

http://blogs.yahoo.co.jp/dk521123/35027281.html
http://blogs.yahoo.co.jp/dk521123/36550446.html
で、モジュール提供用テンプレートを作ってみたが
ひとまず、申し訳ない程度にログイン画面も必要な場合もあるので
簡単なテンプレートを作ってみた

# JPasswordField を使ってるだけだが...

サンプル

LoginView.java

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

public class LoginView extends JFrame implements ActionListener {
  /** Serial Version UID. */
  private static final long serialVersionUID = 1L;

  private static final String ID = "admin";
  private static final String PASSWORD = "password";
  
  private static final String FONT_NAME = "Arial";
  private static final int FONT_SIZE = 30;
  
  private static final String TITLE = "System Name";
  private static final String INPUT_TITLE = "Login";
  private static final String ICON = "etc/icon.jpg";
  private static final int X_COORDINATE_SCREEN = 800;
  private static final int Y_COORDINATE_SCREEN = 200;
  private static final int WIDTH_SCREEN = 600;
  private static final int HEIGHT_SCREEN = 400;
  
  private static final boolean IS_RESIZABLE = true;
  
  private JTextField idTextField;
  private JPasswordField passwordField;
  private JButton loginButton;
  
  public static void main(String[] args) {
    LoginView view = new LoginView();
    view.showScreen();
  }
  
  public LoginView() {
    // For North Panel
    JPanel northPanel = new JPanel();
    JLabel outputLabel = new JLabel("<html><h1>" + TITLE + "</h1></html>", JLabel.CENTER);
    northPanel.add(outputLabel);

    // For West Panel
    JPanel westPanel = new JPanel();
    westPanel.setVisible(false);

    // For East Panel
    JPanel eastPanel = new JPanel();
    eastPanel.setVisible(false);

    // For Center Panel
    JPanel centerPanel = new JPanel(new GridBagLayout());
    this.idTextField = new JTextField();
    this.idTextField.setFont(new Font(FONT_NAME, Font.PLAIN, FONT_SIZE));
    this.passwordField = new JPasswordField();
    this.passwordField.setFont(new Font(FONT_NAME, Font.PLAIN, FONT_SIZE));
    
    LineBorder insideBorder = new LineBorder(Color.BLACK, 1);
    TitledBorder titleBorder = new TitledBorder(insideBorder, 
        INPUT_TITLE, TitledBorder.LEFT, TitledBorder.TOP, new Font(FONT_NAME, Font.PLAIN, FONT_SIZE));
    centerPanel.setBorder(titleBorder);
    GridBagConstraints gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridheight = 1;

    gridBagConstraints.gridx = 0;
    gridBagConstraints.insets = new Insets(5, 5, 5, 0);
    gridBagConstraints.anchor = GridBagConstraints.WEST;
    gridBagConstraints.gridy = 0;
    JLabel item1 = new JLabel("ID :");
    item1.setFont(new Font(FONT_NAME, Font.BOLD, FONT_SIZE));
    centerPanel.add(item1, gridBagConstraints);
    gridBagConstraints.gridy = 1;
    JLabel item2 = new JLabel("Password :");
    item2.setFont(new Font(FONT_NAME, Font.BOLD, FONT_SIZE));
    centerPanel.add(item2, gridBagConstraints);
    gridBagConstraints.gridy = 2;

    gridBagConstraints.gridx = 1;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    gridBagConstraints.gridy = 0;
    centerPanel.add(this.idTextField, gridBagConstraints);
    gridBagConstraints.gridy = 1;
    centerPanel.add(this.passwordField, gridBagConstraints);

    // For South Panel
    JPanel southPanel = new JPanel();
    FlowLayout southLayout = new FlowLayout();
    southLayout.setAlignment(FlowLayout.RIGHT);
    southPanel.setLayout(southLayout);
    this.loginButton = new JButton("Login");
    this.loginButton.setFont(new Font(FONT_NAME, Font.BOLD, FONT_SIZE));
    southPanel.add(this.loginButton);
    this.loginButton.addActionListener(this);

    // For Layout
    this.setLayout(new BorderLayout());
    this.getContentPane().add(northPanel, BorderLayout.NORTH);
    this.getContentPane().add(westPanel, BorderLayout.WEST);
    this.getContentPane().add(centerPanel, BorderLayout.CENTER);
    this.getContentPane().add(eastPanel, BorderLayout.EAST);
    this.getContentPane().add(southPanel, BorderLayout.SOUTH);

    this.setTitle(TITLE);
    this.setBounds(X_COORDINATE_SCREEN, Y_COORDINATE_SCREEN, WIDTH_SCREEN, HEIGHT_SCREEN);
    this.setResizable(IS_RESIZABLE);
    
    // Title ICON
    ImageIcon icon = new ImageIcon(ICON);
    this.setIconImage(icon.getImage());

    // For Close
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  }
  
  public void showScreen() {
    this.setVisible(true);
  }
  
  @Override
  public void actionPerformed(ActionEvent e) {
    if (this.idTextField.getText() == null || this.idTextField.getText().equals("")) {
      JOptionPane.showMessageDialog(this, "ID is empty", "Error", JOptionPane.ERROR_MESSAGE);
      return;
    } else if (this.passwordField.getPassword() == null || this.passwordField.getPassword().length == 0) {
      JOptionPane.showMessageDialog(this, "Password is empty", "Error", JOptionPane.ERROR_MESSAGE);
      return;
    }
    
    String id = this.idTextField.getText();
    String password = new String(this.passwordField.getPassword());
    if (ID.equals(id) && PASSWORD.equals(password)) {
      OtherView view = new OtherView();
      view.setVisible(true);
      this.setVisible(false);
    } else {
      JOptionPane.showMessageDialog(this, "Login is failed...", "FAILED...", JOptionPane.INFORMATION_MESSAGE);
    }
  }
}

OtherView.java

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class OtherView extends JFrame implements ActionListener {
  private static final long serialVersionUID = 1L;
  JButton button;

  public OtherView() {
    // 閉じるボタン押下時のアプリケーションの振る舞いを決定
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // ウィンドウの初期サイズ(幅、高さ)をピクセル単位で設定
    this.setSize(320, 160);
    // ウィンドウの表示場所を規定
    this.setLocationRelativeTo(null);
    // タイトルの表示
    this.setTitle("Login is successful!");
    // JFrameよりContentPaneを取得
    Container contentPane = this.getContentPane();
    // ラベルのインスタンスを生成
    JLabel label = new JLabel("Login is successful!");
    // ボタンのインスタンスを生成
    this.button = new JButton("Return");
    // ボタンにイベントを追加
    this.button.addActionListener(this);
    // ラベルをContentPaneに配置
    contentPane.add(label, BorderLayout.CENTER);
    // ボタンをContentPaneに配置
    contentPane.add(this.button, BorderLayout.SOUTH);
  }

  @Override
  public void actionPerformed(ActionEvent ae) {
    if (ae.getSource() == this.button) {
      LoginView loginView = new LoginView();
      loginView.setVisible(true);
      this.setVisible(false);
    } else {
      System.exit(0);
    }
  }
}


関連記事

モジュール提供用テンプレートを作ってみた Part1

http://blogs.yahoo.co.jp/dk521123/35027281.html

モジュール提供用テンプレートを作ってみた Part2

http://blogs.yahoo.co.jp/dk521123/36550446.html

モジュール提供用テンプレートを作ってみた Part3 ~Login画面~

http://blogs.yahoo.co.jp/dk521123/36572567.html

Swing で、画面移動

http://blogs.yahoo.co.jp/dk521123/33127585.html