This site requires JavaScript, please enable it in your browser!
Greenfoot back
Nosson1459
Nosson1459 wrote ...

2016/11/15

JFrame

Nosson1459 Nosson1459

2016/11/15

#
Is it possible to use JFrame (
1
2
3
4
5
6
7
8
9
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ClassName extends JFrame
{
    public static void main(String args[])
    {
    }
}
) in Greenfoot (besides for Greenfoot.ask() method)?
danpost danpost

2016/11/15

#
Nosson1459 wrote...
Is it possible to use JFrame < Code Omitted > in Greenfoot (besides for Greenfoot.ask() method)?
Tested and the answer is --- yes. I do not think you will be using the 'main' method however.
Nosson1459 Nosson1459

2016/11/16

#
danpost wrote...
Tested and the answer is --- yes. I do not think you will be using the 'main' method however.
If I'm asking if it's possible then I probably don't know about doing it, so you answered yes, but now I want to know how. I have a program I made (which starts off the way I did above) and I want to know how to run it in Greenfoot, it says Class compiled - no syntax errors which is good so far but I want my JFrame to be running in Greenfoot, could you maybe tell me how to do it (forgot to ask how to do it in first post, just asked if it's possible, but I was hoping if you see me ask that, then I probably don't know how either)?
danpost danpost

2016/11/16

#
This link shows you how to create a GUI with swing. In greenfoot, do not use a main method. Make sure to 'dispose' the JPanel object when done with it.
Nosson1459 Nosson1459

2016/11/17

#
The only thing there is, is links, so why couldn't you show in the above link which to go to instead of sending me to links. I know how to use Java Swing, I have my class that I made (which in Greenfoot is in the Other Classes box) I just want to know where to go or what to do so I can make these GUI applications pop up in Greenfoot, and what do you mean "Make sure to 'dispose' the JPanel object when done with it.", how do I put it in, and how do I dispose it? (which was my original question, almost) You said "I do not think you will be using the 'main' method however.", what should I write instead of "public static void main(String args){}"?
Nosson1459 Nosson1459

2016/11/17

#
I put in world
1
new ClassName().show();
it works and I didn't change any of the coding in ClassName even though you said I have to, Why? But when I press close on my JPanel it closes itself and Greenfoot then reopens both of them. I guess that's what you meant by dispose but I still don't know any of the things you were talking about so all questions asked still want answers.
danpost danpost

2016/11/17

#
I believe the command to run a JFrame is 'new ClassName().setVisible(true);'. The 'show' method is deprecated. Do not worry about 'dispose' for now. Any issues with that can be dealt with later (if and when you upload the project).
danpost danpost

2016/11/17

#
Nosson1459 wrote...
what should I write instead of "public static void main(String args){}"?
Nothing. The 'main' method is used to launch a project. Greenfoot, however, launches the project for you. Any code you may have had in the 'main' method should be placed in your World subclass (which would at least be the creating of the JFrame object).
Nosson1459 Nosson1459

2016/11/18

#
danpost wrote...
I believe the command to run a JFrame is 'new ClassName().setVisible(true);'. The 'show' method is deprecated. Do not worry about 'dispose' for now. Any issues with that can be dealt with later (if and when you upload the project).
danpost wrote...
Nothing. The 'main' method is used to launch a project. Greenfoot, however, launches the project for you. Any code you may have had in the 'main' method should be placed in your World subclass (which would at least be the creating of the JFrame object).
I did what you said in the first post (new ClassName.setVisible(true); (which I also already have in my original ClassName)), and there is no difference which is good, I hope. But the second post I don't know what I'm supposed to do, you said "I do not think you will be using the 'main' method however.", so why am I doing "Nothing.", and what do you mean "Greenfoot, however, launches the project for you." isn't that because of the line of code you just told me to put in? By the way all the code I have in my 'main' method is one line which is: "new ClassName().show();" which brings us back to that, but what do you mean put this in my world subclass, I thought we already created the JFrame because it pops up(, it just never closes!)?
danpost danpost

2016/11/18

#
Nosson1459 wrote...
what do you mean "Greenfoot, however, launches the project for you." isn't that because of the line of code you just told me to put in?
Not because of that line. I said that the 'main' method launches the project -- not the JFrame object. It is what java looks for the launch an application. If that one line was the only thing in the 'main' method, then by having placed it in your World subclass, you are set.
Nosson1459 Nosson1459

2016/11/18

#
danpost wrote...
If that one line was the only thing in the 'main' method, then by having placed it in your World subclass, you are set.
I guess so, but now that JFrame has to stay open forever in this scenario, it doesn't matter so much here cause the scenario doesn't do anything itself but for other scenarios I might want to open a JFrame, use it, then close it.
danpost danpost

2016/11/18

#
Nosson1459 wrote...
in this scenario, it doesn't matter so much here cause the scenario doesn't do anything itself but for other scenarios I might want to open a JFrame, use it, then close it.
Is there supposed to be a question here? If you want it to stay open, I guess you could do this:
1
2
3
4
5
// instance field
private JFrame jf = new ClassName();
 
// in world act
if ( ! jf.isShowing()) jf.setVisible(true);
and if not using it anymore, the last line would be:
1
if ( ! jf.isShowing()) jf.dispose();
Nosson1459 Nosson1459

2016/11/19

#
What do you mean "if jf is not showing then dispose", if it's not showing then what are you disposing, and why? I didn't ask the question here, because I already did earlier and you said not to worry about it now, but that won't help me, so I was telling you that not worrying isn't going to help anybody. (You did the quote wrong, I put a comma after "in this scenario," which is going back on the JFrame staying open, the way you quoted it, it doesn't make sense, the word scenario isn't needed twice.)
danpost danpost

2016/11/20

#
Nosson1459 wrote...
What do you mean "if jf is not showing then dispose", if it's not showing then what are you disposing, and why?
If you do not dispose it, the resources remain (the listeners continue to exist which reference the JFrame object, which must then also remain in memory). By using dispose, the listeners are removed and the JFrame object can then be flagged for garbage collection.
Nosson1459 Nosson1459

2016/11/20

#
Everything works, but that is because I put it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public static Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
private JFrame jf=new ClassName();
private boolean down;
/**
 * Constructor for objects of class MyWorld.
 *
 */
public MyWorld()
{
    super((int)screenSize.getWidth()/2, (int)screenSize.getHeight()/2, 1);
    jf.setVisible(true);
    Greenfoot.start();
}
public void act()
{
    if (down!=Greenfoot.isKeyDown("Shift")&&down!=Greenfoot.isKeyDown("F1"))
    {
        down=!down;
        if (!down)
        {
            if (  jf.isShowing()) jf.dispose();
            else if ( ! jf.isShowing()) jf.setVisible(true);
        }
    }
}
this scenario is just for testing as of now, I don't know if I'm actually going to use it for anything, just the coding in it (I wrote if ( jf.isShowing()) jf.dispose(); without the '!' to make it work).
You need to login to post a reply.