Graphical User Interface (GUI) Probelm

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jimgym1989
    New Member
    • Sep 2008
    • 30

    Graphical User Interface (GUI) Probelm

    Code:
    import java.util.*;
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Scanner;
    
    public class solano extends JFrame{
    
    private JLabel letterL ,countL, PercentageL;
    	private JTextArea display;
    	private JButton exitB;
    	private ExitButtonHandler ebHandler;
    	
    	private static final int WIDTH = 600;
    	private static final int HEIGHT = 600;
    	
    	private static final int SIZE1 = 300;
    	private static final int SIZE2 = 20;
    	
    	public solano(){
    		
    		letterL= new JLabel("Letter",SwingConstants.CENTER);
    		countL =  new JLabel("Count",SwingConstants.CENTER);
    		PercentageL =  new JLabel("Percentage",SwingConstants.CENTER);
    		
    		display = new JTextArea(26,3);
    		
    		//window
        	setTitle("Text Character Count");		
        	Container pane = getContentPane();
        	pane.setLayout(null);
        	letterL.setLocation(-70,1);
        	countL.setLocation(85,1);
        	PercentageL.setLocation(250,1);
        	
        	exitB = new JButton("Exit");
        	ebHandler = new ExitButtonHandler();
        	exitB.addActionListener(ebHandler);	
        	
        	display.setLocation(SIZE2,SIZE2);
        	letterL.setSize(SIZE1,SIZE2);
        	countL.setSize(SIZE1,SIZE2);
        	PercentageL.setSize(SIZE1,SIZE2);
        	display.setSize(500,500);
        	
        	exitB.setSize(100,SIZE2);
        	
        	
        	pane.add(letterL);		
        	pane.add(countL);		
        	pane.add(PercentageL);	
        		pane.add(exitB);
        	pane.add(display);
        	
        	display.setEditable(false);
        	
        	setSize(WIDTH, HEIGHT);
        	setVisible(true);
        	setDefaultCloseOperation(EXIT_ON_CLOSE); 
    
    private class ExitButtonHandler implements ActionListener
        {
        	public void actionPerformed(ActionEvent e)
        	{
        		System.exit(0);
        	}
        }
      public static void main(String[] args)throws FileNotFoundException
        	{
    	solano Count = new solano();
        		
        }
    I did not put all of my codes..
    Everytime I run my program the Exit Button is covering the Letter JLabel
    I want to make the Exit Button be on the buttom of my GUI
    How will I do that?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by jimgym1989
    [CODE]Everytime I run my program the Exit Button is covering the Letter JLabel
    I want to make the Exit Button be on the buttom of my GUI
    How will I do that?
    That's what you get for not using an appropriate LayoutManager. For now you
    can explicitly set the location of your exit button. You only set its size now.

    kind regards,

    Jos

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      Change [code=java]pane.add(exitB) ;[/code] to [code=java]pane.add(exitB, BorderLayout.SO UTH);[/code] and it should end up at the bottom of your pane. Also, you probably should read the Sun LayoutManager Tutorial - LayoutManagers make Life much easier.

      Greetings,
      Nepomuk

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Nepomuk
        Change [code=java]pane.add(exitB) ;[/code] to [code=java]pane.add(exitB, BorderLayout.SO UTH);[/code] and it should end up at the bottom of your pane.
        That'd only work if there *were* actually a BorderLayout manager installed.

        kind regards,

        Jos

        Comment

        • jimgym1989
          New Member
          • Sep 2008
          • 30

          #5
          Originally posted by JosAH
          That's what you get for not using an appropriate LayoutManager. For now you
          can explicitly set the location of your exit button. You only set its size now.

          kind regards,

          Jos
          Thank you Jos..you made my day!!
          keep it up!! and for those who answer this forum..
          I thank you all!!

          Comment

          • Nepomuk
            Recognized Expert Specialist
            • Aug 2007
            • 3111

            #6
            Originally posted by JosAH
            That'd only work if there *were* actually a BorderLayout manager installed.
            Correct me if I'm wrong, but BorderLayout is the default LayoutManager, so a BorderLayout manager is installed, isn't it? In the code for the BorderLayoutDem o in the BorderLayout Tutorial it says:
            Originally posted by sun
            [code=java]//Use the content pane's default BorderLayout. No need for
            //setLayout(new BorderLayout()) ;[/code]
            So, shouldn't it work although no LayoutManager was chosen here? They don't choose one in that example.

            Greetings,
            Nepomuk

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by Nepomuk
              Correct me if I'm wrong, but BorderLayout is the default LayoutManager, so a BorderLayout manager is installed, isn't it?
              Yup, normally it is but see what the OP did in line #32 (see above)

              kind regards,

              Jos

              Comment

              • Nepomuk
                Recognized Expert Specialist
                • Aug 2007
                • 3111

                #8
                Originally posted by JosAH
                Yup, normally it is but see what the OP did in line #32 (see above)
                Ah, right. Well. Not good. (Although line 32 wasn't that easy to find with the new code display...)

                Greetings,
                Nepomuk

                Comment

                Working...