• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JButtonの描画
#navi(../)
RIGHT:Posted by [[aterai]] at 2009-08-24
*JButtonの描画 [#b1486d31]
JButtonの状態描画をテストします。
---
category: swing
folder: ButtonPainted
title: JButtonの描画
tags: [JButton, Focus, Icon]
author: aterai
pubdate: 2009-08-24T12:58:07+09:00
description: JButtonの設定を変更し、コンテンツ領域、フチ、フォーカスやロールオーバー状態がどう描画されるかをテストします。
image: https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTIWYXt9eI/AAAAAAAAASo/se2OKkQA83U/s800/ButtonPainted.png
---
* 概要 [#summary]
`JButton`の設定を変更し、コンテンツ領域、フチ、フォーカスやロールオーバー状態がどう描画されるかをテストします。

-&jnlp;
-&jar;
-&zip;
#download(https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTIWYXt9eI/AAAAAAAAASo/se2OKkQA83U/s800/ButtonPainted.png)

//#screenshot
#ref(http://lh3.ggpht.com/_9Z4BYR88imo/TQTIWYXt9eI/AAAAAAAAASo/se2OKkQA83U/s800/ButtonPainted.png)

**サンプルコード [#l599c4e7]
#code{{
java.util.List<JCheckBox> clist = Arrays.asList(
* サンプルコード [#sourcecode]
#code(link){{
List<JCheckBox> clist = Arrays.asList(
  new JCheckBox(new AbstractAction("setFocusPainted") {
    @Override public void actionPerformed(ActionEvent e) {
      boolean flg = ((JCheckBox)e.getSource()).isSelected();
      for(JButton b:list) b.setFocusPainted(flg);
      boolean flg = ((JCheckBox) e.getSource()).isSelected();
      for (JButton b: list) {
        b.setFocusPainted(flg);
      }
      p.revalidate();
    }
  }),
  new JCheckBox(new AbstractAction("setBorderPainted") {
    @Override public void actionPerformed(ActionEvent e) {
      boolean flg = ((JCheckBox)e.getSource()).isSelected();
      for(JButton b:list) b.setBorderPainted(flg);
      boolean flg = ((JCheckBox) e.getSource()).isSelected();
      for (JButton b: list) {
        b.setBorderPainted(flg);
      }
      p.revalidate();
    }
  }),
  new JCheckBox(new AbstractAction("setContentAreaFilled") {
    @Override public void actionPerformed(ActionEvent e) {
      boolean flg = ((JCheckBox)e.getSource()).isSelected();
      for(JButton b:list) b.setContentAreaFilled(flg);
      boolean flg = ((JCheckBox) e.getSource()).isSelected();
      for (JButton b: list) {
        b.setContentAreaFilled(flg);
      }
      p.revalidate();
    }
  }),
  new JCheckBox(new AbstractAction("setRolloverEnabled") {
    @Override public void actionPerformed(ActionEvent e) {
      boolean flg = ((JCheckBox)e.getSource()).isSelected();
      for(JButton b:list) b.setRolloverEnabled(flg);
      boolean flg = ((JCheckBox) e.getSource()).isSelected();
      for (JButton b: list) {
        b.setRolloverEnabled(flg);
      }
      p.revalidate();
    }
  })
);
}}

**解説 [#ua38e7ee]
上記のサンプルでは、JButtonの状態(たとえばフォーカスの有無を描画するか?など)をテストします。これらはLook & Feelによって効果が異なる場合があるようです。
* 解説 [#explanation]
上記のサンプルでは、フォーカスの有無を表示するかなどの設定を切り替えて`JButton`の描画をテストしています。これらは`Look & Feel`に依存しそれぞれ有効か無効かなどが異なります。

- [http://java.sun.com/javase/ja/6/docs/ja/api/javax/swing/AbstractButton.html#setFocusPainted(boolean) setFocusPainted]
- [http://java.sun.com/javase/ja/6/docs/ja/api/javax/swing/AbstractButton.html#setBorderPainted(boolean) setBorderPainted]
- [http://java.sun.com/javase/ja/6/docs/ja/api/javax/swing/AbstractButton.html#setContentAreaFilled(boolean) setContentAreaFilled]
- [http://java.sun.com/javase/ja/6/docs/ja/api/javax/swing/AbstractButton.html#setRolloverEnabled(boolean) setRolloverEnabled]
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/AbstractButton.html#setFocusPainted-boolean- setFocusPainted]
-- フォーカス状態の描画
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/AbstractButton.html#setBorderPainted-boolean- setBorderPainted]
-- ボーダー(フチの装飾)の描画
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/AbstractButton.html#setContentAreaFilled-boolean- setContentAreaFilled]
-- ボタンのコンテンツ領域(ボタンのテキストやアイコン以外の領域)の描画
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/AbstractButton.html#setRolloverEnabled-boolean- setRolloverEnabled]
-- ロールオーバー効果の描画

//**参考リンク
**コメント [#u75fef0a]
* 参考リンク [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/AbstractButton.html AbstractButton (Java Platform SE 8)]

* コメント [#comment]
#comment
#comment