为menuItem[][]数组添加监听报错
有两个类,ItemTestMain(用于显示界面,JMenu对象Menu有JMenuItem对象menuItem,File有item[0][0],item[0][1],Edit有item[1][0]和item[1][1] ) ;
类ItemControl对象itemControl用于监听JMenuItem对象.
核心语句及问题:
menuItem.addActionListener(itemControl);//这里测试成功,
jMenuItems[i][j].addActionListener(itemControl);//为各菜单选项添加监听;这里时报错,单击要么没反应,要么出错,
//File:ItemTestMain.java
package itemtest;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.UIManager;
import treetest.TreeTest;
public class ItemTestMain extends JFrame{
private JPanel pane=null;
private JMenuBar bar=null;
private JMenu menua=null;
public JMenuItem item=null;
private JMenu[] jMenus = null;//菜单项数组;
private String[] jMenusStr = {"File", "Edit"};//用于定义菜单项的内容
public JMenuItem[][] jMenuItems =null;//下拉菜单项;
private String[][] jMenuItemsStr = {
{ "Aa", "Ab", "Ac"}, {"Undo typing", "Bb"}};//定义下拉菜单项的显示内容;
ItemControl itemControl=null;
/**
*
*/
public ItemTestMain() {
super();
// TODO Auto-generated constructor stub
pane=new JPanel(new FlowLayout());
setSize(500, 310);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
initialize();
this.add(pane);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void initialize() {
// TODO Auto-generated method stub
itemControl=new ItemControl(this);
bar=new JMenuBar();
menua=new JMenu("Menu");
item=new JMenuItem("MenuItem");
item.addActionListener(itemControl);
menua.add(item);
bar.add(menua);
pane.add(bar);
jMenus = new JMenu[jMenusStr.length];
jMenuItems = new JMenuItem[jMenuItemsStr.length][];//一维定义——定义jMenuItems行数;jMenuItemsStr.length=13
for (int i = 0; i < jMenusStr.length; i++) { //jMenusStr.length=13
jMenus[i] = new JMenu(jMenusStr[i]);
jMenuItems = new JMenuItem[jMenuItemsStr.length][jMenuItemsStr[i].length];//二维定义——定义jMenuItems列数
for (int j = 0; j < jMenuItemsStr[i].length; j++) {
jMenuItems[i][j] = new JMenuItem(jMenuItemsStr[i][j]);//初始化各jMenuItems;
jMenus[i].add(jMenuItems[i][j]);
jMenuItems[i][j].addActionListener(itemControl);//为各菜单选项添加监听;
}
bar.add(jMenus[i]);
}
}
public void actionshow(String str){//响应单击System.out.println("Str="+str);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new ItemTestMain();
}
} /*******************************/
//File:ItemControl
package itemtest;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ItemControl implements ActionListener {
ItemTestMain frame = null;
/**
*
*/
public ItemControl(ItemTestMain frame) {
super();
// TODO Auto-generated constructor stub
this.frame = frame;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object source = e.getSource();
if (source == frame.item) {
System.out.println("SS8=" + frame.item.getText());
frame.actionshow(frame.item.getText());
}
if (source == frame.jMenuItems[0][1])// Aa
{
System.out.println("SS00=" + frame.jMenuItems[0][0].getText());
frame.actionshow(frame.jMenuItems[0][0].getText());
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论