3

我创建了一个简单的应用程序,其中包含一个面板(由 JPanel 扩展),其中包含一个小框(myMessagePanel - 请参见下面的代码)。

基本上,我想在按下框时从面板和框生成焦点事件。但是,只调用了 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 时不会打印“框获得的焦点”。

谢谢

(PS:没有写评论,因为它是一个简单的应用程序。如果需要评论,请告诉我)

4

2 回答 2

4

事实上,msgBox您的面板上没有 - 它只是在面板上绘制,但从结构的角度来看,它根本不存在。您需要msgBox使用标准 JPanel 的add方法添加到面板的子列表中。您的类必须从 SwingmyMessageBox继承 a ,而不是从 AWT,然后您的方法将覆盖 Swing 的默认值,并将自动从其父级调用。JComponentComponentpaintComponentPanel

您必须使用 设置新组件的大小setSize。的确切位置msgBox取决于您Panel的布局。它可以用setLocation, ifPanel的布局显式设置null,或者您可以使用更舒适的布局之一。

作为旁注,我建议您使用某种可视化编辑器,例如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 回答