Results 1 to 4 of 4
  1. #1
    jairoh_ is offline Member
    Join Date
    Feb 2012
    Posts
    23
    Rep Power
    0

    Default Help in positioning and running to the web

    i really have a problem in setting a label, button, or textfield or etc in applets. how can i position in it in the coordinates i want? it's pretty embarrassing since everytime i add a label or a button, it will directly attach it in the center of the applet. setBounds or setLocation doesn't work. it's not like JFrame.
    here's my code. pls help tnx. I'm making sa simple paint applet
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /**
     *
     * @author
     */
    
    import javax.swing.*; //for the maine JFrame design
    import java.awt.*; //for the GUI stuff
    import java.awt.event.*;//for the event handling
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.util.*;//for reading the file
    
    public class Notepad extends JFrame implements ActionListener{
        
        private TextArea textArea;
        private MenuBar menuBar;
        private Menu file;
        private MenuItem openFile;
        private MenuItem saveFile;
        private MenuItem closeFile; 
        
        public Notepad(){
            super( "Java Notepad Practice" );
            this.setSize( 500, 300 );
            this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            
            this.getContentPane().setLayout( new BorderLayout() );//// the BorderLayout bit makes it fill it automatically
            
            this.textArea = new TextArea( "", 0, 0, TextArea.SCROLLBARS_VERTICAL_ONLY );
            this.textArea.setFont( new Font( "Century Gothic", Font.BOLD, 12 ) );
            this.getContentPane().add( this.textArea, BorderLayout.CENTER );
            
            this.menuBar = new MenuBar();
            this.setMenuBar( this.menuBar ); //add out menu bar into the GUI
            
            this.file = new Menu( "File");
            this.menuBar.add( this.file );//add a menu in the menu bar
            
            this.openFile = new MenuItem( "Open File" );
            this.file.add( this.openFile );
            this.openFile.setShortcut( new MenuShortcut( KeyEvent.VK_O, false ) );
            this.file.addActionListener( this );
            
            this.saveFile = new MenuItem( "Save File" );
            this.file.add( this.saveFile );
            this.saveFile.setShortcut( new MenuShortcut( KeyEvent.VK_S, false ) );
            
            this.closeFile = new MenuItem( "Close File" );
            this.file.add( this.closeFile );
            this.closeFile.setShortcut( new MenuShortcut( KeyEvent.VK_C, false ) );
            this.closeFile.addActionListener( this );
            
            
        }
        
        public void actionPerformed( ActionEvent e ){
            
            if( e.getSource() == this.closeFile )
                this.dispose();
            
            else if( e.getSource() == this.openFile){
                JFileChooser open = new JFileChooser(); //open up a file chooser(a dialog for the user to browse files to open)
                int option = open.showOpenDialog( this ); // get the option that the user selected(approve or cancel)
                
                //// NOTE: because we are OPENing a file, we call showOpenDialog~
                // if the user clicked OK, we have "APPROVE_OPTION"
                // so we want to open the file
                
                if( option == JFileChooser.APPROVE_OPTION ){
                    this.textArea.setText( "" ); //clear the textarea before applying the file context
                    
                    try{
                        // create a scanner to read the file 
                        //(getSelectedFile().getPath() will get the path to the file)
                        Scanner scan = new Scanner( new FileReader( open.getSelectedFile().getPath()) );
                        while( scan.hasNext() ) this.textArea.append( scan.nextLine() + "\n");
                    }catch( Exception ex ){
                        System.out.println( ex.getMessage() );
                    }
                }
            }
            else if( e.getSource() == this.saveFile ){
                JFileChooser save = new JFileChooser(); //again open a file chooser
                int option = save.showSaveDialog( this );
                
                if( option == JFileChooser.APPROVE_OPTION ){ 
                    try{
                        //create a buffered writer to write to a file
                        BufferedWriter out = new BufferedWriter( new FileWriter( save.getSelectedFile().getPath() ) );
                        out.write( this.textArea.getText() );//write the contents od the textArea to the file
                        out.close();
                    }catch( Exception ex ){
                        System.out.println( ex.getMessage() );
                    }
                }
            
            }
        }
        
        public static void main( String args [] ){
            new Notepad().setVisible( true );
        }
    }


    OT: why does it won't run on web. here is the ss


    Uploaded with ImageShack.us

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Help in positioning and running to the web

    > how can i position in it in the coordinates i want?
    Don't even try. Lear how to use layouts correctly: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    > why does it won't run on web. here is the ss
    Click 'Details' for more information.

    Also, programs run on a computer -- not 'on web'

    Additionally, Write in clear, grammatical, correctly-spelled language.

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

  3. #3
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    3

    Default Re: Help in positioning and running to the web

    how do you even get it to run in a web browser

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Help in positioning and running to the web

    Don't post to old dead threads, don't post an unrelated question in an ongoing thread and don't hijack another poster's thread.

    Threads are free; start a new thread and ask your question clearly, in a way that it might actually be answerable.

    db

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

Similar Threads

  1. Absolute positioning? I'm confused!
    By DrMadolite in forum AWT / Swing
    Replies: 2
    Last Post: 08-17-2012, 05:02 AM
  2. Positioning text in a frame
    By swenn in forum New To Java
    Replies: 1
    Last Post: 06-21-2012, 04:24 AM
  3. Vertical box and positioning
    By aborgeld in forum New To Java
    Replies: 9
    Last Post: 04-01-2011, 07:33 PM
  4. Positioning using setBounds
    By thayalan in forum AWT / Swing
    Replies: 4
    Last Post: 04-28-2009, 01:59 AM
  5. panel positioning
    By shwein in forum New To Java
    Replies: 4
    Last Post: 09-09-2008, 05:15 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
  •