3

小さなボックス (myMessagePanel - 以下のコードを参照) を含むパネル (JPanel によって拡張) を使用して単純なアプリケーションを作成しました。

基本的に、ボックスが押されたときに、パネルとボックスの両方から focusevents を生成したいと考えています。ただし、Panel の focusGained と focusLost のみが呼び出されます。コードは次のとおりです。

myMessageBox はボックス、Panel はパネル、FocusListenerTest にはメインが含まれています。

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

class Panel extends JPanel implements MouseListener, FocusListener {
myMessageBox msgBox;

public Panel() {
    addMouseListener(this);
    addFocusListener(this);
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    setBackground(Color.GREEN);
    msgBox = new myMessageBox(g);
    g.dispose();
}

public void focusGained(FocusEvent fe) {
    System.out.println("Focus gained in JPanel");
}

public void focusLost(FocusEvent fe){
    System.out.println("Focus lost in JPanel");
}       

public void mousePressed(MouseEvent me) {
    requestFocus();
    System.out.println("Mouse Pressed in JPanel");
}
public void mouseReleased(MouseEvent me) {}
public void mouseClicked(MouseEvent me) {}
public void mouseEntered(MouseEvent me) {}
public void mouseExited(MouseEvent me) {}
}

class myMessageBox extends Component implements FocusListener, MouseListener{
myMessageBox(Graphics g) {
    addMouseListener(this);
    addFocusListener(this);
    paintComponent(g);
    System.out.println("Done");
}

public void paintComponent(Graphics g) {
    g.fillRect(150,100,100,100);
}

public void mousePressed(MouseEvent me) {
    requestFocus();
    System.out.println("Mouse pressed on box");
}

public void mouseReleased(MouseEvent me) {}
public void mouseClicked(MouseEvent me) {}
public void mouseEntered(MouseEvent me) {}
public void mouseExited(MouseEvent me) {}

public void focusGained(FocusEvent fe){
    System.out.println("Focus gained by box");
}

public void focusLost(FocusEvent fe) {
    System.out.println("Focus lost by box");
}
}

public class FocusListenertest {
public static void main(String args[]) {
    JFrame window = new JFrame();
    Panel content = new Panel();
    window.setContentPane(content);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setLocation(100,75);
    window.setSize(400, 400);
    window.setVisible(true);
}
}

このコードを実行すると、myMessageBox が押されたときに「ボックスによって獲得されたフォーカス」が出力されません。

ありがとう

(追伸:簡単なアプリなのでコメントは書いていません。コメントが必要な場合はお知らせください)

4

2 に答える 2

4

実際、msgBoxあなたのパネルにはありません - パネルの上に描かれているだけですが、構造の観点からは、まったく存在しません。msgBox標準的な JPanel のaddメソッドを使用して、パネルの子リストに追加する必要があります。クラスは、 AWTからではなく Swing からmyMessageBoxを継承する必要があります。その場合、メソッドは Swing のデフォルトをオーバーライドし、その親から自動的に呼び出されます。JComponentComponentpaintComponentPanel

を使用して新しいコンポーネントのサイズを設定する必要がありますsetSize。の正確な位置は、のレイアウトmsgBoxによって異なります。のレイアウトがである場合は、 でPanel明示的に設定できます。または、より快適なレイアウトのいずれかを使用することもできます。setLocationPanelnull

補足として、 Eclipse の VEプラグインなど、ある種のビジュアル エディタを使用することをお勧めします。

次の作業例を参照してください。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class SwingTestPanel extends JPanel implements MouseListener, FocusListener {
    MyMessageBox    msgBox    = new MyMessageBox();

    public SwingTestPanel(){
        initialize();
        addMouseListener(this);
        addFocusListener(this);
    }

    private void initialize(){
        this.setBackground(Color.GREEN);
        this.setLayout(null);
        this.setSize(new Dimension(446, 265));
        this.add(msgBox);
    }

    @Override
    public void focusGained(FocusEvent fe){
        System.out.println("Focus gained in JPanel");
    }

    @Override
    public void focusLost(FocusEvent fe){
        System.out.println("Focus lost in JPanel");
    }

    @Override
    public void mousePressed(MouseEvent me){
        requestFocus();
        System.out.println("Mouse Pressed in JPanel");
    }

    @Override
    public void mouseReleased(MouseEvent me){}

    @Override
    public void mouseClicked(MouseEvent me){}

    @Override
    public void mouseEntered(MouseEvent me){}

    @Override
    public void mouseExited(MouseEvent me){}

    static class MyMessageBox extends JComponent implements FocusListener, MouseListener {
        MyMessageBox(){
            initialize();
            addMouseListener(this);
            addFocusListener(this);
            System.out.println("Done");
        }

        private void initialize(){
            this.setName("msgBox");
            this.setEnabled(true);
            this.setSize(100, 100);
            this.setLocation(new Point(150, 100));
        }

        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.setColor(Color.black);
            g.fillRect(0, 0, getWidth(), getHeight());
        }

        @Override
        public void mousePressed(MouseEvent me){
            requestFocus();
            System.out.println("Mouse pressed on box");
        }

        @Override
        public void mouseReleased(MouseEvent me){}

        @Override
        public void mouseClicked(MouseEvent me){}

        @Override
        public void mouseEntered(MouseEvent me){}

        @Override
        public void mouseExited(MouseEvent me){}

        @Override
        public void focusGained(FocusEvent fe){
            System.out.println("Focus gained by box");
        }

        @Override
        public void focusLost(FocusEvent fe){
            System.out.println("Focus lost by box");
        }
    }

    public static void main(String args[]){
        JFrame window = new JFrame();
        SwingTestPanel content = new SwingTestPanel();
        window.setContentPane(content);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setLocation(100, 75);
        window.setSize(400, 400);
        window.setVisible(true);
    }
于 2011-03-19T05:56:57.247 に答える
-1

これはうまくいくはずです

class Panel extends JPanel implements MouseListener, FocusListener {
    myMessageBox msgBox;

    public Panel() {
        addMouseListener(this);
        addFocusListener(this);
        msgBox = new myMessageBox();
        setLayout(null);
        add(msgBox);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        setBackground(Color.GREEN);
    }

    @Override
    public void focusGained(FocusEvent fe) {
        System.out.println("Focus gained in JPanel");
    }

    @Override
    public void focusLost(FocusEvent fe) {
        System.out.println("Focus lost in JPanel");
    }

    @Override
    public void mousePressed(MouseEvent me) {
        requestFocus();
        System.out.println("Mouse Pressed in JPanel");
    }

    @Override
    public void mouseReleased(MouseEvent me) {
    }

    @Override
    public void mouseClicked(MouseEvent me) {
    }

    @Override
    public void mouseEntered(MouseEvent me) {
    }

    @Override
    public void mouseExited(MouseEvent me) {
    }

    public static void main(String args[]) {
        JFrame window = new JFrame();
        Panel content = new Panel();
        window.setContentPane(content);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setLocation(100, 75);
        window.setSize(400, 400);
        window.setVisible(true);
    }

}

class myMessageBox extends JComponent implements FocusListener, MouseListener {
    myMessageBox() {
        addMouseListener(this);
        addFocusListener(this);
        System.out.println("Done");
        setBounds(150, 100, 100, 100);
        setVisible(true);
    }

    public void paintComponent(Graphics g) {
        Rectangle r = getBounds();
        Color saveColor = g.getColor();
        try {
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, r.width, r.height);
        } finally {
            g.setColor(saveColor);
        }
    }

    @Override
    public void mousePressed(MouseEvent me) {
        requestFocus();
        System.out.println("Mouse pressed on box");
    }

    @Override
    public void mouseReleased(MouseEvent me) {
    }

    @Override
    public void mouseClicked(MouseEvent me) {
    }

    @Override
    public void mouseEntered(MouseEvent me) {
    }

    @Override
    public void mouseExited(MouseEvent me) {
    }

    @Override
    public void focusGained(FocusEvent fe) {
        System.out.println("Focus gained by box");
    }

    @Override
    public void focusLost(FocusEvent fe) {
        System.out.println("Focus lost by box");
    }
}
于 2011-03-19T05:51:11.267 に答える