/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tjinternalframe;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class TJInternalFrame extends JFrame {
Container container;
JButton button;
JDesktopPane desktop;
JInternalFrame internalFrame;
static int frameCount = 1;
static final int xOffSet = 25;
static final int yOffSet = 25;
// 1. Constructor of the frame class.
public TJInternalFrame() {
// 2. Give a title to the frame and get its content pane.
super("TJInternalFrame");
container = this.getContentPane();
// 3. Create a button and add it at the lower portion of the
// frame; also add an action listener.
button = new JButton("Click to Create More Internal Frames");
button.addActionListener(new ButtonListener());
container.add(button, BorderLayout.SOUTH);
// 4. Create a desktop pane and add an internal frame.
desktop = new JDesktopPane(); // holds the internal frame
container.add(desktop); // add the desktop to the main frame
createInternalFrame(); // create an internal frame
// 5. Add the window listener, set the frame size, default close
// operation and make the frame visible.
addWindowListener(new WindowEventHandler());
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setSize(400, 300); // width=400, height=300
show(); // Display the frame
}
// 6. Creates an internal frame and adds it to the desktop pane.
// Takes care of displaying frames with overlap offsets when called
// multiple times.
public void createInternalFrame() {
// 7. Use a suitable internal frame constructor.
JInternalFrame iFrame = new JInternalFrame(
"Internal Frame - " + (frameCount++),
true, // can be resized
true, // can be closed
true, // can be maximized
true); // can be iconified
// 8. Set the location and size, and add it to the desktop pane.
iFrame.setLocation( xOffSet*(frameCount-2),
yOffSet*(frameCount-2));
iFrame.setSize(200,150);
desktop.add(iFrame);
// 9. Let the frame be selected.
try {
iFrame.setSelected(true);
} catch (java.beans.PropertyVetoException ex) {
System.out.println(
"Exception while selecting an internal frame");
}
}
// 10. The button (action) listener.
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
createInternalFrame();
}
}
// 11. The listener class to handle closing of the frame.
class WindowEventHandler extends WindowAdapter {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
}
// 12. The main method.
public static void main(String[] args) {
TJInternalFrame frame = new TJInternalFrame();
}
}
Tidak ada komentar:
Posting Komentar