1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Complete Java Newbie

Discussion in 'Java' started by Taharka, Oct 18, 2011.

  1. Taharka

    Taharka New Member

    Joined:
    Oct 8, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Between jobs
    Location:
    Lancashire United Kingdom
    No matter how many times I check it seems OK, but the compiler keeps screaming that there are errors, very frustrated any help will be greatly appreciated. The two errors are at the bottom of the program I will also be able to get a good nights sleep not thinking about the code and were I went wrong cheers..




    Code:
    // java tutorial 68 JRadioButton
    // Java tutorial 67 final JRadioButton 
    // Java tutorial 69 Drop down list program
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class BT68Gui extends JFrame{
        
        private JComboBox box;
        private JLabel picture;
        
        private static String[] filename = {"b.png, x.png"};// and Array with two elements this Array stores the filename
        private Icon[] pics = {new ImageIcon(getClass().getResource(filename[0])), new ImageIcon(getClass().getResource(filename[1]))}; // This Array stores the pictures filename 0 is the first element of the Aray
        
        public BT68Gui(){
            super("The Title");
            setLayout(new FlowLayout());
            
                
            box = new JComboBox(filename);         
            box.addItemListener(
                    new ItemListener(){
                        public void itemStateChanged(ItemEvent event){ od
                        if(event.getStateChange()==ItemEvent.SELECTED)
        picture.setIcon(pics[box.getSelectedIndex()]);/
                        }
                   }
           };                      //compiler shows error
           add(box);       // compiler shows error
           picture = new JLabel(pics[0]);
           add(picture);
      }
    }
     
    Last edited by a moderator: Oct 19, 2011
  2. ewaldhorn

    ewaldhorn New Member

    Joined:
    Feb 16, 2010
    Messages:
    36
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    Cape Town, South Africa
    Home Page:
    Very interesting.

    Might I suggest the following?

    While you are learning Java, try to lign up your code logically, like the following example:

    Code:
    [FONT="Fixedsys"]
    if (condition)
    {
          for (something)
          {
          }
    }
    [/FONT]
    

    Notice how the brackets line up? In your code, you have brackets that EXCLUDE portions of the code "add(box)" from the body of the method they belong to. When you align the brackets, things start to look a lot better all of a sudden and it's easy to spot the problem areas.

    Formatting code properly is going to help you a lot, especially as your programs grow in size and complexity.

    I hope this helps.
     
  3. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    see the errors in your code in order to compile

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class BT68Gui extends JFrame{
        
        private JComboBox box;
        private JLabel picture;
        
        private static String[] filename = {"b.png, x.png"};// and Array with two elements this Array stores the filename
        private Icon[] pics = {new ImageIcon(getClass().getResource(filename[0])), new ImageIcon(getClass().getResource(filename[1]))}; // This Array stores the pictures filename 0 is the first element of the Aray
        
        public BT68Gui(){
            super("The Title");
            setLayout(new FlowLayout());
            
                
            box = new JComboBox(filename);         
            box.addItemListener(
                    new ItemListener(){
                        public void itemStateChanged(ItemEvent event){ [COLOR=Red]//od [/COLOR] <--error
                        if(event.getStateChange()==ItemEvent.SELECTED)
                            picture.setIcon(pics[box.getSelectedIndex()]);[COLOR=Red]// error[/COLOR]
                        }
                   }
           [COLOR=Red]);   //error[/COLOR]                   //compiler shows error
           add(box);       // compiler shows error
           picture = new JLabel(pics[0]);
           add(picture);
      }
    }
    
    
    
     

Share This Page