Skip to main content
GameDev.net gamedev.net
🔒 Locked

Help me re-learn Java?

Started by The_Game Sep 8, 2010 at 5:05 PM 13 replies 1.9k views
Original Post
The_Game
The_Game
I'm starting to do some real programming again and I figured I'd start off using Java, but I'm having a problem.

The first thing I think I should mention is that I'm using JCreator is an IDE due to the fact that it's free (and I'm broke). I don't know if that will be relevant to the question I'm asking but I thought I should put that out there in the first place just in case someone asks.

I'm trying to make a very simple GUI program but I'm having trouble remembering the syntax. I also have 2 beginner's books on Java but neither of them get into GUIs. I think the first thing I need to do is import some libraries that allow me to make windows, but I don't know which library it is or what its called or the syntax for it.

Alternatively, someone could just give me the name of a book that tells me this stuff. I volunteer at a library so I could get it there, assuming they have it. I had some college books that could of told me but I lost them a while back and haven't been able to find them.
stupid_programmer
stupid_programmer
I think Eclipse is probably the best Java IDE out there and its free.

Swing is the main GUI API for Java. A quick Google search of 'java swing' brings up a bunch of results.
The_Game
The_Game
I've heard of Eclipse a little. Does their IDE have the JDK that I think I need? And doesn't JCreator or Eclipse already have an API (I don't even know what that acronym is for anymore) that lets it make GUIs?

I already saw some link for Swing on this board that led me to a site full of stuff that didn't quite seem to do what I wanted. Mostly jargon I couldn't decipher and a bunch of stuff saying how great it was. Apparently they think there is no such thing as excessive self-promotion.

A little off topic, all of these damn acronyms are giving me a headache. So far I have:

JDK - Java Development Kit
IDE - Integrated Development Enviroment (like JCreator and Eclipse I believe).
GUI - Graphical User Interface
API - ????
AverageMidget
AverageMidget
API = Application Programming Interface.

It's, basically, code that is written to do lower-level, or repetitious, work, making it easier for the programmer to achieve his/her goal. A fictitious example would be: instead of writing out every pixel to your screen, you call DrawMyWindow(), and it does the work for you. DrawMyWindow() (again, that's a fake example) would be part of an API.

I've never programmed in Java, but I've seen swing and eclipse mentioned as being "the" standard for GUI and IDE, respectively.
stupid_programmer
stupid_programmer
None of the Java IDEs come with any kind of graphics API built in. Swing is just a set of packages that have a bunch of (mostly) easy to use Windows style GUI controls. Even Visual Studio doesn't have any kind of builtin GUI stuff. It just uses the Win32/.NET GUI controls which is part of Windows itself.

Anyway, if you download the Java EE package from the Oracle site you should have all the things you need to start making GUI apps. To be honest, if GUI type stuff is what you want to do then C#/WinForms would be the easiest way to go. The Express Edition of C# is a free download and has every thing you need.
The_Game
The_Game
Alright. I figured out a part of the problem. I needed to put import javax.swing.* at the top of the program (or sheet or whatever)

I put in a new JFrame, gave it a size (frame1.setSize(800, 600)) and it's not showing up.

I also found the GUI parts of my book at the very ends of them, so not much help there.

I know I'm close to getting what I want but I can't remember the part I know I'm missing. Plus, I still have to figure out how I can put a game engine into a Java program. I don't have any books for exactly that, but I'm hoping I can take the "principles" from my C++ game programming book and apply them to Java.
gretty
gretty
For java swing its a little different to, say, win32, because you need to remember:

You create the JFrame(window), you then create a JPanel which is really just a panel that you place all your JComponents(controls, ie, buttons, listboxes etc) onto.

So your simplist GUI should:
- create a window
- create a panel


So it would be

public class MyWindow extends JFrame{  /// My Variables  private JPanel mainPanel;  public MyWindow()  {      this.setDefaultCloseOperation( EXIT_ON_CLOSE );      this.setTitle( "My Window" );      this.setSize( 500,500 );      mainPanel = (JPanel) this.getContentPane();  // create the JPanel      mainPanel.setLayout( new GridLayout( 1,2,10,10 ) ); // make sure you set the layout type      // Note the window isn't visible yet because we haven't made it so      // Create all your JComponents here & add them to the mainPanel      JButton blah = new JButton( "Blah" );      mainPanel.add( blah );      this.validate();      this.pack();      this.setVisible( true );  }}
The_Game
The_Game
Scratch everything I said before. The damn JCreator isn't letting me save because it says my "license has expired". I just downloaded it yesterday! It was my understanding that it was free to use and that I already went through all that stupid license crap when I clicked "I Agree".

I'm just going to start over with eclipse tommorow. Why can't anything on computers work like it should?
swiftcoder
swiftcoder
NetBeans is also a worthy choice - I happen to prefer it to Eclipse, but in most respects they are pretty equivalent.
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
The_Game
The_Game
Well, I tried downloading Eclipse. It took about 20 minutes or so, but when I went to run it I got an error message. I just tried to run it again to find the error message but I already deleted eclipse since it didn't work.

The one I down loaded was called "Eclipse IDE for Java EE Developers". I'm thinking maybe I want a different one. I'm downloading "Eclipse IDE for Java Developers" now to see if maybe that isn't what I really want. I don't get what the difference between the "Java EE Developers" and the "Java Developers" is though.

Hopefully it will work this time. Then maybe it will also let me save my projects since JCreator didn't let me.
ncite
ncite
Quote:
Original post by The_Game
Well, I tried downloading Eclipse. It took about 20 minutes or so, but when I went to run it I got an error message. I just tried to run it again to find the error message but I already deleted eclipse since it didn't work.

The one I down loaded was called "Eclipse IDE for Java EE Developers". I'm thinking maybe I want a different one. I'm downloading "Eclipse IDE for Java Developers" now to see if maybe that isn't what I really want. I don't get what the difference between the "Java EE Developers" and the "Java Developers" is though.

Hopefully it will work this time. Then maybe it will also let me save my projects since JCreator didn't let me.


Eclipse EE is for Enterprise Developers (Mainly for business stuff like JSP), maybe you just want SE?
The_Game
The_Game
Alright, I got a working Eclipse IDE, but now there is another problem (isn't that how it always works with computers and programming?).

It doesn't seem to like my:

import javax.swing.*
import java.awt.*;
import java.awt.event.*;

At the top. I have an idea what I'm supposed to do. I think I'm supposed to add those java and javax libraries to my project. Trouble is, I don't know how.

Is there anyone out there familiar with Eclipse that can tell me how to do that?
rhnz
rhnz
There is a missing character in line 1. To be honest if you dont know anything (and your questions tell me you dont) then you should maybe start with a tutorial or book on general programming and dont ask everything in a forum.

Im amazed how non trolling and friendly this community is tho.
The_Game
The_Game
Quote:
Original post by rhnz
To be honest if you dont know anything (and your questions tell me you dont) then you should maybe start with a tutorial or book on general programming and dont ask everything in a forum.


First, I happen to know quite a bit. When I put in this code in JCreator:

import java.awt.*;
import java.awt.event*;
import javax.swing.*;

//Class Constructor

public class Prototype {

//Constructor below. Constructor must have same name as class.

Prototype()
{
//Creates a new "frame" AKA Window. Also sets the title bar of window.
JFrame frame1 = new JFrame ("Frame Demo");

//Sets the size of the frame/window
frame1.setSize (800, 600);

//Creates a text-based Label
JLabel pLabel = new JLabel(" Swing defines the modern Java GUI.");

//Adds the label to the content pane.
frame1.add(pLabel);

//Destorys the window upon telling it to close.
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

/*Makes the frame visible. If not present, nothing will show up */
frame1.setVisible(true);
}

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

});

}
}


It worked the way it was supposed to. I could of kept going but JCreator wouldn't let me save my project for no apparent reason so I had to switch to Eclipse.

Secondly, a forum titled For Beginners is for asking questions. If it isn't, then I don't know what else it's supposed to be for.
gretty
gretty
Stick with eclipse, it will be alot easier

this is because it will automatically import libraries for you.

The way it works is...

just type your code normally

JFrame myframe = new JFrame();

you will get a red underline under the word JFrame because you haven't imported a library. So hold your mouse over the word JFrame, an option will appear that says something like 'automatically import javax swing library', click it & eclipse will automatically write "import javax.swing.JFrame;" at the top of ur project.

So if u use eclipse it will do alot for you, so its good when starting off, or coding quickly.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.