import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.BevelBorder;
/***************************************************************
背景色の異なるラベルを縦に並べて設置しクリックされたラベルの背景色を取得。<br>
取得したラベルの背景色にフレームの背景色を変更。再度画面をクリックすると最初の画面に戻る。
****************************************************************/
public class ColorSelect
extends JFrame { private static final long serialVersionUID = 1L;
ColorSelect() {
// メインフレームの初期化
this.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE); this.setTitle("ColorSelect");
this.setBounds(200, 200, 480, 600);
this.setResizable(false);
// カードレイアウトで切り替える部分
// 初期状態のカード(card1)
//card1.setLayout(new GridLayout(3, 3));
addLabel(card1);
card1.setName("card1");
// 切り替え用のカード(card2)
lblCard2
= new JLabel("Test fields on Card2",
JLabel.
CENTER); initLabelDefault
(lblCard2,
Color.
BLACK); card2.setName("card2");
JTextArea txtInfo
= new JTextArea("下の画面を左クリックすると前の画面へ戻ります。\n右クリックすると#付きカラーコードをクリップボードへコピーします。"); txtInfo.
setFont(new Font("メイリオ",
Font.
BOLD,
12)); txtInfo.setEditable(false);
cardPanel.add(card1, "card1");
cardPanel.add(card2, "card2");
// コンテントペインにパネルを追加
}
// ラベルの追加処理
private void addLabel
(JPanel pnl
) { // RGB各カラーの値を格納(赤系色、緑系色、青系色を各3種ずつ)
int[] intR = {255, 255, 255,
0, 153, 255,
0, 0, 0}; // RGB 赤色
int[] intG = {0, 0, 0,
255, 255, 255,
0, 153, 255}; // RGB 緑色
int[] intB = {0, 153, 255,
0, 0, 0,
255, 255, 255}; // RGB 青色
String strHexColor
; // カラーコード(16進数)
for (int i = 0; i < intR.length; i++){
initLabelDefault
(lbl,
new Color(intR
[i
], intG
[i
], intB
[i
])); // ラベルの初期設定
// カラーコード(RGBを各職ごとに16真数に変換。例:255→FF)
strHexColor
= (Integer.
toHexString(intR
[i
] >>> 4) + Integer.
toHexString(intR
[i
] & 0xF
) + Integer.
toHexString(intG
[i
] >>> 4) + Integer.
toHexString(intG
[i
] & 0xF
) + Integer.
toHexString(intB
[i
] >>> 4) + Integer.
toHexString(intB
[i
] & 0xF
)).
toUpperCase();
lbl.setText("#" + strHexColor);
lbl.
setForeground(new Color(~intR
[i
]&255, ~intG
[i
]&255, ~intB
[i
]&255)); // 背景色を反転して文字色に設定 }
}
// ラベルの初期設定(共通)
private void initLabelDefault
(JLabel lb,
Color bgColor
) { lb.
setFont(new Font("Arial",
Font.
PLAIN,
30)); lb.
setForeground(Color.
black); lb.setBackground(bgColor);
lb.setOpaque(true);
lb.addMouseListener(new LabelMouseListener());
}
// マウスイベント処理(ラベル用)
@Override
JPanel jp
= (JPanel)lbClicked.
getParent(); // クリックされたラベルのペアレントを取得
if (jp.getName() == "card1") {
// 左クリック
if(e.
getButton() == MouseEvent.
BUTTON1 && e.
getClickCount() < 2) {
// card2のラベルへクリックされたラベルの背景色、文字色、カラーコードを設定
lblCard2.setBackground(lbClicked.getBackground());
lblCard2.setForeground(lbClicked.getForeground());
lblCard2.setText(lbClicked.getText());
cl.show(cardPanel, "card2");
}
} else if (jp.getName() == "card2") {
// 左クリック
if (e.
getButton() == MouseEvent.
BUTTON1 && e.
getClickCount() < 2) { cl.show(cardPanel, "card1");
} else // 右クリック
if (e.
getButton() == MouseEvent.
BUTTON3 && e.
getClickCount() < 2) { clipBoard.setContents(str, str);
}
}
}
}
public static void main
(String[] args
) { try {
JFrame mainFrame
= new ColorSelect
(); mainFrame.setVisible(true);
System.
out.
println(e.
toString()); }
}
}
aW1wb3J0IGphdmEuYXd0LkJvcmRlckxheW91dDsKaW1wb3J0IGphdmEuYXd0LkNhcmRMYXlvdXQ7CmltcG9ydCBqYXZhLmF3dC5Db2xvcjsKaW1wb3J0IGphdmEuYXd0LkRpbWVuc2lvbjsKaW1wb3J0IGphdmEuYXd0LkZvbnQ7CmltcG9ydCBqYXZhLmF3dC5Ub29sa2l0OwppbXBvcnQgamF2YS5hd3QuZGF0YXRyYW5zZmVyLkNsaXBib2FyZDsKaW1wb3J0IGphdmEuYXd0LmRhdGF0cmFuc2Zlci5TdHJpbmdTZWxlY3Rpb247CmltcG9ydCBqYXZhLmF3dC5ldmVudC5Nb3VzZUFkYXB0ZXI7CmltcG9ydCBqYXZhLmF3dC5ldmVudC5Nb3VzZUV2ZW50OwppbXBvcnQgamF2YXguc3dpbmcuQm94TGF5b3V0OwppbXBvcnQgamF2YXguc3dpbmcuSkZyYW1lOwppbXBvcnQgamF2YXguc3dpbmcuSkxhYmVsOwppbXBvcnQgamF2YXguc3dpbmcuSlBhbmVsOwppbXBvcnQgamF2YXguc3dpbmcuSlRleHRBcmVhOwppbXBvcnQgamF2YXguc3dpbmcuYm9yZGVyLkJldmVsQm9yZGVyOwoKLyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKgrog4zmma/oibLjga7nlbDjgarjgovjg6njg5njg6vjgpLnuKbjgavkuKbjgbnjgaboqK3nva7jgZfjgq/jg6rjg4Pjgq/jgZXjgozjgZ/jg6njg5njg6vjga7og4zmma/oibLjgpLlj5blvpfjgII8YnI+CuWPluW+l+OBl+OBn+ODqeODmeODq+OBruiDjOaZr+iJsuOBq+ODleODrOODvOODoOOBruiDjOaZr+iJsuOCkuWkieabtOOAguWGjeW6pueUu+mdouOCkuOCr+ODquODg+OCr+OBmeOCi+OBqOacgOWIneOBrueUu+mdouOBq+aIu+OCi+OAggoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqLwpwdWJsaWMgY2xhc3MgQ29sb3JTZWxlY3QgZXh0ZW5kcyBKRnJhbWUgewoJcHJpd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