How to call my Button inside my MainWindow - constructor

enter image description hereI have created two classes. One for a PushButton and one for a MainWindow. Now i want to call the PushButton inside my MainWindow, how can i do that.
*Fenster is my MainWindow
*Button is my PushButton <----that needs to be in my MainWindow.
I tried to put QPushButton *Button = new QPushButton(); in the constructor of the MainWindow, but i got the same result as now.

Related

Closing JFrames through button

I have used many methods like hide();, setvisibility and all. But they are not working.
How can I close it dynamically by clicking on another frame button?
I have used all things which are below on button but not working:
rest1.Disp ds = new rest1.Disp();
ds.setVisible(true);
rest.Cashier c = new rest.Cashier();
c.hide();
c.setVisible(false);
c.setDefaultCloseOperation(HIDE_ON_CLOSE);
c.setDefaultCloseOperation(EXIT_ON_CLOSE);
c.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
Step 1.
You should call the setDefaultCloseOperation during initialization of your JFrame. This tells the system how it should respond when the 'X' (close) button of your JFrame is clicked.
It has an integer parameter that can take 4 possible values:
DO_NOTHING_ON_CLOSE (defined in WindowConstants): Don't do
anything; require the program to handle the operation in the
windowClosing method of a registered WindowListener object.
HIDE_ON_CLOSE (defined in WindowConstants): Automatically hide the
frame after invoking any registered WindowListener objects.
DISPOSE_ON_CLOSE (defined in WindowConstants): Automatically hide and
dispose the frame after invoking any registered WindowListener
objects.
EXIT_ON_CLOSE (defined in JFrame): Exit the application
using the System exit method. Use this only in applications.
It sounds like DISPOSE_ON_CLOSE is what you are looking for - it will hide and dispose the JFrame on which the 'X' button was clicked.
So, in the initialization of your JFrame, call
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Further reading: https://docs.oracle.com/javase/tutorial/uiswing/components/frame.html
Step 2.
When you have your JFrame set up to be disposed on closing, you can also close it programmatically. You can use
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
as explained in this answer.
What happens then is that dispatchEvent will send the WINDOW_CLOSING event to the frame. The system knows it should dispose the frame, because you told it to DISPOSE_ON_CLOSE.
So, you should put that command in the ActionListener for the button that you use to close the frame:
someButton.addActionListener(
new ActionListener( )
{
public void actionPerformed(ActionEvent e)
{
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
}
}
);
Together, these two things accomplish that your frame is closed from a button other than the normal 'X' closing button.

Making a button that goes to a random frame (from an array) on the main timeline. How to do from a movie clip?

I saw plenty of questions that asked how to make a button in a movie clip that leads to a frame in the main timeline, and took a look at them. But is it different if you want to go to a random frame in a specific set of frames? I've never really used AS3 aside from simple things like stop(); or gotoAndPlay.
Here's what my main timeline looks like at the moment:
Here's the code I've got so far by using Google:
var frameB:Array=[1,28,45,56,71,91,106,126];
blue_circle1.addEventListener(MouseEvent.CLICK, choose);
function choose1(event:MouseEvent):void {
var randomFrame:Number = frameB[Math.floor(Math.random() * frameB.length)];
trace(randomFrame);
gotoAndPlay(randomFrame);
}
The code works fine when I use it on the rotation buttons in the main timeline. But it doesn't work when I put it in a button in a movie clip. I need to change it so it works. If there is a better way of doing this, I'm willing to try it.
Edit: I should clarify things more. I got rectangle on wheels that moves left to right. It does this in a movie clip. I want the button to move with it. But when I place the button into said movie clip, the code on the button stops working. I hope I didn't end up making things more confusing.
When you place the button and/or code inside the movieClip it changes the movieClip that gotoAndPlay() is referring to. You need to specify which movieClip you are calling gotoAndPlay() on. For the main timeline gotoAndPlay() works, but inside a movieClip you must use this:
parent.gotoAndPlay(randomFrame);
Or you may need to set parent's type to a MovieClip like this:
MovieClip(parent).gotoAndPlay(randomFrame);
However, it's best to use external .as files as it gives you the most control over your code.
Save the code below in a file called 'MyFlashAnimation.as'
Create a folder called 'mycodefolder' and put it in the same directory/folder as your .fla.
Use the 'MyFlashAnimation.as' as your document class. In the Flash/Animate IDE find the Properties panel, then the Publish section
In the Publish section where it says 'Class' enter: mycodefolder.MyFlashAnimation (do not add the .as)
CODE:
package mycodefolder {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class MyFlashAnimation {
private var animationClip:MovieClip;
private var blueCircle1:Button;
private var frameB:Array = [1,28,45,56,71,91,106,126];
// constructor
public function MyFlashAnimation() {
// this your main robot/car animation
// this assumes animation_clip is on the main stage
animationClip = this.animation_clip;
// this is your button. this assumes blue_circle1 is a child of
// your animation_clip. update the path if necessary.
// for example, it might be: animationClip.robot_body.blue_circle1
blueCircle1 = animationClip.blue_circle1;
// add listener
blue_circle1.addEventListener(MouseEvent.CLICK, choose);
}
function choose1(event:MouseEvent):void {
var randomFrame:Number = frameB[Math.floor(Math.random() * frameB.length)];
trace(randomFrame);
// tell animation clip to gotoAndPlay
animationClip.gotoAndPlay(randomFrame);
}
}
}
That should work. If you have all your movieClip paths correct.
"The code works fine when I use it on the rotation buttons in the main timeline. But it doesn't work when I put it in a button in a
movie clip."
What is the instance name of the MClip you paste the button into? That MC name will "add" to the final path of your button.
Examples:
(1) If your blue_circle1 on stage (you already can do this) :
blue_circle1.addEventListener(MouseEvent.CLICK, choose);
versus...
2) If your blue_circle1 is inside another MClip (with example name as: thingMC) :
thingMC.blue_circle1.addEventListener(MouseEvent.CLICK, choose);

add even-listener to a button on timeline that is a grand child of a movie clip

I got a simple flash scene that contain movie clip that contain sliding button that changing every few seconds:
every layer contain a button and another movie clip.
If I want to add event-listener to a simple button on stage, i just write:
f4.addEventListener(MouseEvent.CLICK, f4Click);
function f4Click(event:MouseEvent):void
{
flash.external.ExternalInterface.call("dlHTCMD", "switchtogame?code=fnf50");
}
but when I'm trying to access the button inside the two movie clips, like
optContainer.optBeach.btnBeach.addEventListener(MouseEvent.CLICK, btnBeachClick);
and I'm adding a trace function to see if the event are triggered but nothing is happening.
looks like a simple problem but i didn't find a solution.
I thought about extending the button class and add a bind function with the value as the name of the button and set the Event Listener but I'm not an AS3 expert :(
Thanks.
Try this:
// Pass mouse events to children
optContainer.mouseChildren = true;
optContainer.optBeach.mouseChildren = true;
// Reset hit area
optContainer.hitArea = null;
optContainer.optBeach.hitArea = null;
// Reset masks
optContainer.mask= null;
optContainer.optBeach.mask= null;
Also check whether on each key frame button have name.

actionscript 3 event.target

I have a movie clip named button1 and in this movie clip
there is a dynamic text named txt
public function mouse_down(event:MouseEvent)
{
if(event.target==button1)
{
...//this only recognizes when i click the button without intersecting the dynamic text area
}
if(event.target==button1||event.target==button1.txt)
{
...//this works
}
i would like to know why it dosen't recognize clicks made in the area that contains the dynamic click if i don't specify it, because txt is part of button1, so normally i would only need to check if the target is button1 but it dosen't work:i also have to check if the target is button1.txt
Thanks for your help!
event.target always points to the object the event originated from, even if it is nested in the object you added the listener to. Use event.currentTarget instead.
Check out this blog post to learn more.

Procedure to use the result from a textbox inside a popup - ActionScript

I am relatively new to ActionScript (have started with it 2 months ago), and have a little doubt of 'procedure' or 'technique' related to passing information between objects.
I have made a class that Pops-up a window that contains a panel with a textbox and two buttons, one for accepting, other for cancelling. It should work as a prompt in which you enter some text, and then if you like the changes, you accept, else, you cancel and the text you entered is discarded.
The thing I'm not sure how to handle is how to receive the text, once the user presses 'Accept', from the class I want to receive it from.
So, the approach I took is a bit cumbersome: firstly, when launching the popup, I associate with it a function (called onResult() in the code) from the 'class that launches', which will be called after the user presses the 'Accept' or 'Cancel' buttons; secondly, to get the text that the user inserted in the box, I keep a reference to it public from my class.
Please have a look at the code here:
http://pastebin.com/Kmud8rBe
I've also programmed in Android before, and the approach there would be much cleanier, just putting the text result from the popup inside a bundle inside an intent, and receiving it from the launched class. Here, I have to pass functions and such, which I don't like at all (although it works!).
So, my question is, for you ActionScript gurus out there, how would you approach this?
Thanks and regards!
pepillo
good that you created a class for your popup-functionality but why did you make all functions static? normal class with normal methods would be better ... and let the class extend from Sprite so that you can add the instance right to the stage.
package
{
import flash.display.Sprite;
public class Popup extends Sprite
{
public function Popup (label:String)
{
// add text and buttons ...
}
}
}
then you can just say:
var pop:Popup = new Popup("message");
addChild(pop);
and to get the data back after the popup is closed you would do sth like this:
private function onPressedAccept(event:MouseEvent):void
{
var text:String = _label.text;
// dispatch a custom event which saves the text as its data
dispatchEvent(new MyEvent(MyEvent.ACCEPT, text));
// close popup ...
parent.removeChild(this);
// or you would remove the popup in the ACCEPT eventlistener ...
}
listening for accept/cancel:
var pop:Popup = new Popup("message");
addChild(pop);
// add eventlistener
popup.addEventListener(MyEvent.ACCEPT, onAccept);
popup.addEventListener(MyEvent.CANCEL, onCancel);
private function onAccept(event:MyEvent):void
{
trace(event.data);
}
link about creating custom events:
http://www.8bitrocket.com/2007/7/31/Creating-Custom-Events-In-Flash-AS3-ActionScript-3/