Results 1 to 8 of 8
  1. #1
    Nilla is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default How do I iterate

    In my multiplicationtable tester I can´t get it to function when I choose the nyFraga (new question) -button.
    This is my code:

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    class Multiplikationstabelltestare extends JFrame implements ActionListener {
    
    //    Data Members
    	private static final int FRAME_WIDTH    = 400;
        private static final int FRAME_HEIGHT   = 400;
        private static final int FRAME_X_ORIGIN = 150;
        private static final int FRAME_Y_ORIGIN = 250;
        private static final String x = " X ";
        private static final String likhetsTecken = "=";
    
        private static JFrame frame;
        private static JPanel fragaPanel, resultatPanel, buttonPanel, controlPanel ;
        private static JTextField inputLine;
        private static JLabel label1;
        private static JLabel label2;
        private static JLabel label3;
        private static JLabel label4;
        private static JLabel label5;
        private static Container contentPane;
        private JButton kontrollButton;
        private JButton avslutaButton;
        private JButton nyButton;
        private int random1;
        private int random2;
        private int produkt;
        private int resultat;
        private int number;
        private static String svar;
        private String randoma;
        private String randomb;
    
        // Main method
        public static void main(String[] args) {
        	Multiplikationstabelltestare frame = new Multiplikationstabelltestare();
           	frame.setVisible(true);
        }
    
        // Constructor
        public Multiplikationstabelltestare() {
    
        	//set the frame properties
         	setSize      (FRAME_WIDTH, FRAME_HEIGHT);
            setResizable (false);
            setTitle     ("Multiplikationstabelltestare");
            setLocation  (FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
            contentPane = getContentPane();
    
    		// skapar gridLayout för talet
            contentPane.setLayout(new GridLayout(3,1));
            fragaPanel = new JPanel();
            fragaPanel.setBorder(BorderFactory.createTitledBorder("Multiplicera följande tal:"));
            fragaPanel.add(label1=new JLabel(randomNumber1()));
            fragaPanel.add(label2=new JLabel(x));
            fragaPanel.add(label3=new JLabel(randomNumber2()));
            fragaPanel.add(label4=new JLabel(likhetsTecken));
            fragaPanel.add(inputLine=new JTextField(4));
    
    		// formaterar innehålet i gridLayout
            label1.setFont(new Font("Courier", Font.PLAIN, 24));
            label2.setFont(new Font("Courier", Font.PLAIN, 24));
            label3.setFont(new Font("Courier", Font.PLAIN, 24));
            label4.setFont(new Font("Courier", Font.PLAIN, 24));
            inputLine.setFont(new Font("Courier", Font.PLAIN, 24));
    
    		// skapa fält för redovisning av testresultatet
            resultatPanel = new JPanel();
            resultatPanel.setBorder(BorderFactory.createTitledBorder("Resultat"));
            resultatPanel.add(label5 = new JLabel(" "));
            label5.setFont(new Font("Courier", Font.PLAIN, 24));
            label5.setVisible(true);
    
    		// skapa fält för knapparna
    		controlPanel = new JPanel(new BorderLayout());
    		buttonPanel = new JPanel();
    		buttonPanel.add(kontrollButton = new JButton("Kontrollera"));
    		buttonPanel.add(nyButton = new JButton("Ny fråga"));
    		buttonPanel.add(avslutaButton = new JButton("Avsluta"));
    		controlPanel.add(buttonPanel, BorderLayout.SOUTH);
    
    		// actionHandlers
            nyButton.addActionListener(this);
    		kontrollButton.addActionListener(this);
    		avslutaButton.addActionListener(this);
    
            // panelerna läggs till containern (contentPane)
            contentPane.add(fragaPanel);
            contentPane.add(resultatPanel);
            contentPane.add(buttonPanel);
    
          	// avslutar operationen
            setDefaultCloseOperation( EXIT_ON_CLOSE );
        }
        // metod för att hantera eventen
        public void actionPerformed(ActionEvent event) {
            if (event.getSource() instanceof JButton) {
                JButton clickedButton = (JButton) event.getSource();
                if (clickedButton == kontrollButton || clickedButton == nyButton) {
                   	svar = inputLine.getText();
                    kontrolleraResultat(svar);
                }
            }
    
            if (event.getSource() instanceof JButton) {
                JButton clickedButton = (JButton) event.getSource();
                if (clickedButton == avslutaButton) {
                   	avsluta();
                }
            }
    
            if (event.getSource() instanceof JButton) {
                JButton clickedButton = (JButton) event.getSource();
                if (clickedButton == nyButton) {
                	nyFraga();
                }
            }
         }
    
        private String randomNumber1(){
        	random1 = (int)(9*Math.random() +1);
        	randoma = String.valueOf(random1);
        	return randoma;
        }
    
        private String randomNumber2(){
        	random2 = (int)(9*Math.random() +1);
        	randomb = String.valueOf(random2);
        	return randomb;
        }
    
        private void kontrolleraResultat(String svar) {
    		 	produkt= random1 * random2;
    		 	if(Integer.parseInt(svar)==produkt){
                	label5.setText("Rätt svar!");
                	label5.setVisible(true);
                }
    		 	else if(Integer.parseInt(svar)!=produkt)
    		 	{
                	label5.setText("Fel svar!");
                	label5.setVisible(true);
                }
        }
    
    	private void nyFraga(){  // new question in English
    			label1.setText(randomNumber1());
    			randomNumber1();
    			label2.setText(x);
    			label3.setText(randomNumber2() + " ");
    			randomNumber2();
    			label4.setText(likhetsTecken + " ");
    			label5.setVisible(false);
    			inputLine.setText(" ");
                            
                           // DO MAGIC
    	}
    
    	private void avsluta(){
    			System.exit(0);
    	}
    
    }
    Another problem I have is that Ecplise doesn´t accept BorderLayout, while JCreator does accept it. Why this difference?

    /Nilla
    Last edited by Nilla; 06-22-2012 at 06:05 AM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: How do I iterate

    In my multiplicationtable tester I can´t get it to function when I choose the nyFraga (new question) -button.
    Add some println debugging statements - does the ActionListener fire when you expect? If so, debug further down the assembly line. If not, debug further back up the assembly line.

    Another problem I have is that Ecplise doesn´t accept BorderLayout, while JCreator does accept it. Why this difference?
    Define 'accept'. Please post all error codes

  3. #3
    Nilla is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: How do I iterate

    Quote Originally Posted by doWhile View Post
    Add some println debugging statements - does the ActionListener fire when you expect? If so, debug further down the assembly line. If not, debug further back up the assembly line.

    The actionListener fire once, but not when I push the nyButton and then the kontrollButton to check my answer.
    The I get the following error message:

    --------------------Configuration: <Default>--------------------
    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: " 8"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:449)
    at java.lang.Integer.parseInt(Integer.java:499)
    at Multiplikationstabelltestare.kontrolleraResultat(M ultiplikationstabelltestare.java:136)
    at Multiplikationstabelltestare.actionPerformed(Multi plikationstabelltestare.java:103)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6288)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:605 3)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4651)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 81)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 81)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:643)
    at java.awt.EventQueue.access$000(EventQueue.java:84)
    at java.awt.EventQueue$1.run(EventQueue.java:602)
    at java.awt.EventQueue$1.run(EventQueue.java:600)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$2.run(EventQueue.java:616)
    at java.awt.EventQueue$2.run(EventQueue.java:614)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 613)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)



    Define 'accept'. Please post all error codes
    Error code:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The constructor JPanel(BorderLayout) is undefined
    SOUTH cannot be resolved or is not a field

    at Multiplikationstabelltestare.<init>(Multiplikation stabelltestare.java:77)
    at Multiplikationstabelltestare.main(Multiplikationst abelltestare.java:38)
    Last edited by Nilla; 06-22-2012 at 07:42 AM.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: How do I iterate

    For the compilation error, post an SSCCE

    For the exception, " 8" is not a number, "8" is. Trim the string prior to attempting to parse it

  5. #5
    Nilla is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: How do I iterate

    Sorry, I am a java-beginner and don´t understand what you mean.
    Thank you anyway for your answer.
    /Nilla

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: How do I iterate

    The space inside of the "s caused the error. Remove all the spaces by calling the trim() method before trying to parse the String to an int.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Nilla is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: How do I iterate

    Thank you! The error message is now gone, but I still have some problems.

    1. How do I empty the answer field when I update with new randomNumbers?
    2. How do I get an ActionEvent the second time with the new answer?

    Java Code:
    private void kontrolleraResultat(String svar) {
    		 	produkt= random1 * random2;
    		 	if(Integer.parseInt(svar)==produkt){
                	label5.setText("Rätt svar!");
                	label5.setVisible(true);
                }
    		 	else if(Integer.parseInt(svar)!=produkt)
    		 	{
                	label5.setText("Fel svar!");
                	label5.setVisible(true);
                }
        }
    
    	private void nyFraga(){
    			label1.setText(randomNumber1());
    			randomNumber1();
    			label2.setText(x);
    			label3.setText(randomNumber2());
    			randomNumber2();
    			label4.setText(likhetsTecken);
    			label5.setVisible(false);
    		//	inputLine.getText(" ");  This code was to empty the field from earlier answer
    			kontrolleraResultat(svar);
    	}
    The Eclipse problem was solved with import java.awt.BorderLayout; Why was not import java.awt.*; enough??
    Last edited by Nilla; 06-23-2012 at 04:56 AM.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,250
    Rep Power
    19

    Default Re: How do I iterate

    Quote Originally Posted by Nilla View Post
    The Eclipse problem was solved with import java.awt.BorderLayout; Why was not import java.awt.*; enough??
    You must be having another BorderLayout.class somewhere on the classpath, either one you created or one you downloaded, maybe inside of a .jar.

    That's why it's always a bad idea to name any of your own classes the same as a commonly used JDK class.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. How to iterate over objects in a class?
    By Neilos in forum New To Java
    Replies: 10
    Last Post: 08-18-2011, 08:02 PM
  2. iterate through a HashMap ?
    By aneuryzma in forum New To Java
    Replies: 2
    Last Post: 03-27-2011, 03:26 PM
  3. Iterate 2 maps and adding value to each
    By metal_pat in forum New To Java
    Replies: 3
    Last Post: 02-09-2011, 12:23 PM
  4. How to iterate through two Arrays?
    By aRTx in forum Advanced Java
    Replies: 3
    Last Post: 05-18-2010, 07:48 AM
  5. iterate HashMap with logic
    By Heather in forum Web Frameworks
    Replies: 2
    Last Post: 07-03-2007, 09:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •