java swing n coを使用してGUIプログラミングを行うのはこれが初めてなので、アドバイスが必要です。現在、ボタンにアクションコマンドを設定して、ボタンに機能を追加しています。次に、コンテナで次のようなアクションをリッスンします。
colorButton.setText("Select Color");
colorButton.setFocusable(false);
colorButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
colorButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jToolBar1.add(colorButton);
jToolBar1.add(jSeparator1);
colorButton.setActionCommand("selectColor");
colorButton.addActionListener(this);
次に、以下のようなスニペットを使用して、実行されたアクションが実行されたコンポーネントを確認します。
else if("selectColor".equals(e.getActionCommand())) {
Color c = JColorChooser.showDialog(this, "Select Color", Color.GREEN);
if (selectedShape != null) {
undoStack.add(copyOf(model.getAllShapes()));
model.setShapeColor(selectedShape, c);
}
else{
defaultColor = c;
}
}
これが良い習慣なのか悪い習慣なのか知りたいだけですか?