| ■swingコンポーネント->JColorChooser | |||||||||
|
JColorChooserは、色の操作や選択を行うためのコンポーネントです。 普通のコンポーネントのようにコンテナに配置もできますが、createDialogメソッドでダイアログ表示もできます。 |
|||||||||
|
|
|||||||||
| コンストラクタ(抜粋) | |||||||||
|
|||||||||
| メソッド(抜粋) | |||||||||
|
|||||||||
|
|
|||||||||
| JColorSample.java | |||||||||
| import java.awt.*; | |||||||||
| import java.awt.event.*; | |||||||||
| import javax.swing.*; | |||||||||
| public class JColorSample extends JFrame implements ActionListener{ | |||||||||
| Container contentPane; | |||||||||
| JButton btn = new JButton("ColorDialog"); | |||||||||
| JLabel lblColor = new JLabel("色の選択"); | |||||||||
| JLabel lblText = new JLabel(""); | |||||||||
| JPanel pnl = new JPanel(); | |||||||||
| JColorChooser colorChooser; | |||||||||
| JDialog dialog; | |||||||||
| public JColorSample() { | |||||||||
| super(ColorSample); | |||||||||
| addWindowAdapter(new WindowListener() { | |||||||||
| public void windowClosing(WindowEvent e) { | |||||||||
| System.exit(0); | |||||||||
| } | |||||||||
| }); | |||||||||
| contentPane = getContentPane(); | |||||||||
| contentPane.setLayout(new BorderLayout()); | |||||||||
| pnl.setLayout(new GridLayout(1,2)); | |||||||||
| pnl.add(lblColor); | |||||||||
| pnl.add(btn); | |||||||||
| contentPane.add("North",pnl); | |||||||||
| contentPane.add("South",lblText); | |||||||||
| btn.addActionListener(this); | |||||||||
| colorChooser = new JColorChooser(); | |||||||||
| pack(); | |||||||||
| setVisible(true); | |||||||||
| } | |||||||||
| public void actionPerformed(ActionEvent e){ | |||||||||
|
dialog
= JColorChooser.createDialog(this,"ColorChooser", false,colorChooser,new OKListener(),new CanselListener()); |
← @ | ||||||||
| dialog.setVisible(true); | ← A | ||||||||
| } | |||||||||
| public static void main(String[] args) { | |||||||||
| JColorSample myClass = new JColorSample(); | |||||||||
| } | |||||||||
| class OKListener implements ActionListener{ | ← B | ||||||||
| public void actionPerformed(ActionEvent e){ | |||||||||
| Color color = colorChooser.getColor(); | |||||||||
| lblColor.setForeground(color); | |||||||||
| lblText.setText("色を選択しました。"); | |||||||||
| } | |||||||||
| } | |||||||||
| class CanselListener implements ActionListener{ | ← C | ||||||||
| public void actionPerformed(ActionEvent e){ | |||||||||
| lblText.setText("キャンセルしました。"); | |||||||||
| } | |||||||||
| } | |||||||||
| } | |||||||||
| @dialog
= JColorChooser.createDialog(this,"ColorChooser", false,colorChooser,new OKListener(),new CanselListener()); デフォルトのカラーチューザーを受け取り、dialogに配置しています。 |
|||||||||
| Adialog.setVisible(true); dialogを表示させています。 |
|||||||||
| Bclass
OKListener implements ActionListener{} カラーチューザーの「了解」をクリックした時のアクションイベントリスナー。 選択した色を受け取り、ラベルの文字色を変更しています。 |
|||||||||
| C class
CanselListener implements ActionListener{} カラーチューザーの「取り消し」をクリックした時のアクションイベントリスナー。 |
|||||||||
| もどる | |||||||||