俺是个JAVA新手来着的,有个问题一直纠结了一整天,但是依旧解决不了
我画了一个棋盘,就是16 * 16条线,然后输出这256条线,但是屏幕除了个菜单外,我想要的棋盘却没出现!
JAVA高手们帮忙下,谢谢了,下面是代码import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;public class Gobang extends JPanel
{
static JMenuBar menuBar = new JMenuBar(); //创建菜单条类
static JMenu menu = new JMenu("选择");  
static JMenu menu_difficulty = new JMenu("难度"); 
static JMenu Menu_About = new JMenu("帮助");  
static JMenuItem menuStart = new JMenuItem("开始游戏");   
static JMenuItem menuReStart = new JMenuItem("重新开始");
static JMenuItem menuExit = new JMenuItem("退出"); 
static JRadioButtonMenuItem easy = new JRadioButtonMenuItem("简单");    
static JRadioButtonMenuItem secondary = new JRadioButtonMenuItem("中等");
static JRadioButtonMenuItem difficulty = new JRadioButtonMenuItem("困难",true);
    static JMenuItem help = new JMenuItem("帮助");
    static JMenuItem about = new JMenuItem("关于五子棋");    
    static PopupMenu pm = new PopupMenu("");   
    
    public static void main(String args[])
    {
     JFrame gobang = new JFrame();
        gobang.setLayout(null); 
        gobang.setLocation(100, 80);
        gobang.setSize(750, 600);
        //菜单栏添加
        menu.add(menuStart);  
        menu.add(menuReStart);
        menu.addSeparator();  //加一条下划线
        menu.add(menuExit);
        menu_difficulty.add(easy); 
        menu_difficulty.add(secondary);
        menu_difficulty.add(difficulty); 
        Menu_About.add(help);
        Menu_About.add(about);
        menuBar.add(menu);
        menuBar.add(menu_difficulty);
        menuBar.add(Menu_About);
        gobang.add(menuBar);
        gobang.setJMenuBar(menuBar);
        Container contentPane = gobang.getContentPane();   
        Panel panel = new Panel();  
        gobang.setBackground(new Color(255, 182, 147));
        contentPane.setBackground(new Color(255, 182, 147)); 
        contentPane.add(panel); 
        gobang.setResizable(false);   
        panel.setCursor(new Cursor(Cursor.HAND_CURSOR)); 
        gobang.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      //添加监听器
        menuExit.addActionListener(new ActionListener()
        {   
            public void actionPerformed(ActionEvent e)
            {   
                  System.exit(0);   
            }   
         });
        easy.addActionListener(new ActionListener()
        {   
           public void actionPerformed(ActionEvent e)
           {    
           }   
        }); 
        
        secondary.addActionListener(new ActionListener()
        {   
           public void actionPerformed(ActionEvent e)
           {    
           }   
        }); 
        difficulty.addActionListener(new ActionListener()
        {   
            public void actionPerformed(ActionEvent e)
            {  
            }   
         }); 
        help.addActionListener(new ActionListener()
        {
         public void actionPerformed(ActionEvent e)
         {
         JOptionPane.showMessageDialog(null, "木有帮助");   //打开不了网页
                ProcessBuilder Tomlive  = new ProcessBuilder("iexplore.exe",
                                            "http://hi.csdn.net/space-5376767.html"); 
                ProcessBuilder Gobang_skill = new ProcessBuilder("iexplore.exe",
                        "http://club.topsage.com/thread-799722-1-1.html");
         }
        });
        about.addActionListener(new ActionListener()
        {
         public void actionPerformed(ActionEvent e)
         {
        
         }
        });
        /*
        Graphics g = getGraphics();  //
        chessboard(g);        */
        new game();
        gobang.setVisible(true);
    }   
}class game extends JPanel
{
int i, j, k, oldx, oldy;
    private URL blackMole = Gobang.class.getResource("black.gif");
    private URL whiteMole = Gobang.class.getResource("white.gif");
    private URL currentImgURL = Gobang.class.getResource("current.gif");  
    private ImageIcon black = new ImageIcon(blackMole);
    private ImageIcon white = new ImageIcon(whiteMole);
    private ImageIcon current=new ImageIcon(currentImgURL); 
    
    public void game()
    {
     addMouseListener(new Xiazi() );
     Graphics g = getGraphics();
     paintComponent(g);
    }
    
public void paintComponent(Graphics g) 
    {//绘制棋盘
       super.paintComponent(g);   
       for( i = 0; i < 16; i++)
       {
        for( j =0; j < 16; j++)
        {
        g.drawLine(50, 50 + 30 * i, 50 * 16, 50 + 30 * i);
        g.drawLine(50 + 30 * j, 50, 50 + 30 * j, 50 * 16);
        }  
       }
       for ( i = 0; i < 16; i++)
       {   
           String number = Integer.toString(i);   
           g.drawString(number, 46 + 30 * i, 45);
           g.drawString(number, 33, 53 + 30 * i);  
       } 
      updatePaint(g);   
   }

class Xiazi extends MouseAdapter
    {//下棋
        public void mouseClicked(MouseEvent e)
        {   
         oldx = e.getX();
         oldy = e.getY();
        }   
     } 

 
public void updatePaint(Graphics g)
{
}
}

解决方案 »

  1.   

    我是这样做的,只给你发了话棋盘的,我人工只能写的太恶心了,不好意思发
    package edu.xawl.daocao;import java.awt.*;
    import java.awt.event.*;import javax.swing.*;
    import java.util.*;public class FiveZiQi extends JFrame
    {
    static Toolkit tk = Toolkit.getDefaultToolkit();
    //static Image image = tk.createImage(FiveZiQi.class.getClassLoader().getResource("image/black_qizi.gif"));
    //static ImageIcon pic = new ImageIcon(FiveZiQi.class.getClassLoader().getResource("image/backpic.jpg")); private ImageIcon black_qizi = new ImageIcon(FiveZiQi.class.getClassLoader().getResource("image/black_qizi.gif"));
    private ImageIcon white_qizi = new ImageIcon(FiveZiQi.class.getClassLoader().getResource("image/white_qizi.gif")); private static final int JFRAME_WIDTH = 800;
    private static final int JFRAME_HEIGHT = 640;
    private static final int JFRAME_X = (tk.getScreenSize().width - JFRAME_WIDTH) / 2;
    private static final int JFRAME_Y = (tk.getScreenSize().height - JFRAME_HEIGHT) / 2;
    private static final int QIZI_SIZE = 30;
    static final int QIZI_WEISHU = 13; private JPanel centerPanel = new JPanel();
    private JPanel southPanel = new JPanel();
    private JPanel northPanel = new JPanel();
    JPanel[][] jPanel = new JPanel[QIZI_WEISHU][QIZI_WEISHU];
    JLabel[][] jLabel = new JLabel[QIZI_WEISHU][QIZI_WEISHU]; JLabel caption = new JLabel("这是一个五子棋");
    JTextField tf1 = new JTextField(20); private Button regret = new Button("悔棋");
    private Button pvp = new Button("人人对战");
    private Button pvr = new Button("人机对战");
    private Button replay = new Button("再来一局"); // 这个用来判断到五颗子没有。第三纬是状态位,0代表没有子,1代表黑子,2代表白子。
    static int[][][] STATE = new int[QIZI_WEISHU][QIZI_WEISHU][1]; // 用来记录出子顺序,里面存放的是JPanel
    static ArrayList<JLabel> RECORD = new ArrayList<JLabel>(); // 总步数
    static int COUNT = 0; // 0表示人机对战,1表示人人对战
    static int TYPE = 0; // 1表示横向,2表示纵向,3表示正斜\,4表示反斜/,0表示查找失败
    static int DIRECTION = 0; // new出来一个人工智能对象
    Robot robot = new Robot(this); FiveZiQi()
    {
    FrameInit();
    DataInit(); this.setTitle("五子棋");
    this.setIconImage(image);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setSize(JFRAME_WIDTH, JFRAME_HEIGHT);
    this.setLocation(JFRAME_X, JFRAME_Y);
    this.setVisible(true);
    } void FrameInit()
    {
    // JLabel
    caption.setSize(new Dimension(300, 40));
    caption.setFont(new Font("Dialog", Font.BOLD, 40)); // Button
    regret.addActionListener(new regret_ActionListener(this));
    pvp.addActionListener(new pvp_ActionListener(this));
    pvr.addActionListener(new pvr_ActionListener(this));
    replay.addActionListener(new replay_ActionListener(this));
    // JTextField
    tf1.setEditable(false);
    tf1.setText("黑手走第一手"); // Jpanel,创建棋盘
    for (int i = 0; i < QIZI_WEISHU; i++)
    {
    for (int j = 0; j < QIZI_WEISHU; j++)
    {
    if (i % 2 == 1 && j % 2 == 1)
    {
    jPanel[i][j] = new JPanel();
    jPanel[i][j].setLayout(null); jLabel[i][j] = new JLabel();
    jLabel[i][j].setSize(QIZI_SIZE, QIZI_SIZE); jPanel[i][j].setName("[" + i + "][" + j + "]号棋格");
    jPanel[i][j].setBounds(j * QIZI_SIZE + (JFRAME_WIDTH - QIZI_SIZE * QIZI_WEISHU) / 2, i * QIZI_SIZE + 50, QIZI_SIZE, QIZI_SIZE);
    jPanel[i][j].setBackground(Color.WHITE);
    jPanel[i][j].addMouseListener(new jPanel_MouseListener(this, jPanel[i][j], jLabel[i][j], i, j, robot));
    centerPanel.add(jPanel[i][j]); }
    if (i % 2 == 0 && j % 2 == 0)
    {
    jPanel[i][j] = new JPanel();
    jPanel[i][j].setLayout(null); jLabel[i][j] = new JLabel();
    jLabel[i][j].setSize(QIZI_SIZE, QIZI_SIZE); jPanel[i][j].setName("[" + i + "][" + j + "]号棋格");
    jPanel[i][j].setBounds(j * QIZI_SIZE + (JFRAME_WIDTH - QIZI_SIZE * QIZI_WEISHU) / 2, i * QIZI_SIZE + 50, QIZI_SIZE, QIZI_SIZE);
    jPanel[i][j].setBackground(Color.WHITE);
    jPanel[i][j].addMouseListener(new jPanel_MouseListener(this, jPanel[i][j], jLabel[i][j], i, j, robot));
    centerPanel.add(jPanel[i][j]);
    }
    if (i % 2 == 1 && j % 2 == 0)
    {
    jPanel[i][j] = new JPanel();
    jPanel[i][j].setLayout(null); jLabel[i][j] = new JLabel();
    jLabel[i][j].setSize(QIZI_SIZE, QIZI_SIZE); jPanel[i][j].setName("[" + i + "][" + j + "]号棋格");
    jPanel[i][j].setBounds(j * QIZI_SIZE + (JFRAME_WIDTH - QIZI_SIZE * QIZI_WEISHU) / 2, i * QIZI_SIZE + 50, QIZI_SIZE, QIZI_SIZE);
    jPanel[i][j].setBackground(Color.BLACK);
    jPanel[i][j].addMouseListener(new jPanel_MouseListener(this, jPanel[i][j], jLabel[i][j], i, j, robot));
    centerPanel.add(jPanel[i][j]);
    }
    if (i % 2 == 0 && j % 2 == 1)
    {
    jPanel[i][j] = new JPanel();
    jPanel[i][j].setLayout(null); jLabel[i][j] = new JLabel();
    jLabel[i][j].setSize(QIZI_SIZE, QIZI_SIZE); jPanel[i][j].setName("[" + i + "][" + j + "]号棋格");
    jPanel[i][j].setBounds(j * QIZI_SIZE + (JFRAME_WIDTH - QIZI_SIZE * QIZI_WEISHU) / 2, i * QIZI_SIZE + 50, QIZI_SIZE, QIZI_SIZE);
    jPanel[i][j].setBackground(Color.BLACK);
    jPanel[i][j].addMouseListener(new jPanel_MouseListener(this, jPanel[i][j], jLabel[i][j], i, j, robot));
    centerPanel.add(jPanel[i][j]);
    }
    }
    } centerPanel.setOpaque(false);
    centerPanel.setLayout(null);
    northPanel.setOpaque(false);
    northPanel.add(caption);
    southPanel.setOpaque(false);
    southPanel.add(tf1);
    southPanel.add(regret);
    southPanel.add(pvr);
    southPanel.add(pvp);
    southPanel.add(replay); this.add(northPanel, "North");
    this.add(southPanel, "South");
    this.add(centerPanel, "Center"); }
      

  2.   

    错了优点错,整个全给你改了,你先看。
    直接运行就可以运行出来。
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class Gobang extends JFrame
    {
            static JMenuBar menuBar = new JMenuBar(); // 创建菜单条类
            static JMenu menu = new JMenu("选择");
            static JMenu menu_difficulty = new JMenu("难度");
            static JMenu Menu_About = new JMenu("帮助");
            static JMenuItem menuStart = new JMenuItem("开始游戏");
            static JMenuItem menuReStart = new JMenuItem("重新开始");
            static JMenuItem menuExit = new JMenuItem("退出");
            static JRadioButtonMenuItem easy = new JRadioButtonMenuItem("简单");
            static JRadioButtonMenuItem secondary = new JRadioButtonMenuItem("中等");
            static JRadioButtonMenuItem difficulty = new JRadioButtonMenuItem("困难",
    true);
            static JMenuItem help = new JMenuItem("帮助");
            static JMenuItem about = new JMenuItem("关于五子棋");
            static PopupMenu pm = new PopupMenu("");        public static void main(String args[])
            {
        Gobang gobang = new Gobang();
        gobang.launchFrame();     // 添加监听器
        menuExit.addActionListener(new ActionListener()
        {
    public void actionPerformed(ActionEvent e)
    {
            System.exit(0);
    }
        });
        easy.addActionListener(new ActionListener()
        {
    public void actionPerformed(ActionEvent e)
    {
    }
        });     secondary.addActionListener(new ActionListener()
        {
    public void actionPerformed(ActionEvent e)
    {
    }
        });
        difficulty.addActionListener(new ActionListener()
        {
    public void actionPerformed(ActionEvent e)
    {
    }
        });
        help.addActionListener(new ActionListener()
        {
    public void actionPerformed(ActionEvent e)
    {
            JOptionPane.showMessageDialog(null, "木有帮助"); // 打开不了网页
            ProcessBuilder Tomlive = new ProcessBuilder(
    "iexplore.exe",
    "http://hi.csdn.net/space-5376767.html");
            ProcessBuilder Gobang_skill = new ProcessBuilder(
    "iexplore.exe",
    "http://club.topsage.com/thread-799722-1-1.html");
    }
        });
        about.addActionListener(new ActionListener()
        {
    public void actionPerformed(ActionEvent e)
    { }
        });
        /*
         * Graphics g = getGraphics(); // chessboard(g);
         */
        // panel.add(new game());     gobang.setVisible(true);        }        public void launchFrame()
            {
        this.setLayout(null);
        this.setLocation(100, 80);
        this.setSize(750, 600);
        // 菜单栏添加
        menu.add(menuStart);
        menu.add(menuReStart);
        menu.addSeparator(); // 加一条下划线
        menu.add(menuExit);
        menu_difficulty.add(easy);
        menu_difficulty.add(secondary);
        menu_difficulty.add(difficulty);
        Menu_About.add(help);
        Menu_About.add(about);
        menuBar.add(menu);
        menuBar.add(menu_difficulty);
        menuBar.add(Menu_About);
        this.add(menuBar);
        this.setJMenuBar(menuBar);
        Container contentPane = this.getContentPane();
        Panel panel = new Panel();
        this.setBackground(new Color(255, 182, 147));
        contentPane.setBackground(new Color(255, 182, 147));
        contentPane.add(panel);
        this.setResizable(false);
        panel.setCursor(new Cursor(Cursor.HAND_CURSOR));
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     new Thread(new PaintThread()).start();
            }        public void paint(Graphics g)
            {
        Color c = g.getColor();
        g.setColor(Color.BLACK);
        for (int i = 0; i < 16; i++)
        {
    for (int j = 0; j < 16; j++)
    {
            g.drawLine(50, 50 + 30 * i, 50 * 16,
    50 + 30 * i);
            g.drawLine(50 + 30 * j, 50, 50 + 30 * j,
    50 * 16);
    }
        }
        for (int i = 0; i < 16; i++)
        {
    String number = Integer.toString(i);
    g.drawString(number, 46 + 30 * i, 45);
    g.drawString(number, 33, 53 + 30 * i);
        }
        g.setColor(c);
            }        private class PaintThread implements Runnable
            {
        public void run()
        {
    while (true)
    {
            repaint(); // repaint会调用update()和paint().
            try
            {
        Thread.sleep(20);
            } catch (InterruptedException e)
            {
        e.printStackTrace();
            }
    }
        }
            }
    }
      

  3.   

    在Swing中绘图
    1. 不要直接在JFrame里绘制,而是在JPanel或者JComponent里绘图,把JPanel加入JFrame
    2. 不要在paint方法中绘图,而是在protected void paintComponent(Graphics g)里绘制
      

  4.   

    谢谢你啦,你的这个提醒,帮助我解决了问题
    dengnanyi给的程序出了一个问题就是,没办法显示出菜单!不过俺还是用了你的了,只是多了个JPanel
      

  5.   


    import java.awt.*;
    import java.awt.event.*;
    import java.net.URL;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JRadioButtonMenuItem;public class Gobang extends JPanel
    {
        static JMenuBar menuBar = new JMenuBar(); //创建菜单条类
        static JMenu menu = new JMenu("选择");  
        static JMenu menu_difficulty = new JMenu("难度"); 
        static JMenu Menu_About = new JMenu("帮助");  
        static JMenuItem menuStart = new JMenuItem("开始游戏");   
        static JMenuItem menuReStart = new JMenuItem("重新开始");
        static JMenuItem menuExit = new JMenuItem("退出"); 
        static JRadioButtonMenuItem easy = new JRadioButtonMenuItem("简单");    
        static JRadioButtonMenuItem secondary = new JRadioButtonMenuItem("中等");
        static JRadioButtonMenuItem difficulty = new JRadioButtonMenuItem("困难",true);
        static JMenuItem help = new JMenuItem("帮助");
        static JMenuItem about = new JMenuItem("关于五子棋");    
        static PopupMenu pm = new PopupMenu("");   
        
        public static void main(String args[])
        {
            JFrame gobang = new JFrame();
            gobang.setLayout(null); 
            gobang.setLocation(100, 80);
            gobang.setSize(800, 700);
            //菜单栏添加
            menu.add(menuStart);  
            menu.add(menuReStart);
            menu.addSeparator();  //加一条下划线
            menu.add(menuExit);
            menu_difficulty.add(easy); 
            menu_difficulty.add(secondary);
            menu_difficulty.add(difficulty); 
            Menu_About.add(help);
            Menu_About.add(about);
            menuBar.add(menu);
            menuBar.add(menu_difficulty);
            menuBar.add(Menu_About);
            gobang.add(menuBar);
            gobang.setJMenuBar(menuBar);
            Container contentPane = gobang.getContentPane();   
            Panel panel = new Panel();  
            gobang.setBackground(new Color(255, 182, 147));
            contentPane.setBackground(new Color(255, 182, 147)); 
            contentPane.add(panel); 
            gobang.setResizable(false);   
            panel.setCursor(new Cursor(Cursor.HAND_CURSOR)); 
            gobang.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          //添加监听器
            menuExit.addActionListener(new ActionListener()
            {   
                public void actionPerformed(ActionEvent e)
                {   
                      System.exit(0);   
                }   
             });
            easy.addActionListener(new ActionListener()
            {   
               public void actionPerformed(ActionEvent e)
               {    
               }   
            }); 
            
            secondary.addActionListener(new ActionListener()
            {   
               public void actionPerformed(ActionEvent e)
               {    
               }   
            }); 
            difficulty.addActionListener(new ActionListener()
            {   
                public void actionPerformed(ActionEvent e)
                {  
                }   
             }); 
            help.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    JOptionPane.showMessageDialog(null, "木有帮助");   //打开不了网页
                    ProcessBuilder Tomlive  = new ProcessBuilder("iexplore.exe",
                                                "http://hi.csdn.net/space-5376767.html"); 
                    ProcessBuilder Gobang_skill = new ProcessBuilder("iexplore.exe",
                            "http://club.topsage.com/thread-799722-1-1.html");
                }
            });
            about.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
             JOptionPane.showMessageDialog(null, "             五子棋\n" +
         "\n作者:     纪旭龙   严翔  刘建峰" +
         "\n棋子设计:严翔" +
         "\n算法实现:纪旭龙" +
         "\n结构设计:纪旭龙" );
                }
            });
            JPanel chessboard = new chessboard();
            chessboard.setBounds(50, 50, 550, 550);       //设置菜单位置
            chessboard.setSize(550, 550);
            chessboard.setLocation(50, 50);
            gobang.add(chessboard);
            gobang.setVisible(true);
        }   
    }class chessboard extends JPanel
    {      /*
     private URL blackImgURL = Gobang.class.getResource("black.gif");   
     private ImageIcon black = new ImageIcon(blackImgURL);   
     private URL whiteImgURL = Gobang.class.getResource("white.gif");   
     private ImageIcon white = new ImageIcon(whiteImgURL);   
     private URL currentImgURL = Gobang.class.getResource("current.gif");   
     private ImageIcon current=new ImageIcon(currentImgURL);  */  
     private int i, j, k;

    public void paintComponent(Graphics g) 
        {//绘制棋盘
           super.paintComponent(g);
           g.setColor(Color.BLACK);
           for( i = 0; i < 16; i++)
           {
               for( j =0; j < 16; j++)
               {
                g.drawLine(50, 50 + j * 30, 500, 50 + j * 30); 
                g.drawLine(50 + j * 30, 50, 50 + j * 30, 500);
               }  
           }
           for ( i = 0; i < 16; i++)
           {   
               String number = Integer.toString(i);   
               g.drawString(number, 46 + 30 * i, 45);
               g.drawString(number, 33, 53 + 30 * i);  
           } 
      //    updatePaint(g);   
       }
        
    }
    这个是我的代码