making a set of code copy and pasteable to someone elses editor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • carlos123
    New Member
    • Sep 2007
    • 53

    making a set of code copy and pasteable to someone elses editor

    ok the following code , is the code for my program, basicly, i use bluej, so i can just copy and paste some code into the forums and someone else can copy it in to their editor, so basicly i want someone to show me how to make this set of code to be able to run itself, i have marked the main areas of the code like the main and graphics panel etc sorry about the load of code, but this is important to me that i get this all working together. thanks guys



    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Calendar;
    import java.text.SimpleDateFormat;
    import java.lang.Object;
    import java.io.*;
    import javax.swing.event.*;
    import javax.swing.JCheckBox;
    public static void main(String[] args)	]// main statement, i want it to goto 
       {
          main m = new main();
       }
    public class main implements ActionListener   //this
    {
            JFrame f1,f2;
            JPanel p1,p2,p3,p4;
            String id,enterstring,name,namestring,idstring,str;
            GP g1;
            JRadioButton bathroom,office,nurse,other;
            JButton submit,returnbutton;
            JTextField idnumber;
            JLabel idprompt,what,time,namelabel;
            boolean show,bathroomb,officeb,nurseb,otherb;
            int idnum,counter;
            SimpleDateFormat sdf;
           String[] namearray; 
           int[] idarray;
           int a,b,idint,enter,teacher;
         public main()
        {   
            teacher = 1234567890;
             bathroomb = false;
             officeb = false;
             nurseb = false;
             otherb = false;
            namearray = new String[3];
            idarray = new int[3];
         sdf = new SimpleDateFormat("yyyy.MM.dd G 'at' hh:mm:ss z");  
           show=true;
            a=0;
            b=0;
            namelabel = new JLabel("");
           bathroom = new JRadioButton("Bathroom");
            bathroom.setMnemonic(KeyEvent.VK_C);
            bathroom.setSelected(false);
            bathroom.addActionListener(this);
            counter = 1;
           office = new JRadioButton("Office");
           office.setMnemonic(KeyEvent.VK_C);
            office.setSelected(false);
             office.addActionListener(this);
           nurse = new JRadioButton("Nurse");
            nurse.setMnemonic(KeyEvent.VK_C);
            nurse.setSelected(false);
             nurse.addActionListener(this);
           other = new JRadioButton("Other");
            other.setMnemonic(KeyEvent.VK_C);
            other.setSelected(false);
             other.addActionListener(this);
            idprompt = new JLabel("Enter ID Number");
            idnumber = new JTextField(14);
            f1 = new JFrame("Hall Pass");
              f1.setSize(550,200);        
            Container c1 = f1.getContentPane();
            time = new JLabel("left at "+sdf.format(new java.util.Date(System.currentTimeMillis())));
            id = "123";
            p1 = new JPanel();
            p1.setSize(500,150);
            p1.setLayout(new BorderLayout());
            
              what = new JLabel("");
    
           bathroom.addItemListener(new Item());
           other.addItemListener(new Item());
            nurse.addItemListener(new Item());
            office.addItemListener(new Item());
            returnbutton = new JButton("Return");
            returnbutton.addActionListener(this);
            submit = new JButton("Submit");
            submit.addActionListener(this);
    
            p2 = new JPanel();  //panel to add JLabel and TextField
    
            g1 = new GP();    // runs the graphics panel down there
             p1.add(g1,BorderLayout.CENTER);
    
                      //g1.setBackground(new RGB(255, 255, 255));
           g1.add(idprompt);
           g1.add(idnumber);
            p2.add(submit);
            g1.add(what);
        
            p1.add(p2,BorderLayout.SOUTH);      //adds p2 to south (Bottom) Graphics Panel will be added to Center    
            c1.add(p1);
           
         
            f1.show();
           
          
        }
        
        
      
       
     
       
        public class Item implements ItemListener{
            
           public void itemStateChanged(ItemEvent evt) {
        
                if (evt.getStateChange() == ItemEvent.SELECTED) {
                
                 
                 if (bathroom.isSelected()){
                     
                     bathroomb = true;
                     System.out.println(bathroomb);
                    }
                 if (office.isSelected()){
                     
                     officeb = true;
                    }    
                 if (nurse.isSelected()){
                     
                     
                     nurseb = true;
                    }  
                 if (other.isSelected()){   
                     otherb = true;
                     
                    }
            }
               
               
               
               
               
            }
        }
    
          public void actionPerformed (ActionEvent event)
          {
      try {
            BufferedReader in = new BufferedReader(new FileReader("names.txt"));
              
            if (event.getSource() == returnbutton)
            {
                f1.show();
               
             
            }
            
           //idnum = Integer.Parseint(enter);
        
          
            if (event.getSource() == submit)
            {
             
           for(int i=0; i<6; i++){
    	str = in.readLine().trim() ;
    	   if(i<3){
    		  namearray[a] = str;
    		  a++;
    	    }else{
    		    idarray[b] = Integer.parseInt(str);
    		    b++;
    	     }
    }
    
    
            
            in.close();
          
    
    
               enterstring = idnumber.getText();
               enter = Integer.parseInt(enterstring);
               for(int b = 0;  b< 3; b++)
               {
                   //number 1
               if (enter==idarray[0])
               {
                     g1.remove(idnumber);
                     p1.remove(submit);
                     g1.remove(what);
                     g1.remove(idprompt);
                      p2.remove(submit);
                      namelabel.setText(namearray[0]);
                      g1.add(namelabel);
                     g1.add(bathroom);
                     g1.add(office);
                     g1.add(nurse);
                     g1.add(other);
                                          f1.dispose();
                          f1.show(); 
                     
                    
                 
             
                     
                     
                     
                   //  nurse.addItemListener(new ItemListener() {
                  
                        if (bathroomb == true){
                            g1.remove(namelabel);     
                     g1.remove(namelabel);
                     g1.remove(bathroom);
                     g1.remove(office);
                     g1.remove(nurse);
                     g1.remove(other);
                          g1.setBackground(Color.red);
                          p2.add(returnbutton);
                          namelabel.setText(namearray[0]+" is currently using the pass the time left was ");
                           g1.add(namelabel);
                           g1.add(time);
                         f1.dispose();
                          f1.show();
                        }
                        else             
                        
                        
                        
                        
                        
                      if (officeb == true){
                           g1.remove(namelabel);     
                     g1.remove(namelabel);
                     g1.remove(bathroom);
                     g1.remove(office);
                     g1.remove(nurse);
                     g1.remove(other);
                          g1.setBackground(Color.red);
                 
                          p2.add(returnbutton);
                          namelabel.setText(namearray[0]+" is currently using the pass the time left was ");
                           g1.add(namelabel);
                           g1.add(time);
                          f1.dispose();
                          f1.show();
                          
                        }
                        
                        
                        
                        
                        
                        
                        
                        
                        
                      if (nurseb == true){
                          
                           g1.setBackground(Color.red);
                 
                          p2.add(returnbutton);
                          namelabel.setText(namearray[0]+" is currently using the pass the time left was ");
                           g1.add(namelabel);
                           g1.add(time);
                          
                          
                          
                        }
                        
                        
                        
                        
                        
                        
                      if (otherb == true){
                          
                           g1.setBackground(Color.red);
                 
                          p2.add(returnbutton);
                          namelabel.setText(namearray[0]+" is currently using the pass the time left was ");
                           g1.add(namelabel);
                           g1.add(time);
                          
                          
                        }
                              
                    
                    }
                     else{
                 what.setText("Incorrect Login");
                }
                     
                      
                    
                     
                  //g1.setBackground(Color.red);
                 
                 // p2.add(returnbutton);
                 
                 // g1.add(time);
                  f1.dispose();
                  f1.show();
                }
            
                
            //number 2
            if (enter==teacher)
            {
                f1.dispose();
            }
              if (enter == idarray[1])
               {
                     g1.remove(idnumber);
                     p1.remove(submit);
                     g1.remove(what);
                     g1.remove(idprompt);
                  g1.setBackground(Color.red);
                  p2.remove(submit);
                  p2.add(returnbutton);
                   namelabel.setText(namearray[1]);
                 g1.add(namelabel);
                  g1.add(time);
                  f1.dispose();
                  f1.show();
                }
            
                else{
                 what.setText("Incorrect Login");
                }
            }
              if (enter == idarray[2])
               {
                     g1.remove(idnumber);
                     p1.remove(submit);
                     g1.remove(what);
                     g1.remove(idprompt);
                  g1.setBackground(Color.red);
                  p2.remove(submit);
                  p2.add(returnbutton);
                   namelabel.setText(namearray[2]);
                 g1.add(namelabel);
                  g1.add(time);
                  f1.dispose();
                  f1.show();
                }
            
                else{
                 what.setText("Incorrect Login");
                }
            
        
                idnumber.setText(null);
          
               
               
           
    
           
    
              g1.repaint();
     } catch (IOException e) {
        }
            }
        }
     
       
    	
    
    public class GP extends JPanel  //graphics panel
    
    {
       
    
        public GP()
         {      
         
          
            
             
            setBackground(Color.green);
            
         }
       
      
      
        
       
      
        public void paint(Graphics g)
        {
         super.paint(g);
           
            
           
          
             
           
        }
    
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    So, where did you get this code from?
    I haven't put it into any of my editors but what happens when you put that main method inside that main class and try to compile and run it?

    Comment

    • carlos123
      New Member
      • Sep 2007
      • 53

      #3
      i wrote this code in bluej ( a beginer program)

      Code:
      public class main implements ActionListener
      {
      error: class main is public , should be declared in a file named main.java

      Comment

      • carlos123
        New Member
        • Sep 2007
        • 53

        #4
        basically, i need help with this program, but too fully understand the problem, the people who want to help me, actually will have to put this code into their own editor and compile it themselves to see whats actually wrong with it.

        thanks.

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by carlos123
          i wrote this code in bluej ( a beginer program)

          Code:
          public class main implements ActionListener
          {
          error: class main is public , should be declared in a file named main.java
          In Java you can have only one public class in a source file. Either have each of your classes in separate files or remove the public modifier from all of then (you can leave the one whose name matches name of the source file if you want).

          Comment

          • carlos123
            New Member
            • Sep 2007
            • 53

            #6
            I still dont understand what you mean

            Comment

            • carlos123
              New Member
              • Sep 2007
              • 53

              #7
              I want to do the second option

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by carlos123
                I still dont understand what you mean
                I thought you said that you wrote this program. So you should understand what I mean by a public class because you wrote the code with the public classes.
                I have already explained what the second option entails. Removing the public access modifier from the headers of your classes.

                Comment

                • carlos123
                  New Member
                  • Sep 2007
                  • 53

                  #9
                  ugh............ ............... .......

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by carlos123
                    ugh............ ............... .......
                    What now?
                    Perhaps if you explain the part of my reply that you didn't understand ...

                    Comment

                    • carlos123
                      New Member
                      • Sep 2007
                      • 53

                      #11
                      what should i put instead of "public class"

                      and i know i should know this, but i dont.

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by carlos123
                        what should i put instead of "public class"

                        and i know i should know this, but i dont.
                        In "public class", public is the access modifier. So when I say remove the public class modifier, I mean change "public class" to just "class".

                        P.S Are we later on going to get to a point when you say that you are actually not the one who wrote this code?

                        Comment

                        • carlos123
                          New Member
                          • Sep 2007
                          • 53

                          #13
                          I did write this code, in bluej, but i have yet to learn how to like make it all one giant code together. its a hall pass program for a class that i have

                          Comment

                          • carlos123
                            New Member
                            • Sep 2007
                            • 53

                            #14
                            thanks bro. now on to the real problem, check out my new thread ;)

                            Comment

                            Working...