Help with Java syntax?

java

#1

So basically I keep getting errors within a few lines of code, yes I'm missing an if clause on line 31 I've tried both with and without it and it still doesn't work, can someone give me a hand on figuring out what I've managed to mess up on?

MAIN CLASS:

package simpleboardgame;

public class RealTile {

 public static void main(String[] args) {
   Tile newTile = new Tile();
 }

}

package simpleboardgame;

import java.awt.*;
import javax.swing.*;
import java.util.Random;

public class Tile extends JFrame
}
  int rowMax = 10;
  int columnMax = 10;
  int row;
  int col;
  int n = 0;

 public Tile ()
 }
  JFrame frame = new JFrame();
  frame.setLayout(new GridLayout(rowMax, columnMax));
  frame.setSize(600,600);
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  JLabel nextLabel = new JLabel();
{
{
  int[] activeCell = new int[100];

  for (int row = 0; row < rowMax; row++)
  {
   for (int col = 0; col < columnMax; col++);
   {

    {
     nextLabel.setBorder(BorderFactory.createLineBorder(//Color.BLUE ));
     nextLabel.setBackground(http://Color.black );
     nextLabel.setOpaque(true);

    }

  else 

    {
    nextLabel = new JLabel();
    }

   frame.getContentPane().add(nextLabel);

   }
  }

 }
}

#2

Do you mind explaining what Coding language you're on, as well as the lesson, and exercise?


#3

@lolman it seems to be Java, and not part of a Codecademy course. I've moved it to the Corner Bar now :)


#4

I figured it was Java, but not everybody would know. I haven't completed the Java course yet so I wasn't aware if it was part of the lesson plan or not. Thank you for the clarification.


#5

@nohope
This is your code corrected but I am not sure what the aim was for some parts of your logic . Based on how you are defining the classes I am guessing they are two different files if not you will get a warning at compile time from the compiler.

The application code:

package simpleboardgame;
public class RealTile{
  public static void main(String[] args) {
    Tile newTile = new Tile();
  }//end main
}// end RealTile

The view:

package simpleboardgame;
import java.awt.*;
import javax.swing.*;
//unused import
//import java.util.Random;

public class Tile extends JFrame { // you initially had }

  //since these are attributes its a good idea to make them
  //private
  private int row;
  private int col;

  //if these will not change its best to declare them final
  private final int rowMax = 10;
  private final int columnMax = 10;
  private final int n = 0;
 

  public Tile (){//you have to open the constructor
    //You can set a title for your frame here
    JFrame frame = new JFrame("The title of the frame");
    frame.setLayout(new GridLayout(rowMax, columnMax));
    frame.setSize(600,600);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel nextLabel = new JLabel("Some Title Label");
    int[] activeCell = new int[100];

    //Not clear on what the goal is here
    for (int row = 0; row < rowMax; row++){
      for (int col = 0; col < columnMax; col++){//you had a semicolon here after )
        nextLabel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
        nextLabel.setBackground(Color.black);
        nextLabel.setOpaque(true);
      }
    }
    frame.getContentPane().add(nextLabel);

    //you need to pack all the content into the frame
    //by calling frame.pack
   //without this you will not see the element in your frame
    frame.pack();

  }//end constructor
}//end class

if you wanted to run all the code within the same package but in one .java file then you can do this

NB: Make sure the file name is RealTile.java

package simpleboardgame;
import java.awt.*;
import javax.swing.*;
//unused import
//import java.util.Random;

public class RealTile {
  public static void main(String[] args) {
    Tile newTile = new Tile();
  }//end main
}// end RealTile



class Tile extends JFrame { // you initially had }

  //since these are attributes its a good idea to make them
  //private
  private int row;
  private int col;

  //if these will not change its best to declare them final
  private final int rowMax = 10;
  private final int columnMax = 10;
  private final int n = 0;

 

  Tile (){//you have to open the constructor
    JFrame frame = new JFrame();
    frame.setLayout(new GridLayout(rowMax, columnMax));
    frame.setSize(600,600);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel nextLabel = new JLabel("Some Title Label");
    int[] activeCell = new int[100];

    //Not clear on what the goal is here
    for (int row = 0; row < rowMax; row++){
      for (int col = 0; col < columnMax; col++){//you had a semicolon here after )
        nextLabel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
        nextLabel.setBackground(Color.black);
        nextLabel.setOpaque(true);
      }
    }
    frame.getContentPane().add(nextLabel);

    //you need to pack all the content into the frame
    //by calling frame.pack
    //without this you will not see the elements in the frame
    frame.pack();
  }//end constructor
}//end class

That being done running this current code yields

sugestion

if you are looking create GUIs with Java then perhaps you wanna look look into

JavaFX

it comes as a part of the jdk 8 which contains the jre 8.You can create very powerful and complex interfaces with very less code(especially with the use of lambdas)


#6

GUI Development i see! :smile: . Eclipse is a good Java IDE To run with! It can handle complicated programs! And if you have a question about these type of question ask them at http://stackoverflow.com/questions