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

サンプル

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.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

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

  private static final String FONT_NAME = "Arial";
  private static final int FONT_SIZE = 30;
  
  private static final String TITLE = "This is title";
  private static final String INPUT_TITLE = "Group Title";
  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 textField1;
  private JTextField textField2;
  private JTextField textField3;
  private JButton registButton;
  private JButton clearButton;
  private JMenuItem menuItem1;
  
  public static void main(String[] args) {
    SwingBase view = new SwingBase();
    view.showScreen();
  }

  public SwingBase() {
    // Menu
    JMenuBar menuBar = new JMenuBar();
    JMenu menu1 = new JMenu("AAA");
    menuBar.add(menu1);
    this.menuItem1 = new JMenuItem("BBB");
    this.menuItem1.addActionListener(this);
    menu1.add(this.menuItem1);
    this.setJMenuBar(menuBar);
    
    // 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.textField1 = new JTextField();
    this.textField1.setFont(new Font(FONT_NAME, Font.PLAIN, FONT_SIZE));
    this.textField2 = new JTextField();
    this.textField2.setFont(new Font(FONT_NAME, Font.PLAIN, FONT_SIZE));
    this.textField3 = new JTextField();
    this.textField3.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("Item1 :");
    item1.setFont(new Font(FONT_NAME, Font.BOLD, FONT_SIZE));
    centerPanel.add(item1, gridBagConstraints);
    gridBagConstraints.gridy = 1;
    JLabel item2 = new JLabel("Item2 :");
    item2.setFont(new Font(FONT_NAME, Font.BOLD, FONT_SIZE));
    centerPanel.add(item2, gridBagConstraints);
    gridBagConstraints.gridy = 2;
    JLabel item3 = new JLabel("Item3 :");
    item3.setFont(new Font(FONT_NAME, Font.BOLD, FONT_SIZE));
    centerPanel.add(item3, gridBagConstraints);

    gridBagConstraints.gridx = 1;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    gridBagConstraints.gridy = 0;
    centerPanel.add(this.textField1, gridBagConstraints);
    gridBagConstraints.gridy = 1;
    centerPanel.add(this.textField2, gridBagConstraints);
    gridBagConstraints.gridy = 2;
    centerPanel.add(this.textField3, gridBagConstraints);

    // For South Panel
    JPanel southPanel = new JPanel();
    FlowLayout southLayout = new FlowLayout();
    southLayout.setAlignment(FlowLayout.RIGHT);
    southPanel.setLayout(southLayout);
    this.registButton = new JButton("Regist");
    this.registButton.setFont(new Font(FONT_NAME, Font.BOLD, FONT_SIZE));
    this.clearButton = new JButton("Clear");
    this.clearButton.setFont(new Font(FONT_NAME, Font.BOLD, FONT_SIZE));
    southPanel.add(this.registButton);
    southPanel.add(this.clearButton);
    this.registButton.addActionListener(this);
    this.clearButton.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);
  }

  @Override
  public void actionPerformed(ActionEvent actionEvent) {
    Object control = actionEvent.getSource();
    if (control == this.registButton) {
      this.regist();
    } else if (control == this.clearButton) {
      this.clear();
    } else if (control == this.menuItem1) {
      //FIXME ここを直す
      JOptionPane.showMessageDialog(this, "Menu", "Info", JOptionPane.INFORMATION_MESSAGE);
    }
  }

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

  public void regist() {
    if (this.textField1.getText() == null || this.textField1.getText().equals("")) {
      JOptionPane.showMessageDialog(this, "textField1 is empty", "Error", JOptionPane.ERROR_MESSAGE);
      return;
    } else if (this.textField2.getText() == null || this.textField2.getText().equals("")) {
      JOptionPane.showMessageDialog(this, "textField2 is empty", "Error", JOptionPane.ERROR_MESSAGE);
      return;
    } else if (this.textField3.getText() == null || this.textField3.getText().equals("")) {
      JOptionPane.showMessageDialog(this, "textField3 is empty", "Error", JOptionPane.ERROR_MESSAGE);
      return;
    }

    // TODO Do something

    JOptionPane.showMessageDialog(this, "Done!", "Info", JOptionPane.INFORMATION_MESSAGE);
  }

  public void clear() {
    this.textField1.setText("");
    this.textField2.setText("");
    this.textField3.setText("");
  }
}


関連記事

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

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

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

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

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

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