目的是做一个三子棋游戏。
主类是gui.TTT
listener2.java是用来建立服务器,里面引用了一个mythread.java,mythread会循环运行,主类中有一个static int thread_temp=1,当其值为2时,mythread会进行:netgame ng = new netgame();ng.gamestart();这个操作就是创建游戏窗口
在listener2.java中有这么两句话:
socket = serverSocket.accept();
 Gui_TTT.thread_temp=2;
连接成功后应该就可以建立窗口了,但是出来的窗口总是一片空白,下面是正常窗口的空白窗口的图片:

解决方案 »

  1.   

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package gui_ttt;
    import java.io.*;
    import java.net.*;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    /**
     *
     * @author Administrator
     */
    public class Gui_TTT extends JPanel implements ActionListener{
        static ImageIcon bg = new ImageIcon("1.jpg");
        Image im=Toolkit.getDefaultToolkit().getImage("1.jpg"); 
        static int thread_temp=1;
        public void paintComponent(Graphics g) {   g.drawImage(im, 0, 0, null);   }    /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            mythread th1 = new mythread();
            th1.start();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setBounds(0, 0, bg.getIconWidth(), bg.getIconHeight());
            Gui_TTT jp = new Gui_TTT();
            f.add(jp);
            jp.setLayout(null);
            ImageIcon icon1 = new ImageIcon("offline2.png");
            ImageIcon icon2 = new ImageIcon("online2.png");
            ImageIcon icon1x = new ImageIcon("offline3.png");
            ImageIcon icon2x = new ImageIcon("online3.png");
            JButton offline = new JButton(icon1);
            JButton online = new JButton(icon2);
            offline.setPressedIcon(icon1x);
            online.setPressedIcon(icon2x);
            offline.setBorderPainted(false);
            offline.setContentAreaFilled(false);
            online.setBorderPainted(false);
            online.setContentAreaFilled(false);
            jp.add(offline);
            offline.setBounds(50, 200, 211, 82);
            jp.add(online);
            online.setBounds(539, 200, 211, 82);
            offline.addActionListener(new listener1());
            online.addActionListener(new pk());        f.setTitle("井字棋");
            f.setResizable(false);
            f.setVisible(true);
        }    @Override
        public void actionPerformed(ActionEvent e) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
        
    }
      

  2.   

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package gui_ttt;
    //服务器后手
    import static gui_ttt.pk.getmyip;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.IOException;
    import javax.swing.JPanel;
    import java.net.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import java.io.*;
    /**
     *
     * @author Administrator
     */
    public class listener2  implements ActionListener {
        public void actionPerformed(ActionEvent event){
            try {
                connect();
                
                
            } catch (Exception ex) {
                Logger.getLogger(listener2.class.getName()).log(Level.SEVERE, null, ex);
            }
                }
    public void connect()throws Exception{
        
           Socket socket = null;
           String portstr = pk.myport.getText();
           InetAddress ip = InetAddress.getByName("localhost");
           int port =Integer.parseInt(portstr);   
           ServerSocket serverSocket  = new ServerSocket(port,1,InetAddress.getLocalHost());
           String strSocket, strLocal; 
          
        
       socket = serverSocket.accept();
       Gui_TTT.thread_temp=2;
       while(true){  
          
          
          BufferedReader brInFromUser = new BufferedReader(new InputStreamReader(System.in)); 
          BufferedReader brInFromClient = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
          DataOutputStream  dosOutToClient = new DataOutputStream(socket.getOutputStream());
          do{
              
              strSocket = brInFromClient.readLine(); 
              System.out.println("Client: " + strSocket);//打印客户端发来的信息
             strLocal = brInFromUser.readLine(); 
              dosOutToClient.writeBytes(strLocal + '\n'); //传出服务器发出的信息
            } while(!strSocket.equals("bye"));
            socket.close();
           break;
         }   
        
        }
    }
      

  3.   

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package gui_ttt;import static gui_ttt.Gui_TTT.bg;
    import java.io.*;
    import java.net.*;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    /**
     *
     * @author Administrator
     */
    public class netgame extends JPanel{
        static int x=3;//奇数表示1号,偶数表示2号
        static int i=0,j=0;
        static ImageIcon bg = new ImageIcon("2.jpg");
        static Image im=Toolkit.getDefaultToolkit().getImage("2.jpg"); 
        public void paintComponent(Graphics g) {   g.drawImage(im, 0, 0, null);   }
        static void change(int i,int j){
             if(x%2!=0){
             judge.t[i][j]=1;
             }
             else {
             judge.t[i][j]=2;
             }
             
         }
        static public void gamestart() {                               
            JFrame f = new JFrame();                                                  
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            f.setBounds(0, 0, bg.getIconWidth(), bg.getIconHeight()+30); 
            netgame jp = new netgame(); 
            f.add(jp); 
            jp.setLayout(null); 
            ImageIcon icon3 = new ImageIcon("x.jpg"); 
            ImageIcon icon4 = new ImageIcon("o.jpg"); 
            JButton []xxx = new JButton[99]; 
            JButton []ooo = new JButton[99]; 
            for(int k=0;k<99;k=k+1){
            xxx[k]=new JButton(icon3);
            ooo[k]=new JButton(icon4);
            xxx[k].setBorderPainted(false);
            xxx[k].setContentAreaFilled(false);
            ooo[k].setBorderPainted(false);
            ooo[k].setContentAreaFilled(false);
            }
            judge jd = new judge();
            jp.addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent event){
                    
                    
                    int x1 = event.getX();
                    int y1 = event.getY();
                    if(x1>=0&&x1<=193&&y1>=0&&y1<=193){
                    if(x%2!=0)
                    {jp.add(xxx[i]);
                    xxx[i].setBounds(50, 50, 100, 100);}
                    else{
                    jp.add(ooo[i]);
                    ooo[i].setBounds(50, 50, 100, 100);
                    }
                    change(0,0);
                    i=i+1;x=x+1;
                    }
                    if(x1>=202&&x1<=397&&y1>=0&&y1<=193){
                    if(x%2!=0)
                    {jp.add(xxx[i]);
                    xxx[i].setBounds(252, 50, 100, 100);}
                    else{
                    jp.add(ooo[i]);
                    ooo[i].setBounds(252, 50, 100, 100);
                    }
                    change(0,1);
                    i=i+1;x=x+1;
                    }
                    if(x1>=406&&x1<=600&&y1>=0&&y1<=193){
                    if(x%2!=0)
                    {jp.add(xxx[i]);
                    xxx[i].setBounds(456, 50, 100, 100);}
                    else{
                    jp.add(ooo[i]);
                    ooo[i].setBounds(456, 50, 100, 100);
                    }
                    change(0,2);
                    i=i+1;x=x+1;
                    }
                    
                    
                    if(x1>=0&&x1<=193&&y1>=203&&y1<=397){
                    if(x%2!=0)
                    {jp.add(xxx[i]);
                    xxx[i].setBounds(50, 253, 100, 100);}
                    else{
                    jp.add(ooo[i]);
                    ooo[i].setBounds(50, 253, 100, 100);
                    }
                    change(1,0);
                    i=i+1;x=x+1;
                    }
                     if(x1>=202&&x1<=397&&y1>=203&&y1<=397){
                    if(x%2!=0)
                    {jp.add(xxx[i]);
                    xxx[i].setBounds(252, 253, 100, 100);}
                    else{
                    jp.add(ooo[i]);
                    ooo[i].setBounds(252, 253, 100, 100);
                    }
                    change(1,1);
                    i=i+1;x=x+1;
                    }
                    if(x1>=406&&x1<=600&&y1>=203&&y1<=397){
                    if(x%2!=0)
                    {jp.add(xxx[i]);
                    xxx[i].setBounds(456, 253, 100, 100);}
                    else{
                    jp.add(ooo[i]);
                    ooo[i].setBounds(456, 253, 100, 100);
                    }
                    change(1,2);
                    i=i+1;x=x+1;
                    }
                    
                    if(x1>=0&&x1<=193&&y1>=406&&y1<=600){
                    if(x%2!=0)
                    {jp.add(xxx[i]);
                    xxx[i].setBounds(50, 456, 100, 100);}
                    else{
                    jp.add(ooo[i]);
                    ooo[i].setBounds(50, 456, 100, 100);
                    }
                    change(2,0);
                    i=i+1;x=x+1;
                    }
                    if(x1>=202&&x1<=397&&y1>=406&&y1<=600){
                    if(x%2!=0)
                    {jp.add(xxx[i]);
                    xxx[i].setBounds(252, 456, 100, 100);}
                    else{
                    jp.add(ooo[i]);
                    ooo[i].setBounds(252, 456, 100, 100);
                    }
                    change(2,1);
                    i=i+1;x=x+1;
                    }
                    if(x1>=406&&x1<=600&&y1>=406&&y1<=600){
                    if(x%2!=0)
                    {jp.add(xxx[i]);
                    xxx[i].setBounds(456, 456, 100, 100);}
                    else{
                    jp.add(ooo[i]);
                    ooo[i].setBounds(456, 456, 100, 100);
                    }
                    change(2,2);
                    i=i+1;x=x+1;
                    }
                    if(x==12&&jd.judge()==false){
                    JFrame tempjf = new JFrame();
                    tempjf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    JPanel tempjp = new JPanel();
                    tempjf.add(tempjp);
                    tempjp.setLayout(null);
                    ImageIcon iconwin = new ImageIcon("pj.png");
                    JButton win = new JButton(iconwin);
                    win.setBorderPainted(false);
                    win.setContentAreaFilled(false);
                    tempjp.add(win);
                    win.setBounds(0, 0, 100, 100);
                    tempjf.setSize(100, 150);
                    tempjf.setResizable(false);
                    tempjf.setVisible(true);
                
                }
                    if(jd.judge()==true){
                    if(x%2==0){
                    JFrame tempjf = new JFrame();
                    tempjf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    JPanel tempjp = new JPanel();
                    tempjf.add(tempjp);
                    tempjp.setLayout(null);
                    ImageIcon iconwin = new ImageIcon("x2.png");
                    JButton win = new JButton(iconwin);
                    win.setBorderPainted(false);
                    win.setContentAreaFilled(false);
                    tempjp.add(win);
                    win.setBounds(0, 0, 100, 100);
                    tempjf.setSize(100, 150);
                    tempjf.setResizable(false);
                    tempjf.setVisible(true);
                    }
                    else {
                    JFrame tempjf = new JFrame();
                    tempjf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    JPanel tempjp = new JPanel();
                    tempjf.add(tempjp);
                    tempjp.setLayout(null);
                    ImageIcon iconwin = new ImageIcon("o2.png");
                    JButton win = new JButton(iconwin);
                    win.setBorderPainted(false);
                    win.setContentAreaFilled(false);
                    tempjp.add(win);
                    win.setBounds(0, 0, 100, 100);
                    tempjf.setSize(100, 150);
                    tempjf.setResizable(false);
                    tempjf.setVisible(true);
                    }
                    }
                    //////////////////////////////////////////////////////////////////
                }
                
            }
            );
            //f.setResizable(false);
            f.setVisible(true);
        }
        public void run(){
            gamestart();
        }
          public static void main(String[] args) {
              netgame ng = new netgame();
              ng.run();
              
          }
        }
      

  4.   

    主类增加了两个函数:
    static public void gamestart() {                               
            JFrame f2 = new JFrame();                                                  
            f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            f2.setBounds(0, 0, bg.getIconWidth(), bg.getIconHeight()+30); 
            netgame jp = new netgame(); 
            f2.add(jp); 
            jp.setLayout(null); 
            ImageIcon icon3 = new ImageIcon("x.jpg"); 
            ImageIcon icon4 = new ImageIcon("o.jpg"); 
            JButton []xxx = new JButton[99]; 
            JButton []ooo = new JButton[99]; 
            for(int k=0;k<99;k=k+1){
            xxx[k]=new JButton(icon3);
            ooo[k]=new JButton(icon4);
            xxx[k].setBorderPainted(false);
            xxx[k].setContentAreaFilled(false);
            ooo[k].setBorderPainted(false);
            ooo[k].setContentAreaFilled(false);
            }
            judge jd = new judge();
            jp.addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent event){
                    
                    
                    int x1 = event.getX();
                    int y1 = event.getY();
                    if(x1>=0&&x1<=193&&y1>=0&&y1<=193){
                    if(x%2!=0)
                    {jp.add(xxx[i]);
                    xxx[i].setBounds(50, 50, 100, 100);}
                    else{
                    jp.add(ooo[i]);
                    ooo[i].setBounds(50, 50, 100, 100);
                    }
                    change(0,0);
                    i=i+1;x=x+1;
                    }
                    if(x1>=202&&x1<=397&&y1>=0&&y1<=193){
                    if(x%2!=0)
                    {jp.add(xxx[i]);
                    xxx[i].setBounds(252, 50, 100, 100);}
                    else{
                    jp.add(ooo[i]);
                    ooo[i].setBounds(252, 50, 100, 100);
                    }
                    change(0,1);
                    i=i+1;x=x+1;
                    }
                    if(x1>=406&&x1<=600&&y1>=0&&y1<=193){
                    if(x%2!=0)
                    {jp.add(xxx[i]);
                    xxx[i].setBounds(456, 50, 100, 100);}
                    else{
                    jp.add(ooo[i]);
                    ooo[i].setBounds(456, 50, 100, 100);
                    }
                    change(0,2);
                    i=i+1;x=x+1;
                    }
                    
                    
                    if(x1>=0&&x1<=193&&y1>=203&&y1<=397){
                    if(x%2!=0)
                    {jp.add(xxx[i]);
                    xxx[i].setBounds(50, 253, 100, 100);}
                    else{
                    jp.add(ooo[i]);
                    ooo[i].setBounds(50, 253, 100, 100);
                    }
                    change(1,0);
                    i=i+1;x=x+1;
                    }
                     if(x1>=202&&x1<=397&&y1>=203&&y1<=397){
                    if(x%2!=0)
                    {jp.add(xxx[i]);
                    xxx[i].setBounds(252, 253, 100, 100);}
                    else{
                    jp.add(ooo[i]);
                    ooo[i].setBounds(252, 253, 100, 100);
                    }
                    change(1,1);
                    i=i+1;x=x+1;
                    }
                    if(x1>=406&&x1<=600&&y1>=203&&y1<=397){
                    if(x%2!=0)
                    {jp.add(xxx[i]);
                    xxx[i].setBounds(456, 253, 100, 100);}
                    else{
                    jp.add(ooo[i]);
                    ooo[i].setBounds(456, 253, 100, 100);
                    }
                    change(1,2);
                    i=i+1;x=x+1;
                    }
                    
                    if(x1>=0&&x1<=193&&y1>=406&&y1<=600){
                    if(x%2!=0)
                    {jp.add(xxx[i]);
                    xxx[i].setBounds(50, 456, 100, 100);}
                    else{
                    jp.add(ooo[i]);
                    ooo[i].setBounds(50, 456, 100, 100);
                    }
                    change(2,0);
                    i=i+1;x=x+1;
                    }
                    if(x1>=202&&x1<=397&&y1>=406&&y1<=600){
                    if(x%2!=0)
                    {jp.add(xxx[i]);
                    xxx[i].setBounds(252, 456, 100, 100);}
                    else{
                    jp.add(ooo[i]);
                    ooo[i].setBounds(252, 456, 100, 100);
                    }
                    change(2,1);
                    i=i+1;x=x+1;
                    }
                    if(x1>=406&&x1<=600&&y1>=406&&y1<=600){
                    if(x%2!=0)
                    {jp.add(xxx[i]);
                    xxx[i].setBounds(456, 456, 100, 100);}
                    else{
                    jp.add(ooo[i]);
                    ooo[i].setBounds(456, 456, 100, 100);
                    }
                    change(2,2);
                    i=i+1;x=x+1;
                    }
                    if(x==12&&jd.judge()==false){
                    JFrame tempjf = new JFrame();
                    tempjf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    JPanel tempjp = new JPanel();
                    tempjf.add(tempjp);
                    tempjp.setLayout(null);
                    ImageIcon iconwin = new ImageIcon("pj.png");
                    JButton win = new JButton(iconwin);
                    win.setBorderPainted(false);
                    win.setContentAreaFilled(false);
                    tempjp.add(win);
                    win.setBounds(0, 0, 100, 100);
                    tempjf.setSize(100, 150);
                    tempjf.setResizable(false);
                    tempjf.setVisible(true);
                
                }
                    if(jd.judge()==true){
                    if(x%2==0){
                    JFrame tempjf = new JFrame();
                    tempjf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    JPanel tempjp = new JPanel();
                    tempjf.add(tempjp);
                    tempjp.setLayout(null);
                    ImageIcon iconwin = new ImageIcon("x2.png");
                    JButton win = new JButton(iconwin);
                    win.setBorderPainted(false);
                    win.setContentAreaFilled(false);
                    tempjp.add(win);
                    win.setBounds(0, 0, 100, 100);
                    tempjf.setSize(100, 150);
                    tempjf.setResizable(false);
                    tempjf.setVisible(true);
                    }
                    else {
                    JFrame tempjf = new JFrame();
                    tempjf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    JPanel tempjp = new JPanel();
                    tempjf.add(tempjp);
                    tempjp.setLayout(null);
                    ImageIcon iconwin = new ImageIcon("o2.png");
                    JButton win = new JButton(iconwin);
                    win.setBorderPainted(false);
                    win.setContentAreaFilled(false);
                    tempjp.add(win);
                    win.setBounds(0, 0, 100, 100);
                    tempjf.setSize(100, 150);
                    tempjf.setResizable(false);
                    tempjf.setVisible(true);
                    }
                    }
                    //////////////////////////////////////////////////////////////////
                }
                
            }
            );
            //f2.setResizable(false);
            f2.setVisible(false);
        }
         static void change(int i,int j){
             if(x%2!=0){
             judge.t[i][j]=1;
             }
             else {
             judge.t[i][j]=2;
             }
             
         }
    mythread中改成:if(Gui_TTT.thread_temp==2)
            {
                System.out.println("OK!!!");
                Gui_TTT.f2.setVisible(true);
                break;}
    但是仍然白屏..
      

  5.   

    主类中加了一句static JFrame f2 = new JFrame();  
    这样算是在主类中创建窗口吗?然而还是白屏..
      

  6.   

    调用paint刷新界面
      

  7.   

    能说具体一点吗,我在mythread中加了一句Gui_TTT.f2.repaint(); 然而仍然白屏..
      

  8.   

    能说具体一点吗,我在mythread中加了一句Gui_TTT.f2.repaint(); 然而仍然白屏..http://zhidao.baidu.com/link?url=m7b7p6CPRKgKPrnPDB8EO3q6MNEHmf_q5ODZcyqDZnP73t-C7DiqH2n4XzvHhHG6q3trjiZQRCTN3-9VLWRVV_
      

  9.   

    能说具体一点吗,我在mythread中加了一句Gui_TTT.f2.repaint(); 然而仍然白屏..http://zhidao.baidu.com/link?url=m7b7p6CPRKgKPrnPDB8EO3q6MNEHmf_q5ODZcyqDZnP73t-C7DiqH2n4XzvHhHG6q3trjiZQRCTN3-9VLWRVV_
    mythread里面又改为了:Gui_TTT.gamestart();
                Gui_TTT.f2.repaint();
                Gui_TTT.f2.validate();
    但还是白屏。。
      

  10.   

    能说具体一点吗,我在mythread中加了一句Gui_TTT.f2.repaint(); 然而仍然白屏..http://zhidao.baidu.com/link?url=m7b7p6CPRKgKPrnPDB8EO3q6MNEHmf_q5ODZcyqDZnP73t-C7DiqH2n4XzvHhHG6q3trjiZQRCTN3-9VLWRVV_
    mythread里面又改为了:Gui_TTT.gamestart();
                Gui_TTT.f2.repaint();
                Gui_TTT.f2.validate();
    但还是白屏。。看下图片是否已经载入
      

  11.   

    能说具体一点吗,我在mythread中加了一句Gui_TTT.f2.repaint(); 然而仍然白屏..http://zhidao.baidu.com/link?url=m7b7p6CPRKgKPrnPDB8EO3q6MNEHmf_q5ODZcyqDZnP73t-C7DiqH2n4XzvHhHG6q3trjiZQRCTN3-9VLWRVV_
    mythread里面又改为了:Gui_TTT.gamestart();
                Gui_TTT.f2.repaint();
                Gui_TTT.f2.validate();
    但还是白屏。。看下图片是否已经载入
    没有,如果载入了背景会是背景图片
      

  12.   

    能说具体一点吗,我在mythread中加了一句Gui_TTT.f2.repaint(); 然而仍然白屏..http://zhidao.baidu.com/link?url=m7b7p6CPRKgKPrnPDB8EO3q6MNEHmf_q5ODZcyqDZnP73t-C7DiqH2n4XzvHhHG6q3trjiZQRCTN3-9VLWRVV_
    mythread里面又改为了:Gui_TTT.gamestart();
                Gui_TTT.f2.repaint();
                Gui_TTT.f2.validate();
    但还是白屏。。看下图片是否已经载入
    没有,如果载入了背景会是背景图片我也无法定位错误,不过一般情况就是窗口要放在能够接收消息或事件的线程,二是要刷新