0

正如标题所说,我的 java 屏幕一直空白。我希望它显示类形状和两个标签。如果您想查看其他处理绘图线和 xs 的课程,请告诉我。这是我到目前为止所拥有的:

- -编辑 - -

既然你们给了我建议,我已经取得了一些进展。我已经从 Shapes 的paint 方法中删除了一些不错的代码,并将它移到另一个方法或一个新类中以保持它的清洁。Shape's 仍在按页制作空白,但情况正在好转。另外,请不要粘贴代码以获得答案。作为一名学生,我被指示不要复制代码。诸如“创建 3 个 for 循环并生成一个整数”之类的指示被认为是可以接受的。再次感谢你!每个人都非常有帮助!

---编辑--- 在与老师交谈后,我成功地分离了 Shapes 的图形组件并将它们放入 Moves 中。形状知道提出问题并正常工作。我相信我仍然需要对 Moves 进行一些调整才能绘制所有内容,但我相信现在一切都会更好。我会向你展示它现在的样子。再次感谢您帮助我走上正轨!

项目1(或主要)

import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class Project1 implements Runnable{
public static void main(String[] args) {

    SwingUtilities.invokeLater( new Project1());
}

@Override
        public void run(){
    //Asterick a = new Asterick(); 
  //fsu title bar and footer
    JLabel header = new JLabel( "FSU" );
    JLabel footer = new JLabel( "By Jared Scott" );

    //CREATE EVERYTHING
    JFrame frame = new JFrame(); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );

    JPanel panel = new JPanel( new BorderLayout());

    frame.setSize(400, 700);

    Shapes as = new Shapes();
    //Asterick as = new Asterick();
    String message =
                  String.format( "Enter a number from" 
          + " 1-9 to play tick tack toe.\n" + "The order of squares is\n" +
            "1     4     7\n2     5     8\n3     6     9" +
                  "\nThe game ends when one player has three Xs or Os" +
                  " in a row.");

    JOptionPane.showMessageDialog(null, message);

   //this works
   panel.add( header, BorderLayout.NORTH );
   panel.add( footer, BorderLayout.SOUTH ); 

   panel.add(as, BorderLayout.CENTER);
   frame.add( panel );

   frame.setVisible( true );    
}
}

这是形状:

public class Shapes extends JPanel{

//put graphics inside of moves
//shapes will send an int for moves to interpret
private int answer;
int[] locationFill = new int[ 9 ];
Moves move;
private int turn = 0;
char x = 'x';
char o = 'o';

public Shapes(){
    setBackground( Color.WHITE );
    ask();    
}

public void ask(){

    String tryAgain =
                  String.format( "Already in use. Please choose again.");

  while( getTurn() <= 9){

        goesNext( getTurn() );

       String input = JOptionPane.showInputDialog( "Choose your square" );
       answer = Integer.parseInt( input );

    //put these in pop up questions

    switch( answer ){    

        case 1: 
           if( !isEmpty( answer ) ){
               JOptionPane.showMessageDialog(null, tryAgain );
            } else if( whatToDraw( answer ) == true ){
                 move = new Moves( answer, x );  
                 locationFill[0] = 1;
                 turn++;
                 won();
            } else {
                move = new Moves( answer, o );    
                locationFill[0] = 2;
                turn++;
                won();
            }
            break;

        case 2:
            if( !isEmpty( answer ) ){
                JOptionPane.showMessageDialog(null, tryAgain );
            }else if( whatToDraw( answer ) == true  ){
                move = new Moves( answer, x );  
                locationFill[1] = 1;
                 turn++;
                 won();
            }else{
                move = new Moves( answer, o );
                locationFill[1] = 2;
                turn++;
                won();
            }
            break;

        case 3:
            if( !isEmpty( answer ) ){
                JOptionPane.showMessageDialog(null, tryAgain );
            }else if( whatToDraw( answer ) == true  ){
                move = new Moves( answer, x );       
                locationFill[2] = 1;
               turn++;
               won();
            }else{
                move = new Moves( answer, o ); 
                locationFill[2] = 2;
                turn++;
                won();
            }
            break;

        case 4:
            if( !isEmpty( answer ) ){
                JOptionPane.showMessageDialog(null, tryAgain );
            }else if( whatToDraw( answer ) == true  ){
                move = new Moves( answer, x );            
                locationFill[3] = 1;
                 turn++;
                 won();
            }else{
                move = new Moves( answer, o ); 
                locationFill[3] = 2;
                turn++;
                won();
            }
            break;

        case 5:
            if( !isEmpty( answer ) ){
                JOptionPane.showMessageDialog(null, tryAgain );
            }else if( whatToDraw( answer ) == true  ){
                move = new Moves( answer, x );        
                locationFill[4] = 1;
                 turn++;
                 won();
            }else{
                move = new Moves( answer, o );
                locationFill[4] = 2;
                turn++;
                won();
            }
            break;

        case 6:
            if( !isEmpty( answer ) ){
                JOptionPane.showMessageDialog(null, tryAgain );
            }else if( whatToDraw( answer ) == true  ){
                move = new Moves( answer, x ); 
                locationFill[5] = 1;
                turn++;
                won();
            }else{
                move = new Moves( answer, o ); 
                locationFill[5] = 2;
                turn++;
                won();
            }
            break;

        case 7:
            if( !isEmpty( answer ) ){
                JOptionPane.showMessageDialog(null, tryAgain );
            }else if( whatToDraw( answer ) == true ){
                move = new Moves( answer, x );
                locationFill[6] = 1;
                turn++;
                won();
            }else{
                move = new Moves( answer, o );     
                locationFill[6] = 2;
                turn++;
                won();
            }
            break;

        case 8:
            if( !isEmpty( answer ) ){
                JOptionPane.showMessageDialog(null, tryAgain );
            }else if( whatToDraw( answer ) == true  ){
                move = new Moves( answer, x ); 
                locationFill[7] = 1;
                turn++;
                won();
            }else{
                move = new Moves( answer, o );   
                locationFill[7] = 2;
                turn++;
                won();
            }
            break;

        case 9:
            if( !isEmpty( answer ) ){
                JOptionPane.showMessageDialog(null, tryAgain );
            }else if( whatToDraw( answer ) == true  ){
                move = new Moves( answer, x );        
                locationFill[8] = 1;
               turn++;
                won();
            }else{
                move = new Moves( answer, o ); 
                locationFill[8] = 2;
                turn++;
                won();
            }
            break;

        default:
            System.out.println("Somethings wrong");
            break;
    }//end switch

    tie(); 

   }//end while
    }

public void goesNext( int turn ){
 String player1 =
                  String.format( "Player1's Turn");
 String player2 =
                  String.format( "Player2's Turn");

 if( turn == 0 || turn % 2 == 0 ){
        JOptionPane.showMessageDialog(null, player1);
 }else{
        JOptionPane.showMessageDialog(null, player2);
 }
}

public boolean isEmpty( int i ){

    boolean answer2 = true;

if( locationFill[ i - 1 ] == 0 ){
    answer2 = true;
}else if( locationFill[ i - 1 ] == 1 || locationFill[ i - 1 ] == 2){
    answer2 = false;
}
    return answer2;
}//end isEmpty

        public void won(){

    String message =
                  String.format( "Three in a row!\nPlayer2 win!");
    String message2 =
                  String.format( "Three in a row!\nPlayer1 win!");

    if( ( locationFill[0] == 1 && locationFill[1] == 1 && 
            locationFill[2] == 1 ) || ( locationFill[3] == 1 &&
            locationFill[4] == 1 &&
            locationFill[5] == 1) || ( locationFill[6] == 1 &&
            locationFill[7] == 1 &&
            locationFill[8] == 1) || ( locationFill[0] == 1 &&
            locationFill[4] == 1 &&
            locationFill[8] == 1) || ( locationFill[1] == 1 &&
            locationFill[4] == 1 &&
            locationFill[7] == 1) || ( locationFill[0] == 1 &&
            locationFill[3] == 1 &&
            locationFill[6] == 1) || ( locationFill[2] == 1 &&
            locationFill[5] == 1 &&
            locationFill[8] == 1) || ( locationFill[6] == 1 &&
            locationFill[4] == 1 &&
            locationFill[2] == 1) ){
    JOptionPane.showMessageDialog(null, message);
    System.exit(0);
    }

    if( ( locationFill[0] == 2 && locationFill[1] == 2 && 
            locationFill[2] == 2 ) || ( locationFill[3] == 2 &&
            locationFill[4] == 2 &&
            locationFill[5] == 2) || ( locationFill[6] == 2 &&
            locationFill[7] == 2 &&
            locationFill[8] == 2) || ( locationFill[0] == 2 &&
            locationFill[4] == 2 &&
            locationFill[8] == 2) || ( locationFill[1] == 2 &&
            locationFill[4] == 2 &&
            locationFill[7] == 2) || ( locationFill[0] == 2 &&
            locationFill[3] == 2 &&
            locationFill[6] == 2) || ( locationFill[2] == 2 &&
            locationFill[5] == 2 &&
            locationFill[8] == 2) || ( locationFill[6] == 2 &&
            locationFill[4] == 2 &&
            locationFill[2] == 2) ){
    JOptionPane.showMessageDialog(null, message2);
    System.exit(0);
}

}//end won

public int getTurn(){
    return turn;
}

  public void tie(){
if( locationFill[0] != 0 && locationFill[1] != 0 &&
            locationFill[2] != 0 && locationFill[3] != 0
            && locationFill[4] != 0 && locationFill[5] != 0
            && locationFill[6] != 0 && locationFill[7] != 0
                    && locationFill[8] != 0 ){
           String tie =
                  String.format( "It's a tie!");
           String gameOver =
                   String.format( "Game Over" );
           JOptionPane.showMessageDialog( null, tie );
           JOptionPane.showMessageDialog( null, gameOver );
           System.exit( 0 );
}
}//end tie

 public boolean whatToDraw( int choice){
        boolean a;
    if( locationFill[ choice - 1 ] == 0 && turn % 2 == 0 ){
        a = true;
    }else{
        a = false;
    }
    return a;
    }

}  

这是动作:

public class Moves extends JPanel{

Line[] x = new Line[ 18 ];
int choice = 0;
char letter;
public Line[] lines = new Line[ 6 ];

public Moves(){
}

public Moves( int i, char a ){

    letter = a;
    choice = i;

    //for graph
    lines[ 0 ] = new Line(30, 330, 350, 330, Color.BLACK );
    lines[ 1 ] = new Line(30, 225, 350, 225, Color.BLACK );
    lines[ 2 ] = new Line( 250, 130, 250, 440, Color.BLACK );
    lines[ 3 ] = new Line( 125, 130, 125, 440, Color.BLACK );

    //create header and footer
    lines[ 4 ] = new Line( 0, 70, 400, 70, Color.BLACK );
    lines[ 5 ] = new Line( 0, 600, 400, 600, Color.BLACK ); 

    x[0] = new Line( 118, 220, 30, 130, Color.BLACK );//1
    x[1] = new Line( 30, 220, 118, 130, Color.BLACK );
    x[2] = new Line( 118, 320, 30, 230, Color.BLACK );//2                                                         
    x[3] = new Line( 30, 320, 118, 230, Color.BLACK );   
    x[4] = new Line( 122, 440, 34, 335, Color.BLACK );//3                                                         
    x[5] = new Line( 34, 440, 122, 335, Color.BLACK );

    x[6] = new Line( 230, 220, 145, 130, Color.BLACK );//4
    x[7] = new Line( 145, 220, 230, 130, Color.BLACK );
    x[8] = new Line( 230, 320, 145, 230, Color.BLACK );//5                                                          
    x[9] = new Line( 145, 320, 230, 230, Color.BLACK );
    x[10] = new Line( 230, 440, 145, 335, Color.BLACK );//6                                                      
    x[11] = new Line( 145, 440, 230, 335, Color.BLACK );

    x[12] = new Line( 345, 220, 260, 130, Color.BLACK );//7
    x[13] = new Line( 260, 220, 345, 130, Color.BLACK );
    x[14] = new Line( 345, 320, 260, 230, Color.BLACK );//8                                                        
    x[15] = new Line( 260, 320, 345, 230, Color.BLACK );
    x[16] = new Line( 345, 440, 260, 335, Color.BLACK );//9                                                      
    x[17] = new Line( 260, 440, 345, 335, Color.BLACK );
}

@Override
public void paintComponent( Graphics g ){

super.paintComponent( g );

    for( Line line : lines ){
        line.draw( g );
    }
}

//this will draw the Xs
public void drawX1( Graphics g ){
    x[0].draw(g);
    x[1].draw(g);  
}

public void drawX2( Graphics g ){
    x[0].draw(g);
    x[1].draw(g);        
}

public void drawX3( Graphics g ){
    x[0].draw(g);
    x[1].draw(g);       
}

public void drawX4( Graphics g ){
    x[0].draw(g);
    x[1].draw(g);

}

public void drawX5( Graphics g ){
    x[0].draw(g);
    x[1].draw(g);

}

public void drawX6( Graphics g ){
    x[0].draw(g);
    x[1].draw(g);

}

public void drawX7( Graphics g ){
    x[0].draw(g);
    x[1].draw(g);

}

public void drawX8( Graphics g ){
    x[0].draw(g);
    x[1].draw(g);

}

public void drawX9( Graphics g ){
    x[0].draw(g);
    x[1].draw(g);

}
//this will draw the Os
public void drawO1( Graphics g ){
    g.drawOval(40, 135, 75, 75);

}

public void drawO2( Graphics g ){
    g.drawOval(40, 230, 75, 95);

}

public void drawO3( Graphics g ){
    g.drawOval(40, 350, 75, 90);

}

public void drawO4( Graphics g ){
    g.drawOval( 135, 120, 100, 90);

}

public void drawO5( Graphics g ){
    g.drawOval(140, 230, 95, 100);

}

public void drawO6( Graphics g ){
    g.drawOval(150, 350, 80, 90);

}

public void drawO7( Graphics g ){
    g.drawOval( 265, 130, 80, 80);

}

public void drawO8( Graphics g ){
    g.drawOval( 265, 225, 80, 100);

}

public void drawO9( Graphics g ){
    g.drawOval(260, 350, 85, 90);

}

}

和线:

import java.awt.Color;
import java.awt.Graphics;

//this will be for the 4 lines needed for tick tac toe
public class Line {

private int x1,
        y1,
        x2,
        y2;
private Color myColor;

//this is for grid
public Line( int x1, int y1, int x2, int y2, Color color ){

    //set coordinants
    this.x1 = x1;
    this.y1 = y1;
    this.x2 = x2;
    this.y2 = y2;
    myColor = color;
}//end constructor

public void draw( Graphics g ){

    g.setColor( myColor );
    g.drawLine( x1, y1, x2, y2 );
}//end draw
}//end class
4

1 回答 1

1

我做了一些更改以确保您不会遇到黑屏问题。

我注释掉了 Shape 线条,这样我就可以运行测试了。

首先,我添加了对 SwingUtilities 的调用,以便创建 Swing 组件并在 Event Dispatch Thread 上运行。这要求我将 Swing 代码放在 Project1 类中,让 main 方法执行 SwingUtilities 调用。我让类实现 Runnable,因为这是 SwingUtilities 方法调用所需要的。

其次,我添加了一个主 JPanel。最好将 Swing 组件添加到 JPanel,并将 JPanel 添加到 JFrame。添加其他组件时,您将拥有更大的灵活性。

由于 JPanel 的默认布局管理器是 FLowLayout,因此我必须将布局管理器添加到 JPanel 构造函数中。通常,最好让代码的人类读者(包括您自己)指定布局管理器,即使它是默认设置。对于复杂的 GUI,您可能会尝试两个或三个不同的布局管理器来获得您想要的结果。

最后,我注释掉了最后一次 JFrame 重绘。调用 setVisible 方法就足够了。

试试这个代码,看看它是否能解决你的屏幕问题。

    import java.awt.BorderLayout;

    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;

    public class Project1 implements Runnable {

        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Project1());
        }

        @Override
        public void run() {
            // CREATE EVERYTHING
            JFrame app = new JFrame();
            app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JPanel panel = new JPanel(new BorderLayout());

            // fsu title bar and footer
            JLabel header = new JLabel("FSU");
            JLabel footer = new JLabel("By Jared Scott");
            // Shapes shape = new Shapes();
            String message = String.format("Enter a number from"
                    + " 1-9 to play tick tack toe.\n" + "The order of squares is\n"
                    + "1     4     7\n2     5     8\n3     6     9"
                    + "\nThe game ends when one player has three Xs or Os"
                    + " in a row.\nPlayer1 starts first.");
            JOptionPane.showMessageDialog(null, message);

            // app.add( shape );
            panel.add(header, BorderLayout.NORTH);
            panel.add(footer, BorderLayout.SOUTH);

            app.add(panel);
            app.setSize(400, 700);
            app.setVisible(true);

            // app.validate();
    //      app.repaint();
        }

}
于 2013-02-25T16:38:10.667 回答