Results 1 to 2 of 2

Thread: JFrame - help

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Location
    Propped up at a PC near you...
    Posts
    194

    JFrame - help

    what an earth have I done wrong here - I WAS sure that I was on the right track. This code compiles then error:

    Code:
    import java.io.*;
    	import java.awt.*;
    	import java.awt.event.*;
    	import javax.swing.*;
    
    	class ExampleFrame2a extends JFrame implements WindowListener {
    
    	public static void main (String [] args) {
    		
            ExampleFrame2a demo = new ExampleFrame2a();  // create an instance of the ExampleFrame object
    		
    		demo.setLayout(new GridLayout(4,4));  // set Layout manager to Grid Layout
            demo.setSize(900,700); 
            demo.addWindowListener(demo);  // register listener for window events, eg close
    		
    		String s=readTextFile("poem.txt"); // read from the poem file to a String
    		
    		Label fnameL = new Label("First Name");
    		demo.add(fnameL);
    		TextField fnameTF = new TextField(10);
    		demo.add(fnameTF);
    
    		Label SnameL = new Label("Surname");
    		demo.add(SnameL);
    		TextField SnameTF = new TextField(10);
    		demo.add(SnameTF);
    		
    		Label StreetL = new Label("Street");
    		demo.add(StreetL);
    		TextField StreetTF = new TextField(10);
    		demo.add(StreetTF);
    		
    		Label CityL = new Label("City");
    		demo.add(CityL);
    		TextField CityTF = new TextField(10);
    		demo.add(CityTF);
    
    		Label CountyL = new Label("County");
    		demo.add(CountyL);
    		TextField CountyTF = new TextField(10);
    		demo.add(CountyTF);
    
    		Label PCodeL = new Label("Post Code");
    		demo.add(PCodeL);
    		TextField PCodeTF = new TextField(10);
    		demo.add(PCodeTF);
    
    		TextArea myTextArea = new TextArea("",10,50);  // create a text area and add  it to the frame
    		demo.add(myTextArea);
    		
    		myTextArea.append(s); // append text from file to the text area
    	
    		demo.setVisible(true); 	// make Frame visible
        
    		}
    
    	public static String readTextFile(String filename) {
    	
    		BufferedReader inFile;
    		String line, s = "";  // intialize s
    	
    		try
    
    		{
    			inFile = new BufferedReader(new FileReader(filename));
    
    		while ( (line = inFile.readLine()) != null )
    		{
    			s = s + line + "\n"; // concatenate onto s, WITH  // AN EXTRA NEW LINE
     
    		}
    			inFile.close();
    		}
    		catch (IOException e)
    		{
    		System.out.println("Error in file " + filename + " : " 	+  e.toString() );
    		System.exit(1);  // better if -1, means standard error
    		}
    	
    		return s;
    		}
    	
        public void windowClosing(WindowEvent e) {  // code to handle closing the window
            System.exit(0);
    	    }
        
    		//unused empty WindowListener Methods --- needed to enable a        	// compile
    		public void windowIconified(WindowEvent e) {
    		}
    		public void windowOpened(WindowEvent e) {
    		}
    		public void windowClosed(WindowEvent e) {
    		}
    		public void windowDeiconified(WindowEvent e) {
    		}
    		public void windowActivated(WindowEvent e) {
    		}
    		public void windowDeactivated(WindowEvent e) {
    		}
    	}

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    The root components JFrame, JWindow, JDialog, JApplet and the lightweight container JInternalFrame() use the containment model provided by the root pane class. What this means is you must work with the content pane rather than the top level object itself. Since your ExampleFrame2a class extends JFrame the layout manager must be set on the content pane not the JFrame. Same goes for adding components to a top level object. They must be added to the content pane. There are multiple ways to do this.
    Code:
    JFrame jf = new JFrame(); 
    JLabel jl = new JLabel(); 
    Container c  = jf.getContentPane(); 
    c.add(jl);
    Code:
    JFrame jf = new JFrame();
    jf.getContentPane().add(new JLabel());
    The content pane can be obtained by first retrieving the root pane component then extracting the content pane, which is located inside the layered pane. But this can be very akward.
    Code:
    JRootPane rp  = jf.getRootPane(); 
    JLayeredPane jlp = rp.getLayeredPane();

Posting Permissions

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



Click Here to Expand Forum to Full Width