Results 1 to 3 of 3
  1. #1
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default help with my layout please

    Hi so I trying to make this simple calculator and the only issue I am having is with the layout. I am trying to implement a gridlayout and use blank jlabels to fill spots I want a space but none of this seems to be happening. here is my code.

    Java Code:
    package com.blue2525989;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class MessingAround extends JFrame implements ActionListener {
        
        JPanel main = new JPanel();
        JLabel banner = new JLabel("Simple Calculator");
        JTextField input1 = new JTextField("0",10);
        JLabel add = new JLabel("");
        JTextField input2 = new JTextField("0",10);
        JTextArea output = new JTextArea("Heres your answer.");
        JLabel blank = new JLabel("");
        
        public MessingAround(){
            super("Start of a Calculator");
            
           setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           setSize(325,375);
           GridLayout grd = new GridLayout(3,5,15,15);
           setLayout(grd);
           
           main.add(blank);
           main.add(banner);
           main.add(blank);
           main.add(blank);
           main.add(input1);
           main.add(blank);
           main.add(blank);
           main.add(add);
           main.add(blank);
           main.add(blank);
           main.add(input2);
           JButton magic = new JButton("Add");
           magic.addActionListener(this);
           main.add(magic);
           JButton subtract = new JButton("Subtract");
           subtract.addActionListener(this);
           main.add(subtract);
           JButton divide = new JButton("Divide");
           divide.addActionListener(this);
           main.add(divide);
           JButton multiply = new JButton("Multiply");
           multiply.addActionListener(this);
           main.add(multiply);
           JButton sqrt = new JButton("Sqrt");
           sqrt.addActionListener(this);
           main.add(sqrt);
           main.add(blank);
           main.add(output);
           main.add(blank);
           add(main);
           setVisible(true);
            
        }
        
        public void actionPerformed(ActionEvent event) {
            String command = event.getActionCommand();
            if (command.equals("Add")) {
                magicB();
            }
            if (command.equals("Subtract")){
                subtract();
            }
            if (command.equals("Divide")) {
                divide();
            }
            if (command.equals("Multiply")) {
                multiply();
            }
            if (command.equals("Sqrt")) {
                sqrt();
            }
        }
    
        
        void magicB () {
                    try {
                    int inputOne = Integer.parseInt(input1.getText());
                    int inputTwo = Integer.parseInt(input2.getText());
                    int answer = inputOne + inputTwo;
            output.setText("" + answer);
            } catch (NumberFormatException e) {
                output.setText(e + " is not a number.");            
            }
           
        }
        
        void subtract () {
                    try {
                    int inputOne = Integer.parseInt(input1.getText());
                    int inputTwo = Integer.parseInt(input2.getText());
                    int answer = inputOne - inputTwo;
            output.setText("" + answer);
            } catch (NumberFormatException e) {
                output.setText(e + " is not a number.");            
            }
           
        }
        
        void divide () {
            try {
                int inputOne = Integer.parseInt(input1.getText());
                int inputTwo = Integer.parseInt(input2.getText());
                int answer = inputOne / inputTwo;
                output.setText(" "+ answer);
            } catch (NumberFormatException e) {
                output.setText(e + " is not a number.");
            }
        }
        
        void multiply () {
            try {
                int inputOne = Integer.parseInt(input1.getText());
                int inputTwo = Integer.parseInt(input2.getText());
                int answer = inputOne * inputTwo;
                output.setText("" + answer);
            } catch (NumberFormatException e) {
                output.setText(e + " is not a number.");
            }
        }
        
        void sqrt () {
            try {
                int inputOne = Integer.parseInt(input1.getText());
                int inputTwo = Integer.parseInt(input2.getText());
                int answer2 = inputOne + inputTwo;
                output.setText("" + Math.sqrt(answer2));
            } catch (NumberFormatException e) {
                output.setText(e + " is not a number.");
            }
        }
        
        public void setLookAndFeel() {
            try {
                UIManager.setLookAndFeel(
                "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
            } catch (Exception exc) {
                output.setText("Could not load Look and Feel..");
            }
        }
        
        public static void main(String[] arguments) {
            new MessingAround();
        }
    }

  2. #2
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    183
    Rep Power
    8

    Default Re: help with my layout please

    If am not mistaken (I left swings months ago for JavaFX) you can't locate a component into a container more the JLabel blank has to locate inside JPanel main once. you could try something like:

    Java Code:
     main.add(new JLabel(""));
     main.add(input1);
    main.add(new JLabel(""));
     main.add(input2);
    main.add(new JLabel(""));
     main.add(input3);
    main.add(new JLabel(""));
    ...
    When I used to program in swings I used to use a lot the GridBagLayout cause it's much more flexible than GridLayout.. so you may give it a look

    PS. I have no free time right now.. but I will try to come back to give a better answer.. if someone else doesn't give you one until then
    Last edited by ShadowWalker; 10-15-2016 at 04:21 PM.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: help with my layout please

    You might want to checkout BoxLayout. The tutorial also shows how to create fillers to take up space(the same type of
    thing you are trying to do with labels).

    How to Use BoxLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. GUI layout help
    By mbschultz97 in forum New To Java
    Replies: 3
    Last Post: 05-12-2014, 02:36 PM
  2. Layout help
    By mafyou in forum AWT / Swing
    Replies: 3
    Last Post: 03-28-2012, 07:14 AM
  3. Grid Layout change layout alignment of control s
    By rellicott in forum SWT / JFace
    Replies: 1
    Last Post: 02-13-2012, 05:11 PM
  4. looking for a layout
    By imorio in forum AWT / Swing
    Replies: 3
    Last Post: 08-13-2010, 05:02 PM
  5. Edit layout Layout please help me
    By manhtungtnk28@gmail.com in forum New To Java
    Replies: 4
    Last Post: 11-23-2009, 08:41 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
  •