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: java and programming newbie need help with my GUI

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question java and programming newbie need help with my GUI

    hey guys this is my first post inside this forum so i hope i am accepted well. i just started my first java class and am having trouble with my week 3 lap. im pretty new at this so please bear with me. ill post my code and the text from the assignment. the trouble im having is that my grid wont display inside my frame for the buttons on the calculator. i also dont know how to change the colors on the calculator like im supposed to. im using eclipse as per request by the prof. any help you guys could give would be great. THANKS!!

    ps. i couldnt get the picture of what this calculator is to look like to display. but it is a standard 4x4 button calculator with the display at the top.

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import java.awt.*;


    public class ShowGridLayout extends JFrame{
    public ShowGridLayout () {

    JPanel p1 = new JPanel();
    p1.setLayout (new GridLayout(4, 4, 5, 5));

    for (int i=1; i<=16; i++); {
    p1.add(new JButton ("" + 1));
    }

    p1.add(new JButton("7"));
    p1.add(new JButton("8"));
    p1.add(new JButton("9"));
    p1.add(new JButton("/"));
    p1.add(new JButton("4"));
    p1.add(new JButton("5"));
    p1.add(new JButton("6"));
    p1.add(new JButton("*"));
    p1.add(new JButton("1"));
    p1.add(new JButton("2"));
    p1.add(new JButton("3"));
    p1.add(new JButton("-"));
    p1.add(new JButton("0"));
    p1.add(new JButton("."));
    p1.add(new JButton("="));
    p1.add(new JButton("+"));

    JPanel p2 = new JPanel(new BorderLayout());
    p2.add(new JTextField(""), BorderLayout.NORTH);
    p2.add(p1, BorderLayout.CENTER);

    }

    public static void main (String[] args){
    ShowGridLayout frame = new ShowGridLayout ();
    frame.setTitle("Calculator");
    frame.setSize(250, 250);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setVisible(true);
    }
    }

    Week 3 Programming Assignment

    Create Java programs for the following two problems.

    1. Write a Java application to display the following GUI. At this point you are only implementing the display. We are not ready to make the calculator actually do any calculations!



    This program has the following requirements:

    1. The size of the calculator is 250 x 250 pixels.
    2. The background and foreground color of the calculator buttons must alternate in a checker board pattern as shown above. You can choose any pair of colors for your foreground and background colors.
    3. The buttons should have at least 5 pixels of space between them.
    4. The text on the buttons should be SanSerif size 16 and be bold.
    5. Your application should be implemented in a single class. The main method of the class does nothing more than create an object of the class. The constructor of the class creates and displays the GUI. The constructor may call other methods of the class if needed.
    6. The class must inherit from JFrame as the following demonstrates:

    public myGUI extends JFrame { … }

    The extends keyword specifies inheritance. Inside the class you can directly access methods of the JFrame class without specifying an object due to inheritance. So when you want to add something to the frame, simply say

    add(someComponent);

    You can specify the title of the window in your constructor by simply adding the following line as the first thing in your constructor.

    super(“Title of your window!”);


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: java and programming newbie need help with my GUI

    Welcome acezeesawtooth
    Please see the Announcements page for the use of code tags and other useful forum tips.

    p1 adds a few buttons after a very short lived for loop.
    p2 adds a couple of things including p1. Then p2 and it's contents remain invisible while the frame is shown empty.
    To your second question, are you stuck on how to change the color of the object or how to determine which color it should be?

Similar Threads

  1. [SOLVED] Procedural programming veteran, Java OO newbie question, first time poster
    By feelingroovyslc in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 3rd, 2013, 12:44 PM
  2. programming newbie
    By blue02gls in forum Member Introductions
    Replies: 6
    Last Post: October 7th, 2011, 11:32 AM
  3. newbie in programming i need help
    By boredom in forum AWT / Java Swing
    Replies: 2
    Last Post: October 6th, 2011, 09:11 AM
  4. Java and programming newbie needing help
    By speedmaster in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 8th, 2010, 06:57 AM
  5. new to java programming GUI help
    By eg6_kaydoo in forum AWT / Java Swing
    Replies: 4
    Last Post: July 14th, 2010, 09:55 PM

Tags for this Thread