• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Setting up two src files in Netbeans IDE

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I'm trying to get a minesweepers game found here: http://zetcode.com/tutorials/javagamestutorial/minesweeper/
set up in the Netbeans IDE. Theres two .java files, Mines.java, which is the main class, and Board.java, which holds most of the code.

Do I start a new project, mines, in the IDE, then somehow reference the Board.java file? Or does all the src go in one file somehow?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They can both go into the same class since the second class is not public. Make sure the file name matches the public class name. And that the package name matches the one in the code.

You'll need to create a NetBeans project for your file. See the quickstart for how.
 
Larry Evers
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Started a new project named Mines in the Netbeans IDE, so the code now looks like this:

package mines;

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Mines extends JFrame {

private final int WIDTH = 250;
private final int HEIGHT = 290;

private JLabel statusbar;

public Mines() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(WIDTH, HEIGHT);
setLocationRelativeTo(null);
setTitle("Minesweeper");

statusbar = new JLabel("");
add(statusbar, BorderLayout.SOUTH);

add(new Board(statusbar));

setResizable(false);
setVisible(true);
}

public static void main(String[] args) {
new Mines();

}
}

So your saying the Boards.java code is supposed to go right after:

public static void main(String[] args) {
new Mines();
 
Larry Evers
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tryed setting up the new project "from existing sources" in the IDE, using the Mines.java and Board.java files held in a C:\JavaProject\Mines\MinesSource folder.

This loaded both sources into the IDE Project section.

However, the first line of each source is underlined in red giving an "incorrect package" error. Any ideas on why that is?
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Larry Evers wrote:Tryed setting up the new project "from existing sources" in the IDE, using the Mines.java and Board.java files held in a C:\JavaProject\Mines\MinesSource folder.

This loaded both sources into the IDE Project section.

However, the first line of each source is underlined in red giving an "incorrect package" error. Any ideas on why that is?



Just check that both the classes are in the package, they are supposed to be!!

Plus, I would suggest you to write your own code, rather than getting a code from internet, and running it.. This will help you understand better..
And start with notepad and Command Line.. Don't directly get to an IDE.. This I am saying assuming that you are a beginner in Java..
You will understand whole point of packages and other stuffs better..
 
Larry Evers
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. Speaking as a VB programmer, the Java code itself is not too bad from what I can tell so far, but the IDE/compiler are taking some getting used to. I managed to get the Snakes game at that url running ok. The Minesweeper game will have to wait as I have to manufacture its icons. I'm looking at these practical examples as I can rewrite and modify them to get a better feel for the code, as well as trying to get a handle on the IDE/compiler. Unfortunately the IDE tutorials I've seen are very generic. Will note my progress with the games mentioned here shortly. I'm really only at my fourth Java program, so things aren't going too bad.
 
Larry Evers
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wasn't used to using multiple source .java files, but I got the Snake and Breakout games to work by using the following:

Start a new project, java with existing sources.
Project name: Snake
Project folder: C:\JavaProject\Snake
Buildscript name: build.xml
checkmark "set as main project"
Source package folders: C:\JavaProject\Snake\SnakeSources

in that last directory I had:
apple.png
Board.java
dot.png
head.png
Snake.java

The IDE says: Project Snake does not have a main class set, select the main class: Snake

In the IDE Project window, the Snake.java and Board.java both show errors if the first line of each: "package snake;" is left in. If taken out, the source builds and runs ok.

Note I used freeware icons in .png format, edited by Real World Icon Editor found on the web.

So, I now can edit and run Java programs that have multiple src files.

However, I still am having a problem with setting up the package name in the IDE when I start a new project. I can only get the project to run if I use the default package, not the "package Snake;" specified in the source code files.

Any ideas on this last problem welcome.

 
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Larry Evers wrote:In the IDE Project window, the Snake.java and Board.java both show errors if the first line of each: "package snake;" is left in. If taken out, the source builds and runs ok.


The package declaration must match the names of the directories in which the source files are located. Thus your source files must be at:

C:\JavaProject\Snake\SnakeSources\snake\Snake.java
C:\JavaProject\Snake\SnakeSources\snake\Board.java

NetBeans should have placed a red underling under the "package snake;" line in the source file. If you hit ALT-ENTER, it would have provided tow options to fix this error, one of which is "Move class to the correct folder". That will do the work of creating the "snake" folder and moving the .java files there.
 
Larry Evers
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Peter, your correct. I saved the sources and .png files, then deleted the project. Started again with the files in the new directory structure, and the IDE no longer showed a red line under "package snake;" as it has before. The project built and ran ok. Still going through "Java: The Complete Reference", with a couple of other Java books after that.
 
reply
    Bookmark Topic Watch Topic
  • New Topic