Results 1 to 8 of 8
  1. #1
    mingming2009 is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default Gui program problem..

    ok, just want to say im new to Java, studying this language with C programming, HTML and javascript at university atm.
    Since my finals are coming up, i need the help of someone that can help me fix up a code of mine, that prints "click me" on a random Gui on a gridlayout manager - randomly on any of the 3x3 buttons that i created.

    I've seeked help from friends and lecturer, but none have helped me deal with this properly. The code is:


    /* Title: GridLayoutWeek11V2
    * Description: A sample gui class that prints a GUI interface of a
    * gridlayout
    *
    *Changes
    *1. Removed vgap and hgap on "(new GridLayout())" to contain 3 rows to
    * contain 9 buttons
    *2. Increased the number of array buttons to 9
    *3. Used if function to decide if the user clicks on the button
    *it would either be "Click me" or it would be void
    *
    * Copyright (c) Java in 60 Minutes a Day, pg 389-390
    *@authors Rich Raposa
    *@authors mingming2009
    *@date 22/03/09
    *ver1.2
    */
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.GridLayout;
    import java.lang.*;

    import java.awt.*;
    import java.awt.Container;
    import javax.swing.JFrame;
    import javax.swing.JButton;

    public class GridLayoutWeek11V2 extends JFrame{
    private JButton[] buttons;
    private int randInt;

    //public static void go(int width, int height) {
    public void go() {
    //JFrame f = new JFrame();
    //f.getContentPane().setLayout(new GridLayout(GridLayout.RIGHT,3,3));
    //f.setTitle("Click me if you can!");
    //f.setSize(300,300);
    //f.setVisible(true);
    //f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = this.getContentPane();
    contentPane.setLayout(new GridLayout(0,3));
    this.setTitle("Click me if you can!");
    this.setSize(300,300);
    //this.setSize(width,height);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );


    buttons = new JButton[9];


    for(int i=0; i < buttons.length; i++)
    {
    buttons[i] = new JButton("Button " + i);
    buttons[i].addMouseListener(new buttonListener());
    this.getcontentPane().add(new JButton(buttons[i]));
    }





    randInt = (int)(Math.random()*9);
    buttons[randInt].setText("Click Me!");

    }

    public static void main(String[] args) {
    GridLayoutWeek11V2 guiGame = new GridLayoutWeek11V2();
    guiGame.go();
    //go(new GridLayoutWeek11V2(),300,300);
    }

    public class buttonListener implements MouseListener{
    public void mousePressed(MouseEvent e) { }
    public void mouseReleased(MouseEvent e) { }
    public void mouseClicked(MouseEvent e) { }
    public void mouseEntered(MouseEvent e)
    {
    if(buttons[randInt].getText()=="Click Me"){

    int randInt = (int) (Math.random()*8);
    buttons[randInt].setText("Click Me!");
    }
    }
    public void mouseExited(MouseEvent e) { }
    }


    }

    This would help me a lot, on the upcoming assignment that I'm in need of help right now, and so would appreciate it a lot of anyone can help me correct the problems.

    Many thanks,

    mingming2009

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,309
    Blog Entries
    1
    Rep Power
    32

    Default

    You may wish to post a clear question here. What specifically do you need help with? What's working, what's not working?

  3. #3
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    12

    Default

    can your code complie?
    what is it?
    Java Code:
    this.getcontentPane().add(new JButton(buttons[i]));

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    12

    Default

    What is the problem with the code? What doesn't work? What errors are given?

  5. #5
    mingming2009 is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by mtyoung View Post
    can your code complie?
    what is it?
    Java Code:
    this.getcontentPane().add(new JButton(buttons[i]));
    yea, its that line that says is an error : cannot find symbol
    symbol: constructor JButton(javax.swing.JButton)

    and it says another error in the same line: cannot find symbol
    symbol: method getContentPane()

    Basically my problem is in my constructor, n so need help in fixing that up so the rest of the program compiles.

  6. #6
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    12

    Default

    Attempting to call the constructor using a JButton as the argument, which is not possible. The correct line is probably
    Java Code:
    this.getContentPane().add(buttons[i]);

  7. #7
    mingming2009 is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default

    yep, i jst realised that error and got that fixed luckily, along with some other parts of my coding. Thanks for helping anyway, now back to that java assignment..

  8. #8
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    12

Similar Threads

  1. Program problem
    By arindamchkrbrty in forum New To Java
    Replies: 8
    Last Post: 03-10-2009, 04:58 AM
  2. Help! problem with program (using JFrame)
    By linux1man in forum New To Java
    Replies: 5
    Last Post: 01-21-2009, 08:46 PM
  3. program problem
    By amith in forum AWT / Swing
    Replies: 12
    Last Post: 05-16-2008, 08:07 PM
  4. Problem with First Hibernate program
    By sireesha in forum JDBC
    Replies: 11
    Last Post: 05-05-2008, 04:19 PM
  5. Problem with my program HelloWorld
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 05:32 PM

Posting Permissions

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