Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: 5 JPanels in 1 JFrame, last frame hidden.

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 5 JPanels in 1 JFrame, last frame hidden.

    Hi folks,
    I'm having this problem with a JFrame that I am adding 5 JPanels to. I have put borders on the JPanels, and set their size and location within the JFrame. The first 4 frames have gone in perfectly as I want them to, but the last (which is the only one that contains buttons - maybe a reason?) hasn't taken any of the things I have set to it. No border and not in the right location, it is hiding behind everything else. I have to maximize the JFrame window to see it at all. I have tried resizing the JFrame to (1000,1000) and the edge of the No button shows but that's all.
    Here is my code:
    import javax.swing.*; // Needed to build GUI.
    import java.awt.*; // Needed for some parts of the GUI (frame colours).
    import java.awt.event.*; // Needed to use event listeners.
    import java.text.DecimalFormat; /* Needed to format the percentage of vowels 
    											  to 2 decimal places. */
     
     
    public class Statistics extends JFrame
    { // Start of Statistics class.
     
    	public void StatsWindow(String text, String printable, String consonants, 
    	String vowels, int nonPrintable, char[] vowelArray, int[] vowelFrequency)
    	{ // Start of StatsWindow method.
     
    		DecimalFormat df = new DecimalFormat("#.##"); // Sets decimal format to 2 places.
     
    		// Declare and work out percentage for vowel frequency.
    		double aPerc = ((double)vowelFrequency[0] / printable.length()) * 100;
    		double ePerc = ((double)vowelFrequency[1] / printable.length()) * 100;
    		double iPerc = ((double)vowelFrequency[2] / printable.length()) * 100;
    		double oPerc = ((double)vowelFrequency[3] / printable.length()) * 100;
    		double uPerc = ((double)vowelFrequency[4] / printable.length()) * 100;
     
    		// Set the properties of the JFrame.
    		setTitle("Results of Text Analysis");
    		setSize(600, 425);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setLocationRelativeTo(null);
     
    		// Declare the labels for the results window. Some labels use HTML, some do not.
    		JLabel msg = new JLabel("The original text is:                ");
    		JLabel origStringLength = new JLabel("<html>Original length of text was " 
    		+ text.length() + 
    		" characters. <P>This includes white-space and non-printing characters.");
    		JLabel ratios = new JLabel(
    		"<html>The ratio of printing to non-printing characters is " 
    		+ printable.length() + ":" + nonPrintable 
    		+ ". <P>The ratio of vowels to consonants is " 
    		+ vowels.length() + ":" + consonants.length() + ".");
    		JLabel aFreq = new JLabel("The vowel 'A' occured " + vowelFrequency[0] 
    		+ " times. That is " + df.format(aPerc) + "% of the printable characters.");
    		JLabel eFreq = new JLabel("The vowel 'E' occured " + vowelFrequency[1] 
    		+ " times. That is " 	+ df.format(ePerc) + "% of the printable characters.");
    		JLabel iFreq = new JLabel("The vowel 'I' occured " + vowelFrequency[2] 
    		+ " times. That is " + df.format(iPerc) + "% of the printable characters.");
    		JLabel oFreq = new JLabel("The vowel 'O' occured " + vowelFrequency[3] 
    		+ " times. That is " + df.format(oPerc) + "% of the printable characters.");
    		JLabel uFreq = new JLabel("The vowel 'U' occured " + vowelFrequency[4] 
    		+ " times. That is " + df.format(uPerc) + "% of the printable characters.");
    		JLabel again = new JLabel("Would you like to analyse again?");
     
    		// Create new JTextArea with the original String in it and make it scrollable.
    		JTextArea origString = new JTextArea(text, 4, 25);
    		origString.setLineWrap(true);
    		origString.setWrapStyleWord(true);
    		JScrollPane scroll = new JScrollPane(origString, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
    		JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
     
    		// Create a Yes & No button for if the user wants to analyse again or not.
    		JButton yesButton = new JButton("Yes");
    		JButton noButton = new JButton("No");
     
    		// Create 5 new JPanels.
    		JPanel p1 = new JPanel();
    		JPanel p2 = new JPanel();
    		JPanel p3 = new JPanel();
    		JPanel p4 = new JPanel();
    		JPanel p5 = new JPanel();
     
    		// Set black borders around each panel.
    		p1.setBorder(BorderFactory.createLineBorder(Color.black));
    		p2.setBorder(BorderFactory.createLineBorder(Color.black));
    		p3.setBorder(BorderFactory.createLineBorder(Color.black));
    		p4.setBorder(BorderFactory.createLineBorder(Color.black));
    		p5.setBorder(BorderFactory.createLineBorder(Color.black));
     
    		// Set the size of each panel.
    		p1.setSize(600,100);
    		p2.setSize(600,50);
    		p3.setSize(600,50);
    		p4.setSize(600,110);
    		p5.setSize(600,115);
     
    		// Set the location in the JFrame of each panel.
    		p1.setLocation(0,0);
    		p2.setLocation(0,100);
    		p3.setLocation(0,150);
    		p4.setLocation(0,200);
    		p5.setLocation(0,310);
     
    		// Add two button listeners.
    		yesButton.addActionListener(new yesButtonListener());
    		noButton.addActionListener(new noButtonListener());
     
    		// Add elements to each panel.
    		p1.add(msg);
    		p1.add(scroll);
    		p2.add(origStringLength);
    		p3.add(ratios); 
    		p4.add(aFreq);
    		p4.add(eFreq);
    		p4.add(iFreq);
    		p4.add(oFreq);
    		p4.add(uFreq);
    		p5.add(again);
    		p5.add(yesButton);
    		p5.add(noButton);
     
    		// Add each panel to the JFrame.
    		add(p1);
    		add(p2);
    		add(p3);
    		add(p4);
    		add(p5);
     
    		// Set the window to visible.
    		setVisible(true);
    	} // End of StatsWindow method.
     
    	public class yesButtonListener implements ActionListener
    	{ // Start of yesButtonListener class.
    		public void actionPerformed(ActionEvent e)
    		{ // Start of actionPerformed method.
     
    			// Opens new instance of the ReadFromKeyboard class (opens new window).
    			new InputOrFile();
     
    			// Sets the old window to not be visible.
    			setVisible(false);
     
    			// Disposes of the old window once invisible.
    			dispose();
    		} // End of actionPerformed method.
    	} // End of yesButtonListener class.
     
    	public class noButtonListener implements ActionListener
    	{ // Start of noButtonListener class.
    		public void actionPerformed(ActionEvent e)
    		{ // Start of actionPerformed method.
     
    			// Create new JFrame for pop-up message.
    			JFrame frame = new JFrame();
     
    			// Open pop-up message.			
    			JOptionPane.showMessageDialog(frame, "Thank you, program will now exit.");
    			setVisible(false);
    			dispose();
     
    			// Stop the program.
    			System.exit(0);
    		} // End of actionPerformed method.
    	} // End of noButtonListener class.
    } // End of Statistics class.

    This is a screenshot of the window as it's opening now:

    This is a screenshot of when the window is maximized:


    I hope someone can see where I've gone wrong because I'm stuck fast!

    Thanks a lot.


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 5 JPanels in 1 JFrame, last frame hidden.

    managed to solve this after hours of staring at it. sorry to bother

Similar Threads

  1. 1 JFrame, more JPanels
    By jknwhz in forum AWT / Java Swing
    Replies: 3
    Last Post: October 24th, 2011, 11:18 PM
  2. Imported Class of Components are Hidden
    By polaris395 in forum AWT / Java Swing
    Replies: 3
    Last Post: July 25th, 2011, 09:35 AM
  3. displaying multiple jpanels in a jframe
    By tooktook22 in forum AWT / Java Swing
    Replies: 1
    Last Post: January 19th, 2011, 12:46 PM
  4. swing - switching JPanels
    By bbr201 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 5th, 2010, 09:18 AM
  5. repainting a jframe containing two jpanels
    By musasabi in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 11th, 2010, 10:31 PM