Author Topic: Java frames, panels, etc.  (Read 5176 times)

0 Members and 1 Guest are viewing this topic.

Offline UOMaddogTopic starter

  • Maddog
  • Elite
  • *
  • *
  • Posts: 1625
  • Activity:
    0%
  • Reputation Power: 22
  • UOMaddog might someday be someone...UOMaddog might someday be someone...UOMaddog might someday be someone...UOMaddog might someday be someone...
  • Gender: Male
  • Biggest B@D@$$ of the Universe
  • Respect: +165
  • Referrals: 8
    • View Profile
    • Insane UO
Java frames, panels, etc.
« on: September 05, 2009, 05:40:16 PM »
0
Ok, so this semester I'm taking an advanced programming class. Basically data structures and algorithms. We've done all previous programming in C, Verilog, and other assembly languages, but now we're making the jump to Java. Our first assignment is implementing a game called Nim (look it up on Wikipedia). I already wrote sufficient code to run the entire program (albeit it was as close to C as I could possibly get), but we can earn extra points for extra features, such as:
1. error check - already implemented!
2. use classes/objects for each part (instead of programming it like C) - I haven't really messed with this too much yet
3. make graphical representation of the heaps if using the console as output - skipping this due to part 5
4. ability to pay against a computer opponent - I know how, but haven't really done anything with it yet
5. make a GUI in which to play the game - THIS IS WHAT I WANT TO DO!

So....focusing on part 5:
I have read numerous documents/tutorials on creating/using GUI's. The major problems I am having is with LayoutManagers, Buttons, Panes vs Containers, and Actions. I'll attach the code I have so far below.  I get how to create new labels and such, but the Layout is really messed up. I currently am using a setLayout(null) so that I can manually place everything. This isn't a huge deal since the window is set set as not resizable.  Basically I would like a window that looks like the one below. You can kind of see where I'm at by comparing my code (should compile and everything, if not, I'm using Eclipse SDK v3.5.0) to my image. I do not have the initialization in there yet though. Basically I will have a series of showInputDialog messages that will ask how many heaps they want to start with and then ask the max number of stones in a heap. Probably will just have it randomly assign a number of stones in each heap (up to the max).

So...after all that explanation and rambling...I would like some advice/samples on how to make the buttons "do stuff", and advice on how to layout the window (whether I should use a Layout Manager and if so, which one OR just place everything explicitly). Any and all help is appreciated!

Thanks,
UOMaddog









Code: [Select]
package guitest;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.util.*;

public class GUItest extends JFrame {
private JLabel title;
private static JLabel currentplayer;
private static int player;

//CONSTRUCTOR FOR GUItest
GUItest() {
setTitle("NIM");
setLocation(100,100);
setSize(400,200);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(null);

//TITLE
title = new JLabel("The Game of NIM");
title.setLocation(150,5);
title.setSize(95,15);
this.getContentPane().add(title);


//DISPLAY CURRENT PLAYER
player = 1;
currentplayer = new JLabel("Current Player: "+player,JLabel.LEFT);
currentplayer.setLocation(10,25);
currentplayer.setSize(100,15);
this.getContentPane().add(currentplayer);


}

//Main loop
public static void main(String args[]) throws InterruptedException {
//Generate a new window
JOptionPane.showMessageDialog(null,"NIM\nCreated by: Dominic Russo","Welcome!",JOptionPane.PLAIN_MESSAGE);
new GUItest().setVisible(true);
                Thread.sleep(1000);
GUItest.player = 2;
GUItest.currentplayer.setText("Current Player: "+player);
}
}
There are 10 kinds of people in this world: those that understand binary and those that don't!

Windows:  A 64-bit tweak of a 32-bit extension to a 16-bit user interface for an 8-bit operating system based on a 4-bit architecture from a 2-bit company that can't stand 1 bit of competition!

Offline TrailMyx

  • Officially retired from UO
  • Administrator
  • *
  • *
  • Posts: 13301
  • Activity:
    0%
  • Reputation Power: 154
  • TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!
  • Gender: Male
  • Viper!
  • Respect: +1349
  • Referrals: 33
    • View Profile
    • ScriptUO
Re: Java frames, panels, etc.
« Reply #1 on: September 05, 2009, 05:49:24 PM »
0
Well, I've never used Java so I can't help you directly.  However, generally the same mechanics are involved when you assign function to a specific control.  Normally a control has a method that can be assigned that will cause the control to do something.  Things like:

Button1.OnClick()

For events that occur when you click it, you can then assign a handler function that will do whatever you want.  I know this is kinda basic, so I apologize if I'm being too simple.

I'm away from my normal computer, but I have some sample code I can post later that's more specific.
« Last Edit: September 05, 2009, 05:53:54 PM by TrailMyx »
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline UOMaddogTopic starter

  • Maddog
  • Elite
  • *
  • *
  • Posts: 1625
  • Activity:
    0%
  • Reputation Power: 22
  • UOMaddog might someday be someone...UOMaddog might someday be someone...UOMaddog might someday be someone...UOMaddog might someday be someone...
  • Gender: Male
  • Biggest B@D@$$ of the Universe
  • Respect: +165
  • Referrals: 8
    • View Profile
    • Insane UO
Re: Java frames, panels, etc.
« Reply #2 on: September 05, 2009, 06:07:02 PM »
0
I appreciate the help. I know that they have an "event listener" of some type

basically you add implements ActionListener to the class header, then you do addActionListener(this) and add an actionPerformed() somewhere in the class. I know this cuz it's what I read, the problem is I'm not really sure WHAT I'm doing or WHY I'm doing it. That's my problem with programming, I need to hear someone describe it and give a decent example to understand stuff.
There are 10 kinds of people in this world: those that understand binary and those that don't!

Windows:  A 64-bit tweak of a 32-bit extension to a 16-bit user interface for an 8-bit operating system based on a 4-bit architecture from a 2-bit company that can't stand 1 bit of competition!

Offline TrailMyx

  • Officially retired from UO
  • Administrator
  • *
  • *
  • Posts: 13301
  • Activity:
    0%
  • Reputation Power: 154
  • TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!
  • Gender: Male
  • Viper!
  • Respect: +1349
  • Referrals: 33
    • View Profile
    • ScriptUO
Re: Java frames, panels, etc.
« Reply #3 on: September 05, 2009, 07:24:56 PM »
0
Hopefully someone can come up with a Java example for you.  The C# one I'd come up with would probably not be very helpful to you.
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline UOMaddogTopic starter

  • Maddog
  • Elite
  • *
  • *
  • Posts: 1625
  • Activity:
    0%
  • Reputation Power: 22
  • UOMaddog might someday be someone...UOMaddog might someday be someone...UOMaddog might someday be someone...UOMaddog might someday be someone...
  • Gender: Male
  • Biggest B@D@$$ of the Universe
  • Respect: +165
  • Referrals: 8
    • View Profile
    • Insane UO
Re: Java frames, panels, etc.
« Reply #4 on: September 05, 2009, 09:35:15 PM »
0
LOL probably not, but I appreciate the help!
There are 10 kinds of people in this world: those that understand binary and those that don't!

Windows:  A 64-bit tweak of a 32-bit extension to a 16-bit user interface for an 8-bit operating system based on a 4-bit architecture from a 2-bit company that can't stand 1 bit of competition!

Tags: