Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By zFollette

Thread: GUI Layout Problem

  1. #1
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Question GUI Layout Problem

    I'm programming a calculator using the Swing and AWT libraries, and I have three questions for you. The text field and the area for buttons on my calculator are positioned using a grid layout, however even though the text field is much smaller than the button area it still takes up as much space as the button area, and I don't want it to. (If you don't understand what I mean, look at the picture linked below). So how do I make it smaller?

    My second question is: How do I create spaces between the buttons? I don't want them to be directly next to each other.

    My third question is: Why does the text field become grey when I make it uneditable, and how do I prevent it from?

    imgur: the simple image sharer

    Thanks for helping me! :)

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Calculator extends JFrame implements ActionListener{
    	private JTextField text = new JTextField(20);
    	
    	private JButton oneButton = new JButton("1"), twoButton = new JButton("2"), threeButton = new JButton("3"), divisionButton = new JButton("/"); // First row.
    	private JButton fourButton = new JButton("4"), fiveButton = new JButton("5"), sixButton = new JButton("6"), multiplicationButton = new JButton("*"); // Second row.
    	private JButton sevenButton = new JButton("7"), eightButton = new JButton("8"), nineButton = new JButton("9"), subtractionButton = new JButton("-"); // Third row.
    	private JButton clearButton = new JButton("C"), zeroButton = new JButton("0"), equalsButton = new JButton("="), additionButton = new JButton("+"); // Fourth row.
    	private JButton squareButton = new JButton("x\u00B2"), exponentButton = new JButton("x\u207F"), rootButton = new JButton("\u221A"), eraseButton = new JButton("\u2190"); // Fifth row.
    	
    	private JPanel textArea = new JPanel(), buttonArea = new JPanel();
    	
    	public Calculator(){
    		setTitle("Calculator");
    		setLayout(new GridLayout(2,1));
    		
    		text.setEditable(false);
    		textArea.add(text);
    		
    		buttonArea.setLayout(new GridLayout(5, 4));
    		buttonArea.add(oneButton); buttonArea.add(twoButton); buttonArea.add(threeButton); buttonArea.add(divisionButton);
    		buttonArea.add(fourButton); buttonArea.add(fiveButton); buttonArea.add(sixButton); buttonArea.add(multiplicationButton); 
    		buttonArea.add(sevenButton); buttonArea.add(eightButton); buttonArea.add(nineButton); buttonArea.add(subtractionButton); 
    		buttonArea.add(clearButton); buttonArea.add(zeroButton); buttonArea.add(equalsButton); buttonArea.add(additionButton);
    		buttonArea.add(squareButton); buttonArea.add(exponentButton); buttonArea.add(rootButton); buttonArea.add(eraseButton); 
    		
    		add(textArea);
    		add(buttonArea);
    		
    		setVisible(true);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setLocationRelativeTo(null);
    		setResizable(false);
    		pack();
    	}
    	
    	public void actionPerformed(ActionEvent e){
    		// I haven't started with the button functions yet :P
    	}
    	
    	public static void main(String[] args){
    		Calculator run = new Calculator();
    	}
    }

  2. #2
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: GUI Layout Problem

    GridLayout a constructor with 4 parameters, this is for padding on the outside.
    Java Code:
    GridLayout grid = new GridLayout(5, 4, 5, 5); //This would give 5px of padding (space) in between each row/column
    The grayness is on purpose. Its built in. If you don't like the gray, change setEditable(false); to setEnabled(false);

    To fix the Layout problem, set the frames Layout to a BorderLayout, add the JTextField to BorderLayout.NORTH, and add the buttonArea to BorderLayout.CENTER

    I hope I could help.
    Last edited by zFollette; 12-31-2013 at 02:30 AM.
    Zelaine likes this.

  3. #3
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: GUI Layout Problem

    Thanks, this helped me a lot! :)

  4. #4
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: GUI Layout Problem

    No problem. Though the only thing about setEnabled(false); is that you can't copy from it/paste to it.

Similar Threads

  1. Layout problem
    By bmL in forum Java Applets
    Replies: 3
    Last Post: 06-24-2012, 05:24 AM
  2. Layout Problem
    By kris5228 in forum AWT / Swing
    Replies: 6
    Last Post: 01-09-2012, 10:38 PM
  3. Layout problem
    By miller.bartek in forum AWT / Swing
    Replies: 3
    Last Post: 01-11-2011, 08:50 PM
  4. problem in managing layout
    By namreen in forum NetBeans
    Replies: 10
    Last Post: 09-04-2010, 10:41 AM
  5. [SOLVED] Layout Problem
    By casid in forum New To Java
    Replies: 4
    Last Post: 06-03-2009, 12:17 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
  •