回 帖 发 新 帖 刷新版面

主题:[讨论]我的程序哪里错了??

我的程序哪里错了??在编译的时候会出现:
ceshi.java:28: 需要 <标识符>
              Jmenuitem2.addActionListener(new ActionListener()
                                          ^
1 错误
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class JWindow extends JFrame
{
      Qipan Q=new Qipan();
      public JWindow(String str)
      {
              super(str);
              add(Q);
      }
        public static void main(String[] args)
        {
              System.out.println("nihao");
              JWindow jwindow=new JWindow("五子棋对战平台");
              jwindow.setSize(400,350);
              jwindow.setLocation(300,180);
              jwindow.setVisible(true);
              jwindow.setDefaultCloseOperation(jwindow.EXIT_ON_CLOSE);
        } 
        class QiPan extends JPanel
    {
              JMenuBar Jmenubar=new JMenuBar();
              JMenu Jmenu=new JMenu("操作");
              JMenuItem Jmenuitem1=new JMenuItem("开始");
              JMenuItem Jmenuitem2=new JMenuItem("退出");
          Jmenuitem2.addActionListener(new ActionListener()
          {
                  public void actionPerformed(ActionEvent e) 
                  {
                          System.exit(0);
                  }
          });
      }
}

回复列表 (共5个回复)

沙发

你最下面的小括号和分号怎么会事啊?

板凳

Jmenuitem2.addActionListener(new ActionListener()
              {
                      public void actionPerformed(ActionEvent e) 
                      {
                              System.exit(0);
                      }
              });

需要放在方法体中.

Qipan Q=new Qipan();

改为:
QiPan Q=new QiPan();

3 楼

参考:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class JWindow2 extends JFrame
{
      QiPan Q=new QiPan();
      public JWindow2(String str)
      {
              super(str);
              
              add(Q);
      }
        public static void main(String[] args)
        {
              System.out.println("nihao");
              JWindow jwindow=new JWindow("五子棋对战平台");
              jwindow.setSize(400,350);
              jwindow.setLocation(300,180);
              jwindow.setVisible(true);
              jwindow.setDefaultCloseOperation(jwindow.EXIT_ON_CLOSE);
        } 
        class QiPan extends JPanel
    {
              JMenuBar Jmenubar=new JMenuBar();
              JMenu Jmenu=new JMenu("操作");
              JMenuItem Jmenuitem1=new JMenuItem("开始");
              JMenuItem Jmenuitem2=new JMenuItem("退出");
              
          public QiPan(){
              Jmenuitem2.addActionListener(new ActionListener()
              {
                      public void actionPerformed(ActionEvent e) 
                      {
                              System.exit(0);
                      }
              });
          }
              
          
      }
}

4 楼

没添加菜单栏到窗口也没做响应事件
不知道你相干什么

5 楼

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class JWindow2 extends JFrame
{
      QiPan Q=new QiPan();
      public JWindow2()
      {
              super("五子棋对战平台");
              setSize(400,350);
           setLocation(300,180);
             setVisible(true);
           setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          add(Q);
      }
        public static void main(String[] args)
        {
              System.out.println("nihao");
             // JWindow jwindow=new JWindow("五子棋对战平台");
        new JWindow2();
        } 
        class QiPan extends JPanel
    {
              JMenuBar Jmenubar=new JMenuBar();
              JMenu Jmenu=new JMenu("操作");
              JMenuItem Jmenuitem1=new JMenuItem("开始");
              JMenuItem Jmenuitem2=new JMenuItem("退出");
            //  
          public QiPan(){setJMenuBar(Jmenubar);
              Jmenubar.add(Jmenu);
             Jmenu.add(Jmenuitem1);
           Jmenu.add(Jmenuitem2);
              Jmenuitem2.addActionListener(new ActionListener()
              {
                      public void actionPerformed(ActionEvent e) 
                      {
                              System.exit(0);
                      }
              });
          }
              
          
      }
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册