- PR -

マウスクリックのイベントが受けられない

1
投稿者投稿内容
ばつまる
会議室デビュー日: 2004/01/04
投稿数: 1
投稿日時: 2004-01-06 18:22
JAVAをはじめたばかりなのですが教えてください。
JBuilder9を使用してアプリケーションを作成しています。

レイヤを使って一番下に"image.jpg"ファイルを表示し、
その上にマウスクリックした点を表示するコンポーネントを配置しています。

しかし、実行しただけの状態(★1)ではクリックしても
マウスクリックのイベントを受けてくれません。
ところが、ボタンを押したときに一度レイヤからはずし、再度設定しなおす(★2)と
表示されるようになります。

最初からイベントを受けるようにしたいのですが、何が悪いのでしょうか。
教えてください。
---------------------- Application.java
package layer_draw_test;
import javax.swing.UIManager;

public class Application {
boolean packFrame = false;
//アプリケーションのビルド
public Application() {
Frame1 frame = new Frame1();
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
frame.setVisible(true);
}
//Main メソッド
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new Application();
}
}
---------------------- Frame1.java
package layer_draw_test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Frame1 extends JFrame {
JPanel contentPane;
JButton jButton1 = new JButton();
JPanel jPanel1 = new JPanel();
DrawingComponent drawingComponent1 = new DrawingComponent();
JLayeredPane lpane = new JLayeredPane();
BorderLayout borderLayout1 = new BorderLayout();
FlowLayout flowLayout1 = new FlowLayout();
BorderLayout borderLayout2 = new BorderLayout();
FlowLayout flowLayout2 = new FlowLayout();
ImageIcon icon1 = new ImageIcon("image.jpg");
JLabel label1 = new JLabel((Icon)icon1);

//フレームのビルド
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//コンポーネントの初期化
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
jButton1.setText("jButton1");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel1.setLayout(flowLayout2);
contentPane.add(jButton1, BorderLayout.NORTH);
contentPane.add(jPanel1, BorderLayout.CENTER);
jPanel1.add(lpane);
//★1
SetDrawingComponent();
//レイヤ←ラベル
label1.setLocation(new java.awt.Point(0,0));
label1.setSize(new Dimension(lpane.getWidth(),lpane.getHeight()));
lpane.add(label1, JLayeredPane.DEFAULT_LAYER);
}


//ウィンドウが閉じられたときに終了するようにオーバーライド
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}

//Button1
void jButton1_actionPerformed(ActionEvent e) {
lpane.remove(drawingComponent1);
//★2
SetDrawingComponent();
}

//Drawingコンポーネント設定
private void SetDrawingComponent(){
//レイヤペイン←Drawingコンポーネント
drawingComponent1.setLocation(new java.awt.Point(0,0));
drawingComponent1.setSize(
new Dimension(lpane.getWidth(),lpane.getHeight())
);
drawingComponent1.setLayout(borderLayout2);
lpane.setLayout(flowLayout1);
lpane.add(drawingComponent1, JLayeredPane.PALETTE_LAYER);
}
}

class Frame1_jButton1_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
---------------------- DrawingComponent.java
package layer_draw_test;

import javax.swing.JComponent;
import java.awt.*;
import java.awt.event.*;

public class DrawingComponent extends JComponent {
//クリックした座標を記録する点
Point clic_p;

public DrawingComponent() {
//点の初期化
clic_p = new Point();
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}

private void jbInit() throws Exception {
this.addMouseListener(new DrawingComponent_this_mouseAdapter(this));
}

//マウスクリックしたときの処理
void this_mouseClicked(MouseEvent e) {
clic_p = (Point) e.getPoint();
//再描画
repaint();
}

//コンポーネントの描画
public void paintComponent(Graphics gra){
super.paintComponent(gra);
//点の描写
if ((clic_p.x != 0) && (clic_p.y != 0)){
gra.setColor(Color.red);
gra.fillOval(clic_p.x - 2, clic_p.y - 2, 4, 4);
gra.drawOval(clic_p.x - 8, clic_p.y - 8, 16, 16);
}
}
}

class DrawingComponent_this_mouseAdapter extends java.awt.event.MouseAdapter {
DrawingComponent adaptee;
DrawingComponent_this_mouseAdapter(DrawingComponent adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.this_mouseClicked(e);
}
}
1

スキルアップ/キャリアアップ(JOB@IT)