//main
public class Main {
public static void main( String args[] )
{
KeyFrame frame = new KeyFrame();//创建窗口
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

JMenuBar menubar = new JMenuBar();//添加菜单
frame.setJMenuBar(menubar);
JMenu edit = new JMenu("edit");
JMenu option = new JMenu("Option");
menubar.add(edit);
menubar.add(option);
JMenuItem start = new JMenuItem("start");
JMenuItem end = new JMenuItem("end");
JMenuItem colorSet = new JMenuItem("color);
option.add(start);
option.add(help);
option.add(colorSet);//在option菜单中有三个选项,colorSet想把面板的颜色设为默认的red colorSet.addActionListener(new colorAction());
//这里为colorSet添加一个监控,但问题来了,colorAction在哪里定义呢?因为actionPerformed
                函数中药包括setBackground(Color.red);在这里定义的话setBackground未定义...
                如果在面板keyFrame中定义成内部类的话,这里又不能访问到
}
}public class KeyPanel extends JPanel{
  //面板
}//窗口
public class KeyFrame extends JFrame{
public KeyFrame()
{
setSize(600,600);
setLocation(100,100);
setTitle("keyEvent....");

KeyPanel panel = new KeyPanel();
add(panel);
}
}请高手赐教,谢谢啦!!!

解决方案 »

  1.   

    怎么不能访问呢?要理解面向对象的含义,每个对象都是通过信息互相通知具体要干什么的,只要的class里面含有要用到对象的引用,就能够用到该对象的信息了
      

  2.   

     
    那怎么引用面板Mypanel来实现在ColorSet中的SetBackground呢?