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 8 of 8

Thread: I need help with gui.

  1. #1
    Member Emperor_Xyn's Avatar
    Join Date
    Dec 2011
    Posts
    66
    My Mood
    Devilish
    Thanks
    21
    Thanked 2 Times in 2 Posts

    Default I need help with gui.

    Ok I got this atm.

    PHP Code:

    package rpsgui
    ;

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    import javax.swing.Icon;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    public class 
    RPSgui {

        public static 
    void main(String[] args) {

    // Creates JFrame.
    JFrame jf = new JFrame("RPS - @Xyn") ;
    jf.setSize(500500);
    jf.setVisible(true);    
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Create Panel
    JPanel jp = new JPanel();
    jp.setVisible(true);        
            
    // Creates Play JButton.
    JButton pla = new JButton("Play RPS");
    pla.setLocation(12050);
    pla.setVisible(true);


    jf.add(jp);
    jf.add(pla);

    me tyl = new me();
    pla.addActionListener(tyl);



        }


    static public class 
    me extends RPSgui implements ActionListener{
        public 
    void actionPerformed(ActionEvent e){
            
    System.out.print("CLICKEDDD");
            
        }
        
    }} 
    The thing I have a question with. Inside the ActionListener is where you put the action of clicking the button right? It works and everything the thing is when someone clicks the bottom I want to remove the button from the frame so i can build a game inside instead.

    But I run into a problem since the buttons located outside the action listener I cannot refer to the button inside the main class...

    Just started doing ActionListener so sorry if they awsner is obvious.
    Last edited by Emperor_Xyn; December 19th, 2011 at 06:36 PM.


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: I need help with gui.

    What error are you getting (can you paste it/them here)? Also, is this PHP, or Java (the code said PHP above it...)?

    Another thing: why is your ActionListener class (me) extending your GUI's class (RPSgui)? I do not think that that extension is necessary for the ActionListener to function correctly.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. #3
    Member Emperor_Xyn's Avatar
    Join Date
    Dec 2011
    Posts
    66
    My Mood
    Devilish
    Thanks
    21
    Thanked 2 Times in 2 Posts

    Default Re: I need help with gui.

    I know that I probably didn't need to extend that, I was just testing something. It's java lol, I just enclosed it in PHP.


    I'm not getting an error, I was referring to removing the button in the ActionionListener class but I cannot close the button cause I cannot refer to the button at all in the action listener class.


    Like I want to do

    static public class me extends RPSgui implements ActionListener{
    public void actionPerformed(ActionEvent e){
    pla.remove(JButton1);

    but pla. or my JButton does not get recognized.
    Last edited by Emperor_Xyn; December 19th, 2011 at 07:06 PM.

  4. #4
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: I need help with gui.

    Oh! I see what you mean. Okay. Well, think about this: if you define a variable or Component in the main method, that is the only place from which it can be accessed. So what's the simplest way to allow the Component to be accessed from outside the main method?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  5. #5
    Member Emperor_Xyn's Avatar
    Join Date
    Dec 2011
    Posts
    66
    My Mood
    Devilish
    Thanks
    21
    Thanked 2 Times in 2 Posts

    Default Re: I need help with gui.

    Ohhhh

    So define a JButton like I would a string, and the Jbutton can be accsessed anywhere in the file? Like a regular global variable?

  6. #6
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: I need help with gui.

    So define a JButton like I would a string, and the Jbutton can be accsessed anywhere in the file? Like a regular global variable?
    Yeah. If you define the JButton outside the main method, you can still access it in the main method, and your ActionListener can get to it as well.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  7. The Following User Says Thank You to snowguy13 For This Useful Post:

    Emperor_Xyn (December 19th, 2011)

  8. #7
    Member Emperor_Xyn's Avatar
    Join Date
    Dec 2011
    Posts
    66
    My Mood
    Devilish
    Thanks
    21
    Thanked 2 Times in 2 Posts

    Default Re: I need help with gui.

    Thanks bud xD I didn't realise that is why they did that :O

    Thanks for your help =)

  9. #8
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: I need help with gui.

    Yeah, you're welcome! Glad I could help!
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.