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

はじめに

https://blogs.yahoo.co.jp/dk521123/36572567.html
でやったログイン画面だが、今度は、BoxLayoutで作ってみた。
BoxLayout
https://blogs.yahoo.co.jp/dk521123/36553511.html
https://blogs.yahoo.co.jp/dk521123/36664347.html

サンプル

LoginFrame.java

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
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;

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

  private static final String ID = "admin";
  private static final String PASSWORD = "password";

  private static final Dimension ITEM_SIZE = new Dimension(200, 40);
  private static final Dimension FIELD_SIZE = new Dimension(250, 40);

  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 SUB_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;
  
  /**
   * コンストラクタ.
   */
  public LoginFrame() {
    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

    // [1] ヘッダー
    JPanel headerPanel = new JPanel();
    JLabel titleLabel = new JLabel(SUB_TITLE);
    titleLabel.setFont(new Font(FONT_NAME, Font.BOLD, FONT_SIZE));
    headerPanel.add(titleLabel);

    // [2] ボディ
    JPanel bodyPanel = new JPanel();
    bodyPanel.setLayout(new BoxLayout(bodyPanel, BoxLayout.Y_AXIS));
    JPanel idPanel = new JPanel();
    idPanel.setLayout(new BoxLayout(idPanel, BoxLayout.X_AXIS));
    JLabel idLabel = new JLabel("ID : ");
    idLabel.setFont(new Font(FONT_NAME, Font.BOLD, FONT_SIZE));
    idLabel.setPreferredSize(ITEM_SIZE);
    idLabel.setMinimumSize(ITEM_SIZE);
    idLabel.setMaximumSize(ITEM_SIZE);
    idPanel.add(idLabel);
    this.idTextField = new JTextField();
    this.idTextField.setFont(new Font(FONT_NAME, Font.BOLD, FONT_SIZE));
    this.idTextField.setPreferredSize(FIELD_SIZE);
    this.idTextField.setMinimumSize(FIELD_SIZE);
    this.idTextField.setMaximumSize(FIELD_SIZE);
    idPanel.add(this.idTextField);
    bodyPanel.add(idPanel);

    bodyPanel.add(Box.createVerticalGlue());

    JPanel passwordPanel = new JPanel();
    passwordPanel.setLayout(new BoxLayout(passwordPanel, BoxLayout.X_AXIS));
    JLabel passwordLabel = new JLabel("Password : ");
    passwordLabel.setFont(new Font(FONT_NAME, Font.BOLD, FONT_SIZE));
    passwordLabel.setPreferredSize(ITEM_SIZE);
    passwordLabel.setMinimumSize(ITEM_SIZE);
    passwordLabel.setMaximumSize(ITEM_SIZE);
    passwordPanel.add(passwordLabel);
    this.passwordField = new JPasswordField();
    this.passwordField.setFont(new Font(FONT_NAME, Font.BOLD, FONT_SIZE));
    this.passwordField.setPreferredSize(FIELD_SIZE);
    this.passwordField.setMinimumSize(FIELD_SIZE);
    this.passwordField.setMaximumSize(FIELD_SIZE);
    passwordPanel.add(this.passwordField);
    bodyPanel.add(passwordPanel);

    bodyPanel.add(Box.createVerticalGlue());

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
    JButton loginButton = new JButton("Login");
    loginButton.setFont(new Font(FONT_NAME, Font.BOLD, FONT_SIZE));
    loginButton.setAlignmentX(Component.RIGHT_ALIGNMENT);
    loginButton.addActionListener(this);
    buttonPanel.add(loginButton);
    bodyPanel.add(buttonPanel);

    bodyPanel.add(Box.createVerticalGlue());

    // [3] フッター
    JPanel footerPanel = new JPanel();

    mainPanel.add(Box.createVerticalGlue());
    mainPanel.add(headerPanel);
    mainPanel.add(Box.createVerticalGlue());
    mainPanel.add(bodyPanel);
    mainPanel.add(Box.createVerticalGlue());
    mainPanel.add(footerPanel);
    mainPanel.add(Box.createVerticalGlue());
    this.add(mainPanel);
    
    // 
    ImageIcon icon = new ImageIcon(ICON);
    this.setIconImage(icon.getImage());
    this.pack();
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setBounds(X_COORDINATE_SCREEN, Y_COORDINATE_SCREEN, WIDTH_SCREEN, HEIGHT_SCREEN);
    this.setTitle(TITLE);
    this.setResizable(IS_RESIZABLE);
  }

  public void showFrame() {
    this.setVisible(true);
  }

  public static void main(String[] args) {
    LoginFrame frame = new LoginFrame();
    frame.showFrame();
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    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

 * 以下の関連記事を参照のこと
https://blogs.yahoo.co.jp/dk521123/36572567.html

課題

 * やってみておもったが、実際に業務で使うなら、
   Swingのコンポーネント(JLabel、JButtonなど)はラップして使った方がいい

関連記事

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

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

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

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

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

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

レイアウト [4] ~ BoxLayout 編 - [1] ~

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

レイアウト [4] ~ BoxLayout 編 - [2] ~

https://blogs.yahoo.co.jp/dk521123/36664347.html