Results 1 to 7 of 7
  1. #1
    TopNFalvors is offline Member
    Join Date
    Mar 2011
    Posts
    41
    Rep Power
    0

    Default help with swing layout

    I have the code below that's supposed to add a bunch of buttons and also a label that olds the results of the calculations.

    The numbers are other buttons show up, but for some reason the label is not showing up.

    Is there something I'm forgetting?

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    
    public class superCalc extends JFrame {
        
    	 public static void main(String[] args) {
    
        	JFrame calcFrame = new superCalc();
    		calcFrame.setVisible(true);
    	 }
    	 
    	 public superCalc() {
    		JLabel resultsLabel = new JLabel("0");
    		JButton btn1 = new JButton("1");
       	JButton btn2 = new JButton("2");
       	JButton btn3 = new JButton("3");
       	JButton btn4 = new JButton("4");
       	JButton btn5 = new JButton("5");
    		JButton btn6 = new JButton("6");
    		JButton btn7 = new JButton("7");
    		JButton btn8 = new JButton("8");
    		JButton btn9 = new JButton("9");
    		JButton btn0 = new JButton("0");
    		
    		JButton btnPlus = new JButton("+");
    		JButton btnMinus = new JButton("-");
    		JButton btnMultiply = new JButton("*");
    		JButton btnDivide = new JButton("/");
    			
    		JButton btnEquals = new JButton("=");			
    		JButton btnClear = new JButton("C");
    
          JPanel calcPanel = new JPanel(new GridLayout(5,4));
          	
    		calcPanel.add(btn7);
    		calcPanel.add(btn8);
    		calcPanel.add(btn9);
    		calcPanel.add(btnDivide);
    		
    		calcPanel.add(btn4);
       	calcPanel.add(btn5);	
    		calcPanel.add(btn6);
    		calcPanel.add(btnMultiply);
    		
    		calcPanel.add(btn1);
         	calcPanel.add(btn2);
         	calcPanel.add(btn3);
    		calcPanel.add(btnMinus);
    		
    		calcPanel.add(new JLabel(""));
    		calcPanel.add(new JLabel(""));
    		calcPanel.add(btn0);
    		calcPanel.add(btnPlus);
    	
    		calcPanel.add(new JLabel(""));
    		calcPanel.add(new JLabel(""));			
    		calcPanel.add(btnEquals);
    		calcPanel.add(btnClear);	
    
    		JLabel resultsField = new JLabel();
    		
    		JPanel content = new JPanel();
          content.setLayout(new BorderLayout(8, 8));
          content.add(resultsField, BorderLayout.NORTH );
          content.add(calcPanel   , BorderLayout.CENTER);
            
          setContentPane(content);
    		setTitle("Calc");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		pack();		
        }
    	 
    }
    thanks

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

    Default

    How do you expect to see an empty JLabel? Why do you think an empty JLabel will take up any place in the layout?

    Add a line
    Java Code:
    System.out.println(resultsField.getPreferredSize());
    What does it reveal?

    db

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  4. #4
    nathic is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default

    JLabel resultsLabel = new JLabel("0");
    you're not using this one ..
    either remove it or pass it to the resultsField, which I think you've created excactly for that matter.

    If I have understood it right you should remove that line
    JLabel resultsField = new JLabel();
    and change this one
    content.add(resultsField, BorderLayout.NORTH );
    to
    content.add(resultsLabel, BorderLayout.NORTH );

  5. #5
    TopNFalvors is offline Member
    Join Date
    Mar 2011
    Posts
    41
    Rep Power
    0

    Default

    Great, thanks! I removed 'resultsField' and it worked!

  6. #6
    Ryan10 is offline Senior Member
    Join Date
    Mar 2011
    Posts
    144
    Rep Power
    0

    Default

    Mark as Solved if your satisfied

  7. #7
    TopNFalvors is offline Member
    Join Date
    Mar 2011
    Posts
    41
    Rep Power
    0

Similar Threads

  1. Swing Html Layout
    By Kiruthigadhandapani in forum AWT / Swing
    Replies: 1
    Last Post: 05-03-2011, 04:39 PM
  2. Need Swing Layout Advice
    By Rocketz in forum New To Java
    Replies: 2
    Last Post: 03-08-2011, 01:46 AM
  3. Replies: 13
    Last Post: 12-06-2010, 06:57 AM
  4. Swing layout is done wrong
    By Onra in forum New To Java
    Replies: 5
    Last Post: 10-08-2010, 05:36 AM
  5. Add Swing Layout.jar
    By anilkumar_vist in forum New To Java
    Replies: 0
    Last Post: 10-02-2010, 05:42 AM

Posting Permissions

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