So I'm working on a basic character generator program, like something for an RPG game, but I'm having trouble working with JSpinners.
Here's the program:
//libraries
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.text.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.beans.*;
class SpinnerListeners implements ChangeListener {
public void stateChanged(ChangeEvent e) {
//Uuuhhh, I'unno
}
}
class CharGen extends JFrame {
//creates variables needed for attributes
public int pointsremaining = 300;
public int strength = 0;
public int dexterity = 0;
public int constitution = 0;
public int intelligence = 0;
void screenSetup() {
//creates panels and title borders
JPanel borderpanel = new JPanel();
JPanel pntsrbox = new JPanel();
JPanel rightpane = new JPanel();
JPanel strp = new JPanel();
JPanel dexp = new JPanel();
JPanel conp = new JPanel();
JPanel intp = new JPanel();
TitledBorder pntsrtitle = BorderFactory.createTitledBorder("Points Left To Spend"); //creates a titleborder and sets what title says
TitledBorder rightpanetitle = BorderFactory.createTitledBorder("Attributes");
TitledBorder strptitle = BorderFactory.createTitledBorder("Strength");
TitledBorder dexptitle = BorderFactory.createTitledBorder("Dexterity");
TitledBorder conptitle = BorderFactory.createTitledBorder("Constitution");
TitledBorder intptitle = BorderFactory.createTitledBorder("Intelligence");
//creates text field for the points remaining to be displayed
JFormattedTextField pntsrdis = new JFormattedTextField();
pntsrdis.setValue(pointsremaining);
pntsrdis.setEditable(false);
//creates spinner component
SpinnerNumberModel strspinmod = new SpinnerNumberModel(0, 0, 300, 1); //creates number spinner with initial value = 0, minimum = 0, maximum = 300 and step =1
JSpinner StrSpin = new JSpinner(strspinmod);
StrSpin.addChangeListener(new SpinnerListeners());
SpinnerNumberModel dexspinmod = new SpinnerNumberModel(0, 0, 300, 1); //creates number spinner with initial value = 0, minimum = 0, maximum = 300 and step =1
JSpinner DexSpin = new JSpinner(dexspinmod);
DexSpin.addChangeListener(new SpinnerListeners());
SpinnerNumberModel conspinmod = new SpinnerNumberModel(0, 0, 300, 1); //creates number spinner with initial value = 0, minimum = 0, maximum = 300 and step =1
JSpinner ConSpin = new JSpinner(conspinmod);
ConSpin.addChangeListener(new SpinnerListeners());
SpinnerNumberModel intspinmod = new SpinnerNumberModel(0, 0, 300, 1); //creates number spinner with initial value = 0, minimum = 0, maximum = 300 and step =1
JSpinner IntSpin = new JSpinner(intspinmod);
IntSpin.addChangeListener(new SpinnerListeners());
//sets layout managers of panels
borderpanel.setLayout(new BoxLayout(borderpanel, BoxLayout.LINE_AXIS)); // sets the layout manager of the panel as BoxLayout, "LINE_AXIS" means it'll go horizontally
pntsrbox.setLayout(new BoxLayout(pntsrbox, BoxLayout.PAGE_AXIS));
rightpane.setLayout(new BoxLayout(rightpane, BoxLayout.PAGE_AXIS)); // "PAGE_AXIS" means it'll go vertically
strp.setLayout(new BoxLayout(strp, BoxLayout.LINE_AXIS));
dexp.setLayout(new BoxLayout(dexp, BoxLayout.LINE_AXIS));
conp.setLayout(new BoxLayout(conp, BoxLayout.LINE_AXIS));
intp.setLayout(new BoxLayout(intp, BoxLayout.LINE_AXIS));
//sets up the frame and panels and adds on borders
setTitle("Character Generator V1_5_1");
setDefaultCloseOperation(EXIT_ON_CLOSE); // exits java when clicking on close on main frame, might wanna change this later
setBounds(512, 512, 0, 0);
setContentPane(borderpanel); // adds the panel as a container to the frame, the panel can hold stuff
pntsrbox.setBorder(pntsrtitle); // adds a titled border to pntsrbox
rightpane.setBorder(rightpanetitle);
strp.setBorder(strptitle);
dexp.setBorder(dexptitle);
conp.setBorder(conptitle);
intp.setBorder(intptitle);
//adds components and lays them out
borderpanel.add(pntsrbox);
borderpanel.add(Box.createRigidArea(new Dimension(10,0))); // adds a component of empty space for padding
borderpanel.add(rightpane);
pntsrbox.add(pntsrdis);
rightpane.add(strp);
rightpane.add(Box.createRigidArea(new Dimension(0,10)));
rightpane.add(dexp);
rightpane.add(Box.createRigidArea(new Dimension(0,10)));
rightpane.add(conp);
rightpane.add(Box.createRigidArea(new Dimension(0,10)));
rightpane.add(intp);
strp.add(StrSpin);
dexp.add(DexSpin);
conp.add(ConSpin);
intp.add(IntSpin);
//sets border in borderpanel, this should space the main content in from the sides of the window
borderpanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
}
}
class CharacterGeneratorV1_5_2 {
public static void main(String args[]) {
CharGen CGFrame = new CharGen();
CGFrame.screenSetup();
//sets the size of the frame around it's components and then shows it
CGFrame.pack();
CGFrame.setVisible(true);
}
}
What I'm struggling with is the change listener that the JSpinners refer to. I would like them all to only have to refer to one change listener for efficiency of code but I can't figure out how to determine which JSpinner the change came from and hense adjust the corresponding variable accordingly.
The basic idea of the program is as you put up any of the spinners (or attributes) then the "points remaining" pool should go down and likewise if the attributes are put down then points should be put back in the points remaining pool.
Hope that makes sense.
Also I would appreciate any tips on how to make my code more efficient/object oriented as the first version of this program was totally procedural as I was and still am quite new to java and OOP.
Any pointers?
Thanks in advance.
Edited by: ThePermster on Jan 30, 2008 3:46 AM