Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1] 2 3

Author Topic: Basic Java GUIs  (Read 4517 times)

AlStar

  • Bay Watcher
    • View Profile
Basic Java GUIs
« on: November 20, 2009, 11:29:58 am »

Hey,

I'm currently working on a for-fun project in Java (don't laugh, it's what I know). The game is getting pretty far along, but I'm currently just playing it through System.out.print()/println() on the terminal. It gets the job done, but is neither pretty nor something that I could potentially release for other people to try out.

What I'm looking for is a basic GUI - wouldn't have to do much more than work the same way that the terminal does (displaying text on the screen). Future customizability would be a plus.

Is there anything out there that would work well? That you'd recommend/have used previously with good results? Tell me I'm a pansy and should just learn how to program one up myself?

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: Basic Java GUIs
« Reply #1 on: November 20, 2009, 11:41:21 am »

Simple gui's are actually very easy in java. SWING is probably the best option. Its simple, easy to use for the basics, but has the depth and power needed to eventually build a full interface.


http://en.wikipedia.org/wiki/Swing_%28Java%29
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

Ironhand

  • Bay Watcher
  • the llama is laughing
    • View Profile
Re: Basic Java GUIs
« Reply #2 on: November 20, 2009, 12:39:46 pm »

NetBeans is a Java IDE that builds GUI code for you using Swing.
You just drag and drop Swing components on the screen, and it writes up the code for you.

It's kinda cool. And you can go back and look at the script it wrote for you to learn how it's done.

Honestly, I used to know Swing GUI, and then I discovered NetBeans, and now I'm lazy.
Logged

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: Basic Java GUIs
« Reply #3 on: November 20, 2009, 02:54:13 pm »

I generally prefer eclipse for java dev, though netbeans is pretty decent, there are a few wsywig or drag and drop form designers available for it as plugins, though I have not used any of them. All my professional java projects have been servlets or daemons, I have not used java GUIs since college.

http://stackoverflow.com/questions/29426/best-gui-designer-for-eclipse
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

eerr

  • Bay Watcher
    • View Profile
Re: Basic Java GUIs
« Reply #4 on: November 20, 2009, 06:06:07 pm »

I'll write something up soon.


Totally thought I'd be done by now.
Layout managers are a pain >.<
« Last Edit: November 20, 2009, 07:35:25 pm by eerr »
Logged

eerr

  • Bay Watcher
    • View Profile
Re: Basic Java GUIs
« Reply #5 on: November 20, 2009, 08:59:03 pm »

Ok, the easiest format I can make is one Text output area, and one text input area.
Logged

eerr

  • Bay Watcher
    • View Profile
Re: Basic Java GUIs
« Reply #6 on: November 20, 2009, 10:31:57 pm »

Swing components make everything easy!
Note that the term gui means "Graphical user interface"
And I am not using graphics.

Using the following swing components-
JFrame, JTextArea, JTextField,  and 2 JPanels
I have constructed a window, with a giant white box that has a large white bar below it.

If you type into the little bar and hit enter, the text you type appears in the large white box.

Right now I am considering If I need to buffer text people type in or not.
Logged

eerr

  • Bay Watcher
    • View Profile
Re: Basic Java GUIs
« Reply #7 on: November 20, 2009, 11:30:56 pm »

Code: [Select]
package console;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
public class console_frame extends JFrame {

private static final long serialVersionUID = 1L;

JTextField inputfield        = new JTextField();
JTextArea  outputfield       = new JTextArea();

private JPanel     holds_inputfield  = new JPanel(new BorderLayout());
private JPanel     holds_outputfield = new JPanel(new BorderLayout());

private String[] buffer=  new String[5];
private int buffer_length=5;

public console_frame(){
this.setSize(500,500);
this.getContentPane().setLayout(new BorderLayout());
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);


this.getContentPane().add(holds_outputfield, BorderLayout.CENTER);
this.getContentPane().add(holds_inputfield,BorderLayout.SOUTH);
holds_inputfield.setVisible(true);
holds_outputfield.setVisible(true);

holds_inputfield.setPreferredSize(new Dimension(500,20));
holds_outputfield.setPreferredSize(new Dimension(500,400));


holds_outputfield.add(outputfield, BorderLayout.CENTER);
holds_inputfield.add(inputfield,BorderLayout.CENTER);

inputfield.setText("kkkk");
outputfield.setVisible(true);
outputfield.setText("weeppppppppppppppppppp");

outputfield.setEditable(false);

outputfield.setSize(holds_outputfield.getSize());
inputfield.setSize(holds_inputfield.getSize());


final console_frame self = this;
inputfield.addKeyListener(new KeyListener(){
public void keyReleased(KeyEvent e){

}
public void keyTyped(KeyEvent e){

}
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
     if (key == KeyEvent.VK_ENTER) {

    self.EnterTyped(self.inputfield.getText());
      }
}
});

}

public static void main(String[] args) {
console_frame x= new console_frame();
}
private void EnterTyped(String s){
outputfield.setText(s);
return;
}

}
Logged

eerr

  • Bay Watcher
    • View Profile
Re: Basic Java GUIs
« Reply #8 on: November 21, 2009, 08:24:39 pm »

Considering I didn't read very carefully,
I use Eclipse.

Anytime you make a mistake, including mid-type, eclipse will inform you
that you made a mistake. The helpful part is that for any primitive type or object, eclipse will list every method, and the parameters it takes.

if you type:

String.

eclipse will pop up with a list of all Functions in the String api.
like String.tochararray(), or String.getchar(Int location)
or String.parseInt()

If you type println("bam") and forgot to import the console, you can
Right click and click "import java.io.utill" or whatever that particular import is.

Logged

AlStar

  • Bay Watcher
    • View Profile
Re: Basic Java GUIs
« Reply #9 on: November 22, 2009, 01:11:16 am »

Thanks for the replies everyone - I was using NetBeans already, so I used the auto form creation tools from that (which I had no idea existed until now).

So, I've managed to transfer over my main process to a jpanel, so as to take advantage of a GUI. After some headaches, everything seems to be going well - I can get different information showing up in different panels.

However, I'm running into a problem. When I was just using the terminal, I was using a BufferedReader (which I called 'br') to take in input, and whenever the program needed input, I'd run "input = br.readLine();" This was handy, because it would pause the program while it waited for input. I've been trying to get my jTextField to do the same thing, but can't seem to figure it out, so my program keeps looping many times a second.

Any suggestions on how to accomplish this?

eerr

  • Bay Watcher
    • View Profile
Re: Basic Java GUIs
« Reply #10 on: November 22, 2009, 02:35:41 am »

If it merely loops too fast and hogs everything up you could invoke the wait(number time) function inherited from object. Wait() invokes the minimum amount of time.

Logged

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: Basic Java GUIs
« Reply #11 on: November 22, 2009, 10:01:36 am »

If it merely loops too fast and hogs everything up you could invoke the wait(number time) function inherited from object. Wait() invokes the minimum amount of time.



Ah! That is probably the biggest headache to figure out when you first start working with a gui. Welcome to the wonderful world of events and listeners. http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html

Polling for data is not efficient (though sometimes it is needed) and using events and listeners will be helpful.
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

eerr

  • Bay Watcher
    • View Profile
Re: Basic Java GUIs
« Reply #12 on: November 23, 2009, 12:12:13 am »

If you need an example, my code passes a keylistener to a JTextField.



Code: [Select]
final console_frame console = this;

inputfield.addKeyListener(new KeyListener(){
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public void keyPressed(KeyEvent e){
  if (e.getKeyCode() == KeyEvent.VK_ENTER)
{ console.EnterTyped(inputfield.getText());}
}
});

The user presses enter.
inputfield(an ordinary JTextField) listens, and does
{ console.EnterTyped(inputfield.getText());}

Instead of looping infinitely  around one spot, The JTextField triggers my program with each press of enter.


« Last Edit: December 03, 2009, 02:43:19 pm by eerr »
Logged

SolarShado

  • Bay Watcher
  • Psi-Blade => Your Back
    • View Profile
Re: Basic Java GUIs
« Reply #13 on: November 26, 2009, 10:14:52 pm »

This thread seems to have died, so I'll take it over with a question of my own.

How different is Swing from the java.awt package?

I know all the class names start with "J", and there are a few classes and such that make some things easier.

Is it worth the effort to learn Swing when I'm rather familiar with the AWT?
Logged
Avid (rabid?) Linux user. Preferred flavor: Arch

eerr

  • Bay Watcher
    • View Profile
Re: Basic Java GUIs
« Reply #14 on: November 27, 2009, 12:10:07 am »

This thread seems to have died, so I'll take it over with a question of my own.

How different is Swing from the java.awt package?

I know all the class names start with "J", and there are a few classes and such that make some things easier.

Is it worth the effort to learn Swing when I'm rather familiar with the AWT?
Swing? AWT? I've forgotten any significant diffrence between them.

But looking up swing for its components:
JFrame
JButton
JMenu/JMenuItem
are all very useful

I'm not exactly sure the diffrence between JFrame and JApplet

Note that you can use JList for multiple object selection, without tooo much hassle. Assuming you give it to the right component. The example I had to write in class was an address book.


http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/
This page mentions that Swing was written to supplant the weak and inflexible tools of AWT, but I'm ignorant enough so I barely know the diffrence.

The program I wrote is just enough to input text and output text as I thought he wanted a replacement for using the console with system.in and system.out .
but if console== cmd , then I haven't got a chance.
http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/
I highly reccomend you read the whole page.
Logged
Pages: [1] 2 3