【Java】【Swing】 ボタン ~ JButton ~

■ ボタン位置を右寄せにする

 * FlowLayoutで「FlowLayout.RIGHT」を指定する

サンプル・抜粋

http://blogs.yahoo.co.jp/dk521123/36550446.html
より抜粋
JPanel southPanel = new JPanel();

// ★FlowLayoutで「FlowLayout.RIGHT」をする★
FlowLayout southLayout = new FlowLayout();
southLayout.setAlignment(FlowLayout.RIGHT);
southPanel.setLayout(southLayout);

this.registButton = new JButton("Regist");
this.clearButton = new JButton("Clear");
southPanel.add(this.registButton);
southPanel.add(this.clearButton);

参考文献

http://www.javadrive.jp/tutorial/flowlayout/index3.html

■ イベント

1) ActionEvent
2) ChangeEvent
3) MouseEvent
4) MouseMotionListener
5) KeyListener
6) FocusListener

etc...

サンプル

EventDemo.java (ボタンをマウス押下・離すと、ボタンが拡大・縮小する)
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class EventDemo extends JFrame implements MouseListener {
  private static final long serialVersionUID = 1L;
  private JButton button;
  private JPanel panel;

  public static void main(String[] args) {
    EventDemo demo = new EventDemo();
    demo.setVisible(true);
  }

  public EventDemo() {
    this.button = new JButton("ボタン");
    this.button.addMouseListener(this);
    this.button.setPreferredSize(new Dimension(100, 50));

    this.panel = new JPanel();
    this.panel.setBounds(800, 200, 300, 200);
    this.panel.add(this.button);
    this.setLayout(new BorderLayout());
    this.setBounds(800, 200, 800, 800);
    this.getContentPane().add(panel, BorderLayout.CENTER);
  }

  @Override
  public void mouseClicked(MouseEvent e) {
  }

  @Override
  public void mousePressed(MouseEvent e) {
    this.zoom();
  }

  @Override
  public void mouseReleased(MouseEvent e) {
    this.reposit();
  }

  @Override
  public void mouseEntered(MouseEvent e) {
  }

  @Override
  public void mouseExited(MouseEvent e) {
  }

  private void zoom() {
    this.panel.setSize(600, 400);
    this.button.setPreferredSize(new Dimension(150, 100));

    this.repaint();
    this.revalidate();
  }

  private void reposit() {
    this.panel.setSize(300, 200);
    this.button.setPreferredSize(new Dimension(100, 50));

    this.repaint();
    this.revalidate();
  }
}

参考文献

http://www.javadrive.jp/tutorial/event/
http://www.javadrive.jp/tutorial/event/index1.html
http://www.tohoho-web.com/java/listener.htm