Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: Need help on my code

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    31
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Need help on my code

    Hi everyone. I’m a beginner in Java. I’m trying to write a program that will simulate melee combat between different races and different species. However, I’m having trouble. When I press the ‘go’ button, I need it to set default values to combat, defense HP. But when I tried to code it in, it wouldn’t give me a value, just my placeholder - %d. Can somebody explain to me or lead me to the right direction in terms of getting the value put in when I press go. Or if someone could alter my code to show me and explain what it does, that would be greatly appreciated. Either way, thanks. Also sorry if this isn't well written in the forums.
    --------------------------------------------------------------------------------------------------------------------------------------------------



    <import javax.swing.*;    
     
    public class SimpleWindow extends JFrame {
     
    	JLabel one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thirteen,fourteen,fifteen,sixteen,seventeen,eighteen,nineteen,twenty,twentyone;
    	JButton firstButton,secondButton;
    	JTextField firstTextField,second,third,fourth,fifth,sixth,seventh,eighth,ninth,tenth,eleventh,tweleth,thirteenth,fourteenth;
    	JTextArea firstTextArea;
    	JCheckBox firstCheckBox;
    	JComboBox firstComboBox,secondComboBox,thirdComboBox,fourthComboBox;
    	int a,b,c,d,e,f,g,h,i,j,k,l,m,n,A;
     
     
     
    	public SimpleWindow() {             
            createInterface(); 
    	}
     
    	void createInterface() {
    	setLayout(null);  	
    	one = new JLabel(); 
    	two = new JLabel();
    	three = new JLabel();
    	four = new JLabel();
    	five = new JLabel();
    	six = new JLabel();
    	seven = new JLabel();
    	eight = new JLabel();
    	nine = new JLabel();
    	ten = new JLabel();
    	eleven = new JLabel();
    	twelve = new JLabel();
    	thirteen = new JLabel();
    	fourteen = new JLabel();
    	fifteen=new JLabel();
    	sixteen = new JLabel();
    	seventeen= new JLabel();
    	eighteen = new JLabel();
    	nineteen = new JLabel();
    	twenty = new JLabel();
    	twentyone = new JLabel();
     
     
     
     
    	one.setText("Combat "); 
    	two.setText("Armor");
    	three.setText("Speed");
    	four.setText("Hit Points Maximum");
    	five.setText ("Critical Hit Threshold");
    	six.setText ("Critical Hit Penalty");
    	seven.setText("Critical Speed Penalty");
    	eight.setText("Combat ");
    	nine.setText("Armor");
    	ten.setText("Speed");
    	eleven.setText("Hit Points Maximum");
    	twelve.setText("Critical Hit Threshold");
    	thirteen.setText ("Critical Hit Penalty");
    	fourteen.setText ("Critical Speed Penalty");
    	fifteen.setText("Unit Type");
    	sixteen.setText("Unit Type");
    	seventeen.setText("Species");
    	eighteen.setText("Species");
    	nineteen.setText("Combatant 1");
    	twenty.setText("Combatant 2");
    	twentyone.setText("Play by Play");
     
    	one.setBounds (50,170,80,20); 
    	two.setBounds (50,190,80,20);
    	three.setBounds (50,210,80,20);
    	four.setBounds (50,230,80,20);
    	five.setBounds (50,250,80,20);
    	six.setBounds (50,270,80,20);
    	seven.setBounds (50,290,80,20);
    	eight.setBounds (500,170,80,20);
    	nine.setBounds (500,190,80,20);
    	ten.setBounds (500,210,80,20);
    	eleven.setBounds (500,230,80,20);
    	twelve.setBounds (500,250,80,20);
    	thirteen.setBounds (500,270,80,20);
    	fourteen.setBounds (500,290,80,20);
    	fifteen.setBounds (50,100,80,20);
    	sixteen.setBounds (500,100,80,20);
    	seventeen.setBounds(50,130,80,20);
    	eighteen.setBounds(500,130,80,20);
    	nineteen.setBounds(150,65,80,20);
    	twenty.setBounds(600,65,80,20);
    	twentyone.setBounds(325,370,82,20);
     
     
    	add(one);
    	add(two);
    	add(three);
    	add(four);
    	add(five);
    	add(six);
    	add(seven);
    	add(eight);
    	add(nine);
    	add(ten);
    	add(eleven);
    	add(twelve);
    	add(thirteen);
    	add(fourteen);
    	add(fifteen);
    	add(sixteen);
    	add(seventeen);
    	add(eighteen);
    	add(nineteen);
    	add(twenty);
    	add(twentyone);
     
     
     
    	firstTextField = new JTextField(); 
    	second= new JTextField();
    	third= new JTextField();
    	fourth= new JTextField();
    	fifth= new JTextField();
    	sixth= new JTextField();
    	seventh= new JTextField();
    	eighth= new JTextField();
    	ninth= new JTextField();
    	tenth= new JTextField();
    	eleventh= new JTextField();
    	tweleth= new JTextField();
    	thirteenth= new JTextField();
    	fourteenth= new JTextField();
     
    	firstTextField.setText("%s,a");
    	second.setText("%d,");
    	third.setText("%d,");
    	fourth.setText("%d,");
    	fifth.setText("%d,");
    	sixth.setText("%d,");
    	seventh.setText("%d,");
    	eighth.setText("%d,");
    	ninth.setText("%d,");
    	tenth.setText("%d,");
    	tweleth.setText("%d,");
    	thirteenth.setText("%d,");
    	fourteenth.setText("%d,");
     
    	firstTextField.setBounds(150,170,40,20);
    	second.setBounds(150,190,40,20);
    	third.setBounds(150,210,40,20);
    	fourth.setBounds(150,230,40,20);
    	fifth.setBounds(150,250,40,20);
    	sixth.setBounds(150,270,40,20);
    	seventh.setBounds(150,290,40,20);
    	eighth.setBounds(600,170,40,20);
    	ninth.setBounds(600,190,40,20);
    	tenth.setBounds(600,210,40,20);
    	eleventh.setBounds(600,230,40,20);
    	tweleth.setBounds(600,250,40,20);
    	thirteenth.setBounds(600,270,40,20);
    	fourteenth.setBounds(600,290,40,20);
     
     
     
     
    	add(firstTextField);
    	add(second);
    	add(third);
    	add(fourth);
    	add(fifth);
    	add(sixth);
    	add(seventh);
    	add(eighth);
    	add(ninth);
    	add(tenth);
    	add(eleventh);
    	add(tweleth);
    	add(thirteenth);
    	add(fourteenth);
     
     
     
     
    	firstTextArea = new JTextArea();
    	firstTextArea.setText("");
    	firstTextArea.setBounds(10,400,760,150 );
    	add(firstTextArea); 
     
    	firstCheckBox= new JCheckBox();
    	firstCheckBox.setText("Save output to file");
    	firstCheckBox.setBounds(10,550,140,20);
    	add (firstCheckBox); 
     
    	{firstButton = new JButton();
    	secondButton= new JButton();
    	firstButton.setText("Go");
    	firstButton.addActionListener(new ActionListener() {
    		public void actionPerformed (ActionEvent e)	{	
    			if (firstButton.getText().equals("Go")){
    				firstButton.setText("Go");
     
    					String firstTextField =	JOptionPane.showInputDialog( "Enter first integer" );
    						  int a = Integer.parseInt( firstTextField );
     
    				}
    			else { 
    				firstButton.setText("Go");
    			}
    			}
    			}
    			);
    	}
     
     
    	secondButton.setText("Halt");
    	firstButton.setBounds(650,550,75,20);
    	secondButton.setBounds(550,550,75,20);
    	add(firstButton);
    	add(secondButton);
     
    	String [] combatstrings = {"Elf", "Drawve", "Human", "Goblin", "Orc"};
    	String [] Type = {"Infantry"};
    	firstComboBox= new JComboBox (combatstrings);
    	secondComboBox = new JComboBox (combatstrings);
    	thirdComboBox = new JComboBox(Type);
    	fourthComboBox = new JComboBox(Type);
    	firstComboBox.setBounds(150,130,140,20);
    	secondComboBox.setBounds(600,130,140,20);
    	thirdComboBox.setBounds (150,100,140,20);
    	fourthComboBox.setBounds (600,100,140,20);
     
    	add(firstComboBox);	
    	add(secondComboBox);
    	add(thirdComboBox);
    	add(fourthComboBox);
     
     
    	setSize (800,640);            
    	setTitle("Basic Title" ); 
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    	setVisible(true); 
     
    	}
     
     
     
     
     
    	public static void main(String[] args) {
    		new SimpleWindow(); .
     
    	}
     
    }>


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help on my code

    Please edit your post and wrap the code in code tags to preserve formatting and make it easier to read.

    it wouldn’t give me a value, just my placeholder - %d
    Can you explain what happens? I don't understand what you are trying to say.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Leonardo1143 (December 11th, 2012)

  4. #3
    Member
    Join Date
    Dec 2012
    Posts
    31
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Need help on my code

    Quote Originally Posted by Norm View Post
    Please edit your post and wrap the code in code tags to preserve formatting and make it easier to read.


    Can you explain what happens? I don't understand what you are trying to say.
    how do i wrap the code in code tags so it makes it easier to read?

    also what happens is that "%d,int a" is displayed not my value, in this case 25. all it displays in the textfield is "%d,inta" i want to know how to make it so wben i press "Go" it gives my value not my placeholder. sorry about the format of my first post im new to this site.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help on my code

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    "%d,int a" is displayed
    Where is that String displayed? I see lots of textfields that are loaded with: "%d"
    If on the console window, please copy the full contents of the console window and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Leonardo1143 (December 11th, 2012)

  7. #5
    Member
    Join Date
    Dec 2012
    Posts
    31
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Need help on my code

    Quote Originally Posted by Norm View Post
    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.


    Where is that String displayed? I see lots of textfields that are loaded with: "%d"
    If on the console window, please copy the full contents of the console window and paste it here.
    Okay, I think I wrapped it. Tell me if i did it incorrectly, though.

    I can't copy the window right now since I am not on my programming computer. What I am trying to do is get FirstTextField to give me a value of 25 when I press Go on my screen. I tried to set it to "%d, int a" in hopes of it reading the value of int a, which is 25, and getting it to set the value when I press go. But when i do run my program FirstTextField always reads "%d, int a" which I don't want. I want it to be blank into a user presses go. At that point it will give me a value of 25. I am really sorry if I am hard to work with I have only started programming for about four months,

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help on my code

    If you preload a textfield with a String and then call the textfield's getText() method, you will get back what you put into it. If you put "%d" in the textfield that is what you will read back with getText().
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    Leonardo1143 (December 11th, 2012)

  10. #7
    Member
    Join Date
    Dec 2012
    Posts
    31
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Need help on my code

    Quote Originally Posted by Norm View Post
    If you preload a textfield with a String and then call the textfield's getText() method, you will get back what you put into it. If you put "%d" in the textfield that is what you will read back with getText().
    Okay. but if I leave it as 25. it will show in the TextField even if I never press GO how do I fix that?

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help on my code

    how do I fix that?
    Sorry, I don't know what you are trying to fix. Can you explain what you see when the code executes and what you want to change?
    What do you want the program to display?
    What do you want the program to do when the "Go" button is pressed?
    If you don't understand my answer, don't ignore it, ask a question.

  12. The Following User Says Thank You to Norm For This Useful Post:

    Leonardo1143 (December 10th, 2012)

  13. #9
    Member
    Join Date
    Dec 2012
    Posts
    31
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Need help on my code

    Quote Originally Posted by Norm View Post
    Sorry, I don't know what you are trying to fix. Can you explain what you see when the code executes and what you want to change?
    What do you want the program to display?
    What do you want the program to do when the "Go" button is pressed?
    Okay, again sorry if I am hard to work with, but I want my program to display blank TextFields when it is first started. Then when the GO Button is pressed I want the values put in. For example combat I want to be 25 when I Press Go and only when I Press go.

  14. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help on my code

    display blank TextFields when it is first started.
    Then don't call the setText() methods with Strings until you want data put in the textfields.
    when the GO Button is pressed I want the values put in
    When the "GO" button is pressed, then in the listener method call setText() to put the values in the textfields.
    If you don't understand my answer, don't ignore it, ask a question.

  15. The Following User Says Thank You to Norm For This Useful Post:

    Leonardo1143 (December 11th, 2012)

  16. #11
    Member
    Join Date
    Dec 2012
    Posts
    31
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Need help on my code

    Quote Originally Posted by Norm View Post
    Then don't call the setText() methods with Strings until you want data put in the textfields.

    When the "GO" button is pressed, then in the listener method call setText() to put the values in the textfields.
    For the first part how would I go about changing my code so it wont call the setText() methods?

  17. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help on my code

    how would I go about changing my code so it wont call the setText() methods?
    Two ways:
    delete all the lines that call the setText() method
    Put the calls to the setText() method inside a comment block: /* ... */
    If you don't understand my answer, don't ignore it, ask a question.

  18. The Following User Says Thank You to Norm For This Useful Post:

    Leonardo1143 (December 11th, 2012)

  19. #13
    Member
    Join Date
    Dec 2012
    Posts
    31
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Need help on my code

    Okay which line of code is calling the setText()? I can't see it? Because I thought I needed to place getText in order for the setText to be called. I'm guessing this is wrong?

  20. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help on my code

    which line of code is calling the setText()? I can't see it?
    Do you have the source in an editor with a Find function?
    In the editor: Open the Find function and typing in: setTexT
    If Find works, it should position the editor to a line with setText

    If your editor does not have a Find function, change to another editor.
    If you don't understand my answer, don't ignore it, ask a question.

  21. The Following User Says Thank You to Norm For This Useful Post:

    Leonardo1143 (December 11th, 2012)

  22. #15
    Member
    Join Date
    Dec 2012
    Posts
    31
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Need help on my code

    Quote Originally Posted by Norm View Post
    Do you have the source in an editor with a Find function?
    In the editor: Open the Find function and typing in: setTexT
    If Find works, it should position the editor to a line with setText

    If your editor does not have a Find function, change to another editor.
    Oh. I think I'm getting it. Remove setText(). from the method and move into the action listener only?

  23. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help on my code

    Can the setText() method WHEN you want to change the contents of a textfield.

    If you are confused about when a section of code is executed, add println calls to the different sections and have them print out a message when they are executed. Looking at the messages as they are printed you'll know which section has been executed.
    If you don't understand my answer, don't ignore it, ask a question.

  24. The Following User Says Thank You to Norm For This Useful Post:

    Leonardo1143 (December 11th, 2012)

  25. #17
    Member
    Join Date
    Dec 2012
    Posts
    31
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Need help on my code

    Thank You, Norm. I got it to work thank you for being so patient with me.

  26. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help on my code

    Glad you got it to work.
    If you don't understand my answer, don't ignore it, ask a question.

  27. The Following User Says Thank You to Norm For This Useful Post:

    Leonardo1143 (December 11th, 2012)

Similar Threads

  1. Trouble Porting my Java File Reading Code to Android Code
    By Gravity Games in forum Android Development
    Replies: 0
    Last Post: December 6th, 2012, 04:38 PM
  2. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  3. Help needed - Embedding javascript code in JSP code.
    By chinnu in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: October 29th, 2012, 01:09 PM
  4. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM
  5. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM