Java Tutorial/Swing/JInternalFrame

Материал из Java эксперт
Версия от 04:57, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Customizing a JInternalFrame Look and Feel

Property StringObject TypeInternalFrame.actionMapActionMapInternalFrame.activeBorderColorColorInternalFrame.activeTitleBackgroundColorInternalFrame.activeTitleForegroundColorInternalFrame.activeTitleGradientListInternalFrame.borderBorderInternalFrame.borderColorColorInternalFrame.borderDarkShadowColorInternalFrame.borderHighlightColorInternalFrame.borderLightColorInternalFrame.borderShadowColorInternalFrame.borderWidthIntegerInternalFrame.closeButtonToolTipStringInternalFrame.closeIconIconInternalFrame.closeSoundStringInternalFrame.iconIconInternalFrame.iconButtonToolTipStringInternalFrame.iconifyIconIconInternalFrame.inactiveBorderColorColorInternalFrame.inactiveTitleBackgroundColorInternalFrame.inactiveTitleForegroundColorInternalFrame.inactiveTitleGradientListInternalFrame.layoutTitlePaneAtOriginBooleanInternalFrame.maxButtonToolTipStringInternalFrame.maximizeIconIconInternalFrame.maximizeSoundStringInternalFrame.minimizeIconIconInternalFrame.minimizeIconBackgroundColorInternalFrame.minimizeSoundStringInternalFrame.optionDialogBorderBorderInternalFrame.paletteBorderBorderInternalFrame.paletteCloseIconIconInternalFrame.paletteTitleHeightIntegerInternalFrame.resizeIconHighlightColorInternalFrame.resizeIconShadowColorInternalFrame.restoreButtonToolTipStringInternalFrame.restoreDownSoundStringInternalFrame.restoreUpSoundStringInternalFrame.titleButtonHeightIntegerInternalFrame.titleButtonWidthIntegerInternalFrame.titleFontFontInternalFrame.titlePaneHeightIntegerInternalFrame.useTaskBarBooleanInternalFrame.windowBindingsObject[ ]InternalFrameTitlePane.closeButtonAccessibleNameStringInternalFrameTitlePane.closeButtonTextStringInternalFrameTitlePane.closeIconIconInternalFrameTitlePane.iconifyButtonAccessibleNameStringInternalFrameTitlePane.iconifyIconIconInternalFrameTitlePane.maximizeButtonAccessibleNameStringInternalFrameTitlePane.maximizeButtonTextStringInternalFrameTitlePane.maximizeIconIconInternalFrameTitlePane.minimizeButtonTextStringInternalFrameTitlePane.minimizeIconIconInternalFrameTitlePane.moveButtonTextStringInternalFrameTitlePane.restoreButtonTextStringInternalFrameTitlePane.sizeButtonTextStringInternalFrameTitlePane.titlePaneLayoutLayoutManagerInternalFrameTitlePaneUIStringInternalFrameUIString


Customizing JInternalFrame.DesktopIcon Look and Feel

Property StringObject TypeDesktopIcon.backgroundColorDesktopIcon.borderBorderDesktopIcon.fontFontDesktopIcon.foregroundColorDesktopIcon.iconIconDesktopIcon.widthIntegerDesktopIcon.windowBindingsObject[ ]DesktopIconUIString


Demonstrating the use of the constants within a PropertyChangeListener.

import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
class InternalFramePropertyChangeHandler implements PropertyChangeListener {
  public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
    String propertyName = propertyChangeEvent.getPropertyName();
    System.out.println(propertyName);
    if (propertyName.equals(JInternalFrame.IS_ICON_PROPERTY)) {
      System.out.println("Icon property changed. React.");
    }
  }
}
public class InternalFramePropertyChangeHandlerSample {
  public static void main(final String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JDesktopPane desktop = new JDesktopPane();
    JInternalFrame internalFrame = new JInternalFrame("Can Do All", true, true, true, true);
    InternalFramePropertyChangeHandler ins = new InternalFramePropertyChangeHandler();
    // Add listener for iconification events
    internalFrame.addPropertyChangeListener(ins);
    desktop.add(internalFrame);
    internalFrame.setBounds(25, 25, 200, 100);
    JLabel label = new JLabel(internalFrame.getTitle(), JLabel.CENTER);
    internalFrame.add(label, BorderLayout.CENTER);
    internalFrame.setVisible(true);
    frame.add(desktop, BorderLayout.CENTER);
    frame.setSize(500, 300);
    frame.setVisible(true);
  }
}





DragMode: JDesktopPane.OUTLINE_DRAG_MODE

import java.awt.BorderLayout;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;

public class InternalFrameOutLineDragSample {
  public static void main(final String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JDesktopPane desktop = new JDesktopPane();
    JInternalFrame internalFrame = new JInternalFrame("Can Do All", true, true, true, true);
    desktop.add(internalFrame);
    internalFrame.setBounds(25, 25, 200, 100);
    JLabel label = new JLabel(internalFrame.getTitle(), JLabel.CENTER);
    internalFrame.add(label, BorderLayout.CENTER);
    internalFrame.setVisible(true);
    desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
    
    frame.add(desktop, BorderLayout.CENTER);
    frame.setSize(500, 300);
    frame.setVisible(true);
  }
}





JInternalFrame: JInternalFrame.isPalette

import java.awt.BorderLayout;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;

public class InternalFramePaletteSample {
  public static void main(final String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JDesktopPane desktop = new JDesktopPane();
    
    JInternalFrame palette = new JInternalFrame("Palette", true, false, true, false);
    palette.setBounds(350, 150, 100, 100);
    palette.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE);
    desktop.add(palette, JDesktopPane.PALETTE_LAYER);
    palette.setVisible(true);
    
    frame.add(desktop, BorderLayout.CENTER);
    frame.setSize(500, 300);
    frame.setVisible(true);
  }
}





JInternalFrame Property Constants

Property Name ConstantAssociated PropertyCONTENT_PANE_PROPERTYcontentPaneFRAME_ICON_PROPERTYframeIconGLASS_PANE_PROPERTYglassPaneIS_CLOSED_PROPERTYclosedIS_ICON_PROPERTYiconIS_MAXIMUM_PROPERTYmaximumIS_SELECTED_PROPERTYselectedLAYERED_PANE_PROPERTYlayeredPaneMENU_BAR_PROPERTYjMenuBarROOT_PANE_PROPERTYrootPaneTITLE_PROPERTYtitle


JInternalFrame style: Not Resizable, Not Closable, Not Maximizable, Not Iconifiable

The JInternalFrame is hidden when first created.



import java.awt.BorderLayout;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
public class DesktopInternalFrameStyleSample {
  public static void main(final String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JDesktopPane desktop = new JDesktopPane();
    JInternalFrame internalFrames[] = { new JInternalFrame("Can Do All", true, true, true, true),
        new JInternalFrame("Not Resizable", false, true, true, true),
        new JInternalFrame("Not Closable", true, false, true, true),
        new JInternalFrame("Not Maximizable", true, true, false, true),
        new JInternalFrame("Not Iconifiable", true, true, true, false) };
    int pos = 0;
    for (JInternalFrame internalFrame : internalFrames) {
      desktop.add(internalFrame);
      internalFrame.setBounds(pos * 25, pos * 25, 200, 100);
      pos++;
      JLabel label = new JLabel(internalFrame.getTitle(), JLabel.CENTER);
      internalFrame.add(label, BorderLayout.CENTER);
      internalFrame.setVisible(true);
    }
    frame.add(desktop, BorderLayout.CENTER);
    frame.setSize(500, 300);
    frame.setVisible(true);
  }
}





Listening to InternalFrameListener

import java.awt.BorderLayout;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;
class InternalFrameIconifyListener extends InternalFrameAdapter {
  public void internalFrameIconified(InternalFrameEvent internalFrameEvent) {
    JInternalFrame source = (JInternalFrame) internalFrameEvent.getSource();
    System.out.println("Iconified: " + source.getTitle());
  }
  public void internalFrameDeiconified(InternalFrameEvent internalFrameEvent) {
    JInternalFrame source = (JInternalFrame) internalFrameEvent.getSource();
    System.out.println("Deiconified: " + source.getTitle());
  }
}
public class InternalFrameIconifyListenerSample {
  public static void main(final String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JDesktopPane desktop = new JDesktopPane();
    JInternalFrame internalFrame = new JInternalFrame("Can Do All", true, true, true, true);
    InternalFrameListener internalFrameListener = new InternalFrameIconifyListener();
//     Add listener for iconification events
    internalFrame.addInternalFrameListener(internalFrameListener);
    desktop.add(internalFrame);
    internalFrame.setBounds(25, 25, 200, 100);
    JLabel label = new JLabel(internalFrame.getTitle(), JLabel.CENTER);
    internalFrame.add(label, BorderLayout.CENTER);
    internalFrame.setVisible(true);
    frame.add(desktop, BorderLayout.CENTER);
    frame.setSize(500, 300);
    frame.setVisible(true);
  }
}





Rules of Using Internal Frames

  1. You must set the size of the internal frame.
  2. You should set the location of the internal frame.

Dialogs that are internal frames should be implemented using JOptionPane or JInternalFrame, not JDialog. To create a simple dialog, you can use the JOptionPane showInternalXxxDialog methods.

Internal frames fire internal frame events, not window events.