Java屏幕是空白的

Java屏幕是空白的,java,swing,screen,Java,Swing,Screen,正如标题所说,我的java屏幕一直是空白的。我想让它显示类的形状和两个标签。如果你想看另一个关于画线和X的类,请告诉我。这就是我到目前为止所做的: ---编辑--- 既然你给了我建议,我已经取得了一些进展。我已经从Shapes的paint方法中删除了一段相当不错的代码,并将其移动到另一个方法或一个新类中以保持其干净。形状仍然是空白页,但事情看起来更好。另外,请不要粘贴答案代码。作为一名学生,我被指示不要复制代码。诸如“为循环创建3并生成整数”之类的指示被认为是可以接受的。再次感谢你!每个人都很乐

正如标题所说,我的java屏幕一直是空白的。我想让它显示类的形状和两个标签。如果你想看另一个关于画线和X的类,请告诉我。这就是我到目前为止所做的:

---编辑---

既然你给了我建议,我已经取得了一些进展。我已经从Shapes的paint方法中删除了一段相当不错的代码,并将其移动到另一个方法或一个新类中以保持其干净。形状仍然是空白页,但事情看起来更好。另外,请不要粘贴答案代码。作为一名学生,我被指示不要复制代码。诸如“为循环创建3并生成整数”之类的指示被认为是可以接受的。再次感谢你!每个人都很乐于助人

---编辑--- 在与一位老师交谈后,我成功地分离了形状的图形组件,并将它们放入移动中。Shapes知道如何提问并正确工作。我相信我仍然需要做一些调整,以绘制所有的移动,但我相信,现在一切都将更好地工作。我会让你看看现在的样子。再次感谢你帮助我走上正轨

项目1(或主要)

以下是形状:

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;
    }

}  
和行:

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

我做了一些更改,以确保您不会出现黑屏问题

    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();
        }

}
我注释掉了形状线,以便运行测试

首先,我添加了对SwingUtilities的调用,以便创建Swing组件并在事件调度线程上运行。这需要我将Swing代码放在Project1类中,让main方法执行SwingUtilities调用。我让类实现可运行,因为这是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();
        }

}

我运行了你的代码,注释了关于形状的行,在消息对话框之后,我得到了一个窗口,顶部是“FSU”,底部是“Jared Scott”。这是意料之中的。需要查看Shape类以进一步帮助。什么是类形状?你能显示它的代码吗?好的,我已经添加了它。因此,您不必感到困惑,它创建的线条要么成为Xs,要么成为tick-tac-toe的图形。在解释了switch语句中的答案后,它也会画出来。Line类在哪里?我很抱歉。我同时做了三件事。哈哈,就是这样。很抱歉我没有提供足够的细节。我正在尝试附加一个名为Shapes的类,它将绘制一个tick-tac-toe网格,接受用户输入,并在屏幕上绘制Xs和Os。你需要看形状课吗?不,但那是另一个问题。形状(更好的名称;游戏区域)应该扩展JPanel并覆盖paintComponent。实际移动应该保存在移动类中。列表将记录所有的移动。PlayArea将在每次调用paintComponent方法时绘制网格和移动。你可以将游戏区域添加到边框布局中心的主JPanel中。我将尝试将形状和绘制网格的星号结合起来。我将创建两个绘制组件,看看它的作用。
    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();
        }

}