我用Panel创建了一个简单的应用程序(由JPanel扩展),其中包含一个小方框(myMessagePanel - 请参阅下面的代码)。
基本上,我想在按下框时从Panel和框中生成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时不会打印“按框获取的焦点”。
由于
(PS:没有写评论,因为它是一个简单的应用程序。如果需要评论,请告诉我)
答案 0 :(得分:3)
事实上,您的面板上没有msgBox
- 它只是在面板上绘制,但从结构的角度来看,它根本不存在。您需要使用标准JPanel的msgBox
方法将add
添加到面板的子列表中。您的myMessageBox
类必须从Swing继承JComponent
,而不是从AWT继承Component
,那么您的paintComponent
方法将覆盖Swing的默认值,并且会自动从其父级调用Panel
{1}}。
您必须使用setSize
设置新组件的大小。 msgBox
的确切位置取决于您的Panel
布局。如果setLocation
的布局为Panel
,则可以使用null
明确设置,也可以使用其中一种更舒适的布局。
作为旁注,我建议您使用某种可视化编辑器,例如Eclipse's 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);
}
答案 1 :(得分:0)
这应该有效
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");
}
}