aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes exit shuts both frames Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "exit shuts both frames" Watch "exit shuts both frames" New topic
Author

exit shuts both frames

Lou Caudell
Ranch Hand

Joined: Oct 06, 2001
Posts: 32
The question was posed to me, as to wheather an application could call another . I created the simple program below to prove such could be done. However, when the second frame is closed it also closes the first.
Can any one tel me why?
JDK 1.6 w/ XP SP++..., AMD 2600 Semp., 1.5gb Ram.
// ============================= file 1
public class App1
{
public static void main(String[] args)
{
App1Frame frame1 = new App1Frame(); // Create application frame.
frame1.pack();
frame1.setVisible(true); // Show frame
}
}
// ============================= file 2
import java.awt.*;
import java.awt.event.*;
import java.awt.Font;
import java.awt.Button;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;

public class App1Frame extends Frame
{
public App1Frame()
{
super("multi component frame");
setLayout(new FlowLayout());
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu();
MenuItem menuFileExit = new MenuItem();
menuFile.setLabel("File");
menuFileExit.setLabel("Exit");

// fonts
Font font14 = new Font("Sans Serif", Font.PLAIN, 14);
Font font18 = new Font("Sans Serif", Font.PLAIN, 18);

// label component
label = new Label("Start another Application frame");
label.setFont(font18);

// button component
button = new Button("TimeView");
button.setFont(font14);

// Add action listener.for the menu button
menuFileExit.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
App1Frame.this.windowClosed();
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
setTitle("App1");
setMenuBar(menuBar);
setSize(new Dimension(400, 400));
add(label);
add(button);
ButtonHandler bHandler = new ButtonHandler(this);
button.addActionListener(bHandler);
// Add window listener.
this.addWindowListener
(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
App1Frame.this.windowClosed();
}
}
);
}

protected void windowClosed()
{
System.exit(0);
} // method

public Dimension getPreferedSize()
{
return new Dimension(300, 300);
}
Button button;
Label label;
} // class

// ============================ file 3
import java.awt.*;
import java.awt.event.*;

public class App2Frame extends Frame
{
public App2Frame()
{
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu();
MenuItem menuFileExit = new MenuItem();
menuFile.setLabel("File");
menuFileExit.setLabel("Exit");

// Add action listener.for the menu button
menuFileExit.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
App2Frame.this.windowClosed();
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
setTitle("App2");
setMenuBar(menuBar);
setSize(new Dimension(400, 400));
Font font14 = new Font("Sans Serif", Font.PLAIN, 14);
Font font18 = new Font("Sans Serif", Font.PLAIN, 18);
label = new Label("Time view");
label.setFont(font18);
add(label);
this.addWindowListener
(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
App2Frame.this.windowClosed();
}
}
);
}

protected void windowClosed()
{
System.exit(0);
}

public Dimension getPreferedSize()
{
return new Dimension(300, 300);
}
Label label;
} // class

// ============================ file 4
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

class ButtonHandler implements ActionListener
{
public ButtonHandler( App1Frame application)
{
super();
app = application;
}

public void actionPerformed(ActionEvent e)
{
if( e.getSource() == app.button )
{
App2Frame frame2 = new App2Frame();
frame2.pack();
frame2.setVisible(true);
}
}
App1Frame app;
} // class
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> when the second frame is closed it also closes the first.
> Can any one tel me why?




[edit]
wrong frame # copied
[ December 20, 2006: Message edited by: Michael Dunn ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: exit shuts both frames