InternalFrameEventDemopublic class InternalFrameEventDemo extends JFrame implements InternalFrameListener, ActionListener
Fields Summary |
---|
JTextArea | display | JDesktopPane | desktop | JInternalFrame | displayWindow | JInternalFrame | listenedToWindow | static final String | SHOW | static final String | CLEAR | String | newline | static final int | desktopWidth | static final int | desktopHeight |
Constructors Summary |
---|
public InternalFrameEventDemo(String title)
super(title);
//Set up the GUI.
desktop = new JDesktopPane();
desktop.putClientProperty("JDesktopPane.dragMode",
"outline");
//Because we use pack, it's not enough to call setSize.
//We must set the desktop's preferred size.
desktop.setPreferredSize(new Dimension(desktopWidth, desktopHeight));
setContentPane(desktop);
createDisplayWindow();
desktop.add(displayWindow); //DON'T FORGET THIS!!!
Dimension displaySize = displayWindow.getSize();
displayWindow.setSize(desktopWidth, displaySize.height);
|
Methods Summary |
---|
public void | actionPerformed(java.awt.event.ActionEvent e)
if (e.getActionCommand().equals(SHOW)) {
//XXX: Can't reuse internal frame (bug 4138031).
//listenedToWindow.setVisible(true);
//XXX: Instead, create a new internal frame.
if (listenedToWindow == null) {
createListenedToWindow();
listenedToWindow.addInternalFrameListener(this);
desktop.add(listenedToWindow);
listenedToWindow.setLocation(
desktopWidth/2 - listenedToWindow.getWidth()/2,
desktopHeight - listenedToWindow.getHeight());
listenedToWindow.show(); //necessary as of 1.3
}
} else {
display.setText("");
}
| protected void | createDisplayWindow()
JButton b1 = new JButton("Show internal frame");
b1.setActionCommand(SHOW);
b1.addActionListener(this);
JButton b2 = new JButton("Clear event info");
b2.setActionCommand(CLEAR);
b2.addActionListener(this);
display = new JTextArea(3, 30);
display.setEditable(false);
JScrollPane textScroller = new JScrollPane(display);
//Have to supply a preferred size, or else the scroll
//area will try to stay as large as the text area.
textScroller.setPreferredSize(new Dimension(200, 75));
textScroller.setMinimumSize(new Dimension(10, 10));
displayWindow = new JInternalFrame("Event Watcher",
true, //resizable
false, //not closable
false, //not maximizable
true); //iconifiable
JPanel contentPane = new JPanel();
contentPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
contentPane.setLayout(new BoxLayout(contentPane,
BoxLayout.Y_AXIS));
b1.setAlignmentX(CENTER_ALIGNMENT);
contentPane.add(b1);
contentPane.add(Box.createRigidArea(new Dimension(0, 5)));
contentPane.add(textScroller);
contentPane.add(Box.createRigidArea(new Dimension(0, 5)));
b2.setAlignmentX(CENTER_ALIGNMENT);
contentPane.add(b2);
displayWindow.setContentPane(contentPane);
displayWindow.pack();
displayWindow.show(); //necessary as of 1.3
| protected void | createListenedToWindow()
listenedToWindow = new JInternalFrame("Event Generator",
true, //resizable
true, //closable
true, //maximizable
true); //iconifiable
//The next statement is necessary to work around bug 4138031.
listenedToWindow.setDefaultCloseOperation(
WindowConstants.DISPOSE_ON_CLOSE);
listenedToWindow.setSize(300, 100);
| void | displayMessage(java.lang.String prefix, javax.swing.event.InternalFrameEvent e)
String s = prefix + ": " + e.getSource();
display.append(s + newline);
| public void | internalFrameActivated(javax.swing.event.InternalFrameEvent e)
displayMessage("Internal frame activated", e);
| public void | internalFrameClosed(javax.swing.event.InternalFrameEvent e)
displayMessage("Internal frame closed", e);
listenedToWindow = null;
| public void | internalFrameClosing(javax.swing.event.InternalFrameEvent e)
displayMessage("Internal frame closing", e);
| public void | internalFrameDeactivated(javax.swing.event.InternalFrameEvent e)
displayMessage("Internal frame deactivated", e);
| public void | internalFrameDeiconified(javax.swing.event.InternalFrameEvent e)
displayMessage("Internal frame deiconified", e);
| public void | internalFrameIconified(javax.swing.event.InternalFrameEvent e)
displayMessage("Internal frame iconified", e);
| public void | internalFrameOpened(javax.swing.event.InternalFrameEvent e)
//XXX: We don't seem to get any of these.
displayMessage("Internal frame opened", e);
| public static void | main(java.lang.String[] args)
JFrame frame = new InternalFrameEventDemo(
"InternalFrameEventDemo");
//Quit this app when the big window closes.
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
|
|