Results 1 to 15 of 15

Thread: Java code     submit to reddit submit to twitter

  1. #1
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    Java code

    Ok i need to make a program that displays a GUI that has 2 buttons, 1 increases the count, one decreases, and a label that displays the current count. I know how to do it and the code i have should work although im getting a lot of "Cannot find symbol" errors. Can anyone help me out? I fell like im missing something small

    Code:
    import javax.swing.JFrame;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Adder {
    
    	public static void main (String[] args) {
    		JFrame frame = new JFrame ("Assignment 5");
    		frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    		
    		frame.getContentPane().add(AdderPanel);
    		
    		frame.pack();
    		frame.setVisible(true);
    	}
    	
    	public class Adderpanel extends JPanel {
    		
    		private int count;
    		private JButton left, right;
    		private JLabel label;
    		private JPanel buttonPanel;
    	
    		public Adderpanel () {
    			left = new JButton ("Increment");
    			right = new JButton ("Decrement");
    			
    			ButtonListener listener = new ButtonListener();
    			left.addActionListener (listener);
    			right.addActoinListener (listener);
    			
    			label = new JLabel ("Count");
    			
    			buttonPanel = new JPanel();
    			buttonPanel.add(left);
    			buttonPanel.add(right);
    			
    			add(label);
    			add(buttonPanel);
    		}
    	}
    	
    	private class ButtonListener implements ActionListener {
    		
    		public void actionPerformed (ActionEvent event) {
    			if (event.getSource() == left) {
    				count ++;
    				label.setText("Pushes: " + count);
    			}
    			else {
    				count --;
    				label.SetText("Pushes: " + count);
    			}
    		}
    	}
    }

  2. #2
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    fixed most of the errors, i closed the Adderpanel to early. Once i fixed it I only have 1 error left.

    cannot find symbol "Variable AdderPanel"
    frame.getContentPane().add(Adderpanel);


    Fixed code so far:

    Code:
    import javax.swing.JFrame;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Adder {
    
    	public static void main (String[] args) {
    		JFrame frame = new JFrame ("Assignment 5");
    		frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    		
    		frame.getContentPane().add(Adderpanel);
    		
    		frame.pack();
    		frame.setVisible(true);
    	}
    	
    	public class Adderpanel extends JPanel {
    		
    		private int count;
    		private JButton left, right;
    		private JLabel label;
    		private JPanel buttonPanel;
    	
    		public Adderpanel () {
    			left = new JButton ("Increment");
    			right = new JButton ("Decrement");
    			
    			ButtonListener listener = new ButtonListener();
    			left.addActionListener (listener);
    			right.addActionListener (listener);
    			
    			label = new JLabel ("Count");
    			
    			buttonPanel = new JPanel();
    			buttonPanel.add(left);
    			buttonPanel.add(right);
    			
    			add(label);
    			add(buttonPanel);
    		}
    	
    	
    		private class ButtonListener implements ActionListener {
    			
    			public void actionPerformed (ActionEvent event) {
    				if (event.getSource() == left) {
    					count ++;
    					label.setText("Pushes: " + count);
    				}
    				else {
    					count --;
    					label.setText("Pushes: " + count);
    				}
    			}
    		}
    	}
    }

  3. #3
    Banned.

    Join Date
    Jul 2005
    Posts
    5,821
    BG Level
    8
    FFXI Server
    Sylph
    WoW Realm
    Arthas

    Not to be rude, but I've helped you enough for free. If you're not able to figure out how to debug errors, you shouldn't be doing programming.

    No more help from me, friend. At least not for free. If you want help, $40 an hour

    Best of luck.

  4. #4
    Relic Shield
    Join Date
    Oct 2006
    Posts
    1,695
    BG Level
    6

    Taking a quick gander... your problem seems to be on line 11:

    Code:
    frame.getContentPane().add(Adderpanel);
    Symbol not found exceptions are pretty self explanatory. Adderpanel is a variable that hasn't been declared or initialized. Your form (spacing, variable/object capitalization schemes, etc.) are all fucked up too. I wouldn't code this way at all.

  5. #5
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    The spacing is messed up because of the way it is passed in. It doesn't really look like that i swear XD. I found that error right before checking here >< Missed where it says new in the book. Should have been.

    Code:
    frame.getContentPane().add(new Adderpanel());
    But once I do this it starts saying i cant reference non static variables in a static function.

  6. #6
    Relic Shield
    Join Date
    Oct 2006
    Posts
    1,695
    BG Level
    6

    Shouldn't need the book to tell it to you. What sort of class are you taking that is asking you to write simple GUI's without showing you extremely basic object oriented stuff like how to create instances of objects first?

    Make the Adderpanel class static. Code should run.

    If you want to see why read this:

    http://download.oracle.com/javase/tu...OO/nested.html

    and if you want to avoid this sort of crap, don't write with nested classes, especially if you're a noob.

  7. #7
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    I took out the nested class and made it a separate file and it fixed all the errors, works like a charm. Thank you for the help!

  8. #8
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    I have a new question. I have to make a E-mail simulator. I have it finished except its not layed out the way I would like. When the user hits the "Send" button it should print to a file called email.txt (which it does). But the layout is like this:


    To : brettCC : whitneySubject : heyMessage : yea thats cool

    It should read:

    To: brett
    cc: Whitney
    Subject: hey
    message: yea thats cool

    how do i insert breaks after each line? the writing section of the code is:

    Code:
    	private class ButtonListener implements ActionListener {
    		public void actionPerformed (ActionEvent event) {
    			
    			try {
    				FileWriter fstream = new FileWriter("email.txt");
    				BufferedWriter out = new BufferedWriter(fstream);
    				
    				String toText = toField.getText();
    				String ccText = ccField.getText();
    				String subjectText = subjectField.getText();
    				String messageText = messageField.getText();
    				out.write("To : " + toField.getText());
    				out.write("CC : " + ccField.getText());
    				out.write("Subject : " + subjectField.getText());
    				out.write("Message : " + messageField.getText());
    				out.close();
    			}
    			catch (Exception e) {
    				System.err.println("Error: " + e.getMessage());
    			}
    		}
    	}

    Thanks!

  9. #9
    Chram
    Join Date
    Nov 2007
    Posts
    2,662
    BG Level
    7
    FFXIV Character
    Avelle Asteria
    FFXIV Server
    Hyperion
    FFXI Server
    Phoenix
    WoW Realm
    Alleria

    I haven't worked with streamwriters in Java so maybe this is a dumb suggestion, but in your out.write calls can you just concatenate a "\n" to it?

    Something like:
    Code:
    out.write("To : " + toField.getText() + "\n");
    If the bufferedwriter out works anything like System.out I would think it should work, but again I haven't dealt with streams in Java.

  10. #10
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    well I had done that originally but it was actually putting /n into the text... i'll try it again and mess around with it a little more.

  11. #11
    The Wang
    Join Date
    Jun 2006
    Posts
    1,343
    BG Level
    6
    FFXIV Character
    Furt Wangler
    FFXIV Server
    Coeurl
    FFXI Server
    Sylph

  12. #12
    E. Body
    Join Date
    Jan 2007
    Posts
    2,103
    BG Level
    7

    / != \

  13. #13
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    so a line separator would be the best route?

  14. #14
    Chram
    Join Date
    Nov 2007
    Posts
    2,662
    BG Level
    7
    FFXIV Character
    Avelle Asteria
    FFXIV Server
    Hyperion
    FFXI Server
    Phoenix
    WoW Realm
    Alleria

    The description of newline() seems a little strange to me. I'd try both routes and see what happens.

  15. #15
    Puppetmaster
    Join Date
    Oct 2009
    Posts
    54
    BG Level
    2
    FFXI Server
    Diabolos

    I hate to be a complete ass, but 99% of coding is debugging. Any monkey with 2 hours of training can churn out java code. Programmers get paid to debug. If you don't start debugging your own code now, you have NO chance at a job in the future.

    With that in mind, use an IDE like Netbeans or Eclipse. Either one of those programs will help you debug and have you on your way to learning how to code and debug properly.

    Netbeans: http://netbeans.org/

    Eclipse: http://www.eclipse.org/

Similar Threads

  1. Java code question
    By Skie in forum Tech
    Replies: 4
    Last Post: 2011-01-13, 23:33
  2. Java Code help
    By Skie in forum Tech
    Replies: 11
    Last Post: 2010-10-11, 14:37
  3. more java code
    By Skie in forum Tech
    Replies: 0
    Last Post: 2010-09-24, 20:14