java编程菜单项监听
最近用java做一个程序,此处是一个菜单,想实现点击菜单里的一个菜单项然后跳到另一个窗口,但是调试过程中出现了问题,菜单项监听有错误,请高手帮忙看看,回答正确的有分数献上...
最近 用java做一个程序,此处是一个菜单,想实现点击菜单里的一个菜单项然后跳到另一个窗口,但是调试过程中出现了问题,菜单项监听有错误,请高手帮忙看看,回答正确的有分数献上!!!(错误处已在程序中注明)
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
public class MenuDesign extends JFrame implements ActionListener
{
public MenuDesign(String strTitle)
{
super(strTitle);
JMenuBar mnuMnuBar=new JMenuBar();
this.setJMenuBar(mnuMnuBar);
JMenu txl=new JMenu("通讯录");
JMenuItem Tin=new JMenuItem("T录入");
JMenuItem Tout=new JMenuItem("T查询");
JMenu rj=new JMenu("日记");
JMenuItem Rin=new JMenuItem("R录入");
JMenuItem Rout=new JMenuItem("R查询");
JMenu about=new JMenu("关于");
JMenuItem bj=new JMenuItem("背景");
//添加通讯录菜单Item
txl.add(Tin);
txl.add(Tout);
//添加日记菜单Item
rj.add(Rin);
rj.add(Rout);
//添加关于菜单Item
about.add(bj);
//将菜单加入菜单栏
mnuMnuBar.add(txl);
mnuMnuBar.add(rj);
mnuMnuBar.add(about);
//添加监听
Tin.addActionListener(this);
//添加背景图片
final ImageIcon icon = new ImageIcon("menu.jpg");
JPanel panel = new JPanel()
{
public void paintComponent(Graphics g)
{
g.drawImage(icon.getImage(), 0, 0, null);
super.paintComponent(g);
}
};
panel.setOpaque( false );
Container container=this.getContentPane();
container.add(panel);
this.setSize(new Dimension(400,300));
this.setVisible(true);
}
public Dimension getPerreSize()
{
return new Dimension(800,800);
}
public void actionPerformed(ActionEvent evt){
if (evt.getSource()instanceof JMenuItem)//判断事件源是否为菜单项
{
String ItemName = evt.getActionCommand();
if (ItemName.equals("T录入"))
//这个if (ItemName.equals("T录入"))地方错误,提示信息如下:Multiple markers at this line- Syntax error on token ")", delete this token
//- Syntax error, insert "}" to complete Statement
//- Syntax error, insert ") Statement" to complete
{
//此处为其他代码!已省略
}
}
}
public static void main(String args[])
{
MenuDesign frmMenu=new MenuDesign("个人信息系统");
frmMenu.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
}
省略部分代码就算不用也不行,我用jdk1.5,错误在这if (ItemName.equals("T录入"))
//这个if (ItemName.equals("T录入"))地方错误,提示信息如下:Multiple markers at this line- Syntax error on token ")", delete this token
//- Syntax error, insert "}" to complete Statement
//- Syntax error, insert ") Statement" to complete 展开
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
public class MenuDesign extends JFrame implements ActionListener
{
public MenuDesign(String strTitle)
{
super(strTitle);
JMenuBar mnuMnuBar=new JMenuBar();
this.setJMenuBar(mnuMnuBar);
JMenu txl=new JMenu("通讯录");
JMenuItem Tin=new JMenuItem("T录入");
JMenuItem Tout=new JMenuItem("T查询");
JMenu rj=new JMenu("日记");
JMenuItem Rin=new JMenuItem("R录入");
JMenuItem Rout=new JMenuItem("R查询");
JMenu about=new JMenu("关于");
JMenuItem bj=new JMenuItem("背景");
//添加通讯录菜单Item
txl.add(Tin);
txl.add(Tout);
//添加日记菜单Item
rj.add(Rin);
rj.add(Rout);
//添加关于菜单Item
about.add(bj);
//将菜单加入菜单栏
mnuMnuBar.add(txl);
mnuMnuBar.add(rj);
mnuMnuBar.add(about);
//添加监听
Tin.addActionListener(this);
//添加背景图片
final ImageIcon icon = new ImageIcon("menu.jpg");
JPanel panel = new JPanel()
{
public void paintComponent(Graphics g)
{
g.drawImage(icon.getImage(), 0, 0, null);
super.paintComponent(g);
}
};
panel.setOpaque( false );
Container container=this.getContentPane();
container.add(panel);
this.setSize(new Dimension(400,300));
this.setVisible(true);
}
public Dimension getPerreSize()
{
return new Dimension(800,800);
}
public void actionPerformed(ActionEvent evt){
if (evt.getSource()instanceof JMenuItem)//判断事件源是否为菜单项
{
String ItemName = evt.getActionCommand();
if (ItemName.equals("T录入"))
//这个if (ItemName.equals("T录入"))地方错误,提示信息如下:Multiple markers at this line- Syntax error on token ")", delete this token
//- Syntax error, insert "}" to complete Statement
//- Syntax error, insert ") Statement" to complete
{
//此处为其他代码!已省略
}
}
}
public static void main(String args[])
{
MenuDesign frmMenu=new MenuDesign("个人信息系统");
frmMenu.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
}
省略部分代码就算不用也不行,我用jdk1.5,错误在这if (ItemName.equals("T录入"))
//这个if (ItemName.equals("T录入"))地方错误,提示信息如下:Multiple markers at this line- Syntax error on token ")", delete this token
//- Syntax error, insert "}" to complete Statement
//- Syntax error, insert ") Statement" to complete 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询